Hello,
I tried out to let my subclass "raceTrack" follow my Actor "Auto". When I copy danpost's basic code for the World subclass to follow an actor, I am getting 2x "Scroller" marked in red (underlined in the code below). I already replaced some spots with my actor class names. But what do I have to put in those spots marked red?
Hope someone can help me out :)
Edit: Just saw that underlining code doesn't work. But you should probably see where I tried to underline (Line 6, Line 14).
public class raceTrack extends World
{
public static final int WIDE = 800; // world width (viewport)
public static final int HIGH = 600; // world height (viewport)
[u]Scroller[/u] scroller; // the object that performs the scrolling
Actor scrollActor; // an actor to stay in view
public raceTrack()
{
super(WIDE, HIGH, 1, false); // creates an unbounded world
GreenfootImage bg = new GreenfootImage("World.png"); // creates an image to scroll (adjust as needed)
int bgWide = bg.getWidth(); // scrolling image width
int bgHigh = bg.getHeight(); // scrolling image height
scroller = new [u]Scroller[/u](this, bg, bgWide, bgHigh); // creates the Scroller object
scrollActor = new Auto(); // creates the actor to maintain view on (adjust class name as needed)
addObject(scrollActor, bgWide/2, bgHigh/2); // add actor at center of scrolling area (wherever)
scroll(); // sets initial background image and puts main actor in view if needed
//Car is being created
Auto auto = new Auto();
addObject(auto,440,105);
}
private void scroll()
{
// roaming limits of actor
int loX = 100;
int hiX = WIDE-100;
int loY = 50;
int hiY = HIGH-50;
// determine offsets and scroll
int dsx = 0, dsy = 0;
if (scrollActor.getX() < loX) dsx = scrollActor.getX()-loX;
if (scrollActor.getX() > hiX) dsx = scrollActor.getX()-hiX;
if (scrollActor.getY() < loY) dsy = scrollActor.getY()-loY;
if (scrollActor.getY() > hiY) dsy = scrollActor.getY()-hiY;
scroller.scroll(dsx, dsy);
}
}
