This site requires JavaScript, please enable it in your browser!
Greenfoot back
hernry2812
hernry2812 wrote ...

2021/3/22

Camera following Actor

hernry2812 hernry2812

2021/3/22

#
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?
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);
    }
}
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).
danpost danpost

2021/3/22

#
Did you create a Scroller class and copy the Scroller class codes into it?
hernry2812 hernry2812

2021/3/22

#
No, have you got a link to it? Would be very helpful^^
danpost danpost

2021/3/22

#
hernry2812 wrote...
No, have you got a link to it? Would be very helpful^^
The link can be found in the description of either my Scrolling Tutorial scenario or my Scroller Class Demos scenario (or both).
You need to login to post a reply.