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

2021/3/26

Implementing Scrolling SuperWorld

hernry2812 hernry2812

2021/3/26

#
I am having trouble with implementing danposts Scrolling SuperWorld. I already created the SWorld and pasted everything into it. Now (if I understand right) I only have to adjust the code of the subclass of SWorld, which is RaceWorld in my case. Scrolling works already, but only horizontally. What I basically want is a "zoom window" around my actor following it (vertically and horizontally). My RaceWorld image is 1000x750. My Actor image is 46x24. RaceTrack and StartLine should scroll as the world does so they stick to it. This is my subclass of SWorld:
public class RaceWorld extends SWorld 
{
    public RaceWorld()
    {    
        super(750, 750, 1, 1000); // scroll world constructor call; last parameter is scroll width
        
        addObject(new RaceTrack(), 375, 375);
        addObject(new StartLine(), 372,126);
        
        setMainActor(new Car(), 250, 300);
        mainActor.setLocation(450, 125);
        
        GreenfootImage bg = new GreenfootImage("World.png");
        setScrollingBackground(bg);
        
    }
}
Any help is highly appreciated ^^
danpost danpost

2021/3/27

#
To scroll both horizontally and vertically, use the SWorld constructor with 5 int parameters. Line 5 is missing the final argument for scrolling height. Of course, if your world window is 750 high and the scrolling height is also 750, no vertical scrolling will happen anyway.
hernry2812 hernry2812

2021/3/27

#
When my RaceWorld is 1920x1440, the window is in the central of the world. I would like to move the window to another position, because I can't spawn my actor outside of the window. This is my code:
public class RaceWorld extends SWorld 
{
    public RaceWorld()
    {    

        super(800, 800, 1, 1920, 1440); // scroll world constructor call; last parameter is scroll width
        
        addObject(new RaceTrack(), 400, 400, true);
        
        setMainActor(new Car(), 1, 1);
        mainActor.setLocation(400, 400);
        
        GreenfootImage bg = new GreenfootImage("World.png");
        setScrollingBackground(bg);
        
    }
}
danpost danpost

2021/3/27

#
hernry2812 wrote...
When my RaceWorld is 1920x1440, the window is in the central of the world. I would like to move the window to another position, because I can't spawn my actor outside of the window.
But you can set its location outside of the window and have the scrolling mechanism "move the camera" back onto it. For example:
setMainActor(new Car(), 1, 1);
mainActor.setLocation(-600, 400);
super.act();
Do this AFTER setting the scrolling background.
You need to login to post a reply.