找不到符号 - 方法 scroll()
Cannot find symbol - method scroll()
我正在开发一个简单的 Mario 横向卷轴器,当我尝试调用滚动对象的 scroll() 方法时,我得到 Cannot查找符号 - 方法 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 个滚动条之一 类(除了 Name/picture
,它们完全相同
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);
}
}
这里是'Scrollable'界面
import greenfoot.*;
public interface Scrollable
{
public void scroll();
}
things
是 Actor
的列表。 Actor
没有实现 Scollable
,但是 Shamrock
实现了。 things.get(i)
returns Actor
没有 Scollable.scroll()
。您需要将其转换为 Shamrock
或 Scollable
。或者您可以将 things
设为 Shamrock
.
的列表
这里 moving
是 ArrayList
的 object
而不是 Shamrock
所以你只能用 [=11] 调用 arraylist
Class 方法=] 像 add()
remove()
.
这样的对象
此处ArrayListis generic type Safed
arraylist. So, you need to remove the
objectfrom the arraylist then only try to apply method
Scroll()`
使用此代码
((Shamrock)things.get(i)).scroll();
而不是things.get(i).scroll();
我正在开发一个简单的 Mario 横向卷轴器,当我尝试调用滚动对象的 scroll() 方法时,我得到 Cannot查找符号 - 方法 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 个滚动条之一 类(除了 Name/picture
,它们完全相同 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);
}
}
这里是'Scrollable'界面
import greenfoot.*;
public interface Scrollable
{
public void scroll();
}
things
是 Actor
的列表。 Actor
没有实现 Scollable
,但是 Shamrock
实现了。 things.get(i)
returns Actor
没有 Scollable.scroll()
。您需要将其转换为 Shamrock
或 Scollable
。或者您可以将 things
设为 Shamrock
.
这里 moving
是 ArrayList
的 object
而不是 Shamrock
所以你只能用 [=11] 调用 arraylist
Class 方法=] 像 add()
remove()
.
此处ArrayListis generic type Safed
arraylist. So, you need to remove the
objectfrom the arraylist then only try to apply method
Scroll()`
使用此代码
((Shamrock)things.get(i)).scroll();
而不是things.get(i).scroll();