1

我正在开发一个简单的马里奥侧卷轴,当我尝试调用 Scrolling 对象的scroll()方法时,我得到了无法找到符号 - 方法 scroll()引发的错误。请帮忙!
这是子世界文件

import greenfoot.*;
import java.util.ArrayList;
/**
 * Write a description of class MarioWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a 


public class MarioWorld extends World
{
    ArrayList<MovingActor> moving = new ArrayList<MovingActor>();
    ArrayList<Actor> things = new ArrayList<Actor>();
    Message messagebox = new Message("");
    /**
     * Constructor for objects of class MarioWorld.
     * 
     */
    public MarioWorld()
    {
        super(800, 600, 1);
        for(int i = 0; i < 40; i++)
            {
                things.add(new GreenApple());
                things.add(new Shamrock());

            }
            for(int i = 0; i<8;i++)
                things.add(new Brick());
            moving.add(new Mario());
            for(int r = 0;r < things.size();r++)
            {
                addObject(things.get(r),Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(600));
            }
    }
    public void act()
    {

       for(int i = 0;i < things.size();i++)
            {
                things.get(i).scroll();
            }
            addObject(moving.get(0),15,300);
            moving.get(0).worldact();
    }


}
`

这是 3 个滚动类之一(除了名称/图片外,它们是相同的

 import greenfoot.*;
public class Shamrock extends Actor implements Scrollable
{
    /**
     * Act - do whatever the Shamrock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public Actor worldact()
    {
        scroll();
        return this;
    }
    public void scroll()
    {
        int x = getX();
        int y = getY(); 
        if(x <= 0)
            x = 800;
        x -= 6;
        setLocation(x,y);

    }
}

这是“可滚动”界面

import greenfoot.*;

public interface Scrollable  
{
 public void scroll();
}
4

2 回答 2

0

things是一个列表ActorActor不实现Scollable,但Shamrock实现。things.get(i)返回Actor没有Scollable.scroll(). 您需要将其转换为ShamrockScollable。或者,您可以things列出Shamrock.

于 2016-03-08T04:25:33.563 回答
0

moving是一个objectnotArrayList 所以Shamrock你只能用Object调用arraylistClass 方法。movingadd() remove()

这里 ArrayList 数组is generic type Safed列表. So, you need to remove the对象from the arraylist then only try to apply methodScroll()`

使用此代码

((Shamrock)things.get(i)).scroll(); 

代替things.get(i).scroll();

于 2016-03-08T04:29:09.400 回答