I am working on a simple Mario side-scroller, and When I try to call the Scrolling objects' scroll() method, I get the Cannot find symbol - method scroll() error thrown. Please help!
Here is the Subworld file
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();
}
}
`
Here is one of the 3 scrolling classes(they are identical except for 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);
}
}
Here is the 'Scrollable' interface
import greenfoot.*;
public interface Scrollable
{
public void scroll();
}
Here moving
is a object
of ArrayList
not Shamrock
so you can call only arraylist
Class method with moving
Object like add()
remove()
.
Here ArrayListis generic type Safed
arraylist. So, you need to remove the
objectfrom the arraylist then only try to apply method
Scroll()`
Use this code
((Shamrock)things.get(i)).scroll();
instead of things.get(i).scroll();