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

2021/10/1

Scenario completely breaks when adding Scrolling SuperWorld

1
2
Gbasire Gbasire

2021/10/1

#
Hello, I was working on a game, and tried to add the Scrolling SuperWorld by danpost. The problem is, everything breaks. Collisions sometimes don't work, the center of the world is not what it should be, everything is not working well. Is there something I can do to fix this, or is it doomed ? the gfar of my scenario : https://drive.google.com/file/d/1BdVg6vtCsz4H1ajzvDk99-TGG0VofaAy/view?usp=sharing
danpost danpost

2021/10/1

#
Gbasire wrote...
I was working on a game, and tried to add the Scrolling SuperWorld by danpost. The problem is, everything breaks. Collisions sometimes don't work, the center of the world is not what it should be, everything is not working well.
That scrolling system was an early one. It is not easy to work with. Better, and much easier, would be to use my Scroller class which comes with a tutorial and a demo. A link to the class itself can be found in the description of both.
Gbasire Gbasire

2021/10/2

#
thanks, can you explain what to do if I want to have a "camera" of 400x400 following my player (centered on him) on a, for example, 1000x800 world
Gbasire Gbasire

2021/10/2

#
with the camera not getting "out of borders" (stops following the player when he gets too close to the border of the world)
danpost danpost

2021/10/2

#
Gbasire wrote...
thanks, can you explain what to do if I want to have a "camera" of 400x400 following my player (centered on him) on a, for example, 1000x800 world
See ImageScrollWorld of demo.
Gbasire Gbasire

2021/10/2

#
it works very well, but one things still isn't working. adding the main actor out of camera and putting "scroll();" at the end of the constuctor still doesn't work and bugs the map out for some reason. it works if I don't put it, but it will take 1 act() time to update the cameras position
danpost danpost

2021/10/2

#
Gbasire wrote...
it works very well, but one things still isn't working. adding the main actor out of camera and putting "scroll();" at the end of the constuctor still doesn't work and bugs the map out for some reason. it works if I don't put it, but it will take 1 act() time to update the cameras position
Please show your world class codes.
Gbasire Gbasire

2021/10/2

#
import greenfoot.*;
public class WorldTownTrash extends World
{
    static int originalX = 400;
    static int originalY = 395;
    
    static boolean[] itemTaken = {};
    static boolean[] npcDone = {};
    
    Scroller scroller;
    Actor scrollActor;
    
    public static final int WIDE = 400, HIGH = 400;
    
    public WorldTownTrash()
    {
        super(WIDE, HIGH, 1, false);
        setPaintOrder(Transition.class, OverlayTextTriangle.class, OverlayText.class, ObjectBasicOver.class, DustCloud.class, Player.class, Objects.class);
        addData();
        addPlayer();
        addObjects();
    }
    
    public void act()
    {
        if(scrollActor != null)
            scroll();
    }
    
    public void scroll()
    {
        int dsX = scrollActor.getX() - WIDE / 2;
        int dsY = scrollActor.getY() - HIGH / 2;
        scroller.scroll(dsX, dsY);
    }
    
    public void addPlayer()
    {
        GreenfootImage bg = new GreenfootImage("BackOverworldGrass.png");
        scroller = new Scroller(this, bg, 880, 600);
        scrollActor = new Player();
        addObject(scrollActor, originalX, originalY);
        Player.speed = 2;
        scroll(); //if I remove this, it will work fine but will take one act action to update camera. if I let it here, it will break the map and bug some parts of the game
    }
    //all other methods, not relevant here
}
danpost danpost

2021/10/2

#
Try switching lines 20 and 21. or Try moving line 44 to 22.
Gbasire Gbasire

2021/10/2

#
it works perfectly, thank you really much !
Gbasire Gbasire

2021/10/2

#
Ok, well now I have an unexpected problem. When the world is scrolling, the collisions won't work. You can test it here Apparently, the objects are unable to detect if the object is touching the player (isTouching() method probably breaking) while the world is scrolling, resulting in my collision system breaking. When I try to add a condition for the scrolling to fix it (if the player just had entered in collision with an object, the variable canScroll is set to false, making it so that the world can't scroll), it gives another problem : link to a video of the problem Is it possible to fix this ? Collision code :
    //in Objects class
    public void checkPlayer()
    {
        Player player = (Player)getWorld().getObjects(Player.class).get(0);
        playerX = player.getX();
        playerY = player.getY();
        if(isTouching(Player.class) && playerY < getY() + center)
            player.hitBox();
    }
    //in Player class
    public void hitBox()
    {
        setLocation(originalX, originalY);
        WorldTownTrash.canScroll = false; //the canScroll variable I added to try to fix the problem
    }
    //in World class
    public void act()
    {
        if(scrollActor != null && canScroll) //the canScroll variable I added to try to fix the problem
            scroll();
        canScroll = true; //I checked it's the right place to put it, it won't do anything if I put it somewhere else
    }
Gbasire Gbasire

2021/10/3

#
I found a temporary solution : replacing the collision code in Player class by this, but I still hope a better solution exists. (it works because the player can only move in one direction at a time)
    public void hitBox()
    {
        setLocation(originalX, originalY);
        if(Greenfoot.isKeyDown("left"))
            move(1);
        else if(Greenfoot.isKeyDown("right"))
            move(-1);
        else if(Greenfoot.isKeyDown("up"))
            setLocation(getX(), getY() + 1);
        else if(Greenfoot.isKeyDown("down"))
            setLocation(getX(), getY() - 1);
        WorldTownTrash.canScroll = false;
    }
What I think causes the problem is that the world scrolls first, then an object detects a collision and prevents the world from scrolling, but the player already went too far because the world scrolled once, that's why I have to add this to "revert" the moves the player did. I might be wrong, but I think it is what happens.
danpost danpost

2021/10/3

#
Gbasire wrote...
the objects are unable to detect if the object is touching the player (isTouching() method probably breaking) while the world is scrolling, resulting in my collision system breaking.
The scrolling system has no affect whatsoever on your movement/collision system. All behaviors of your movement/collision is based on your movement/collision codes. As a side note, be it known that you can code everything aside from the scrolling system as if the scrolling system was not there at all. That is one of the beauties of how I make the scrolling system. That being said, adding the boolean canScroll field was not warranted. Any limitations on scrolling should be conditionals in the scroll method of your world class. Such as:
if (scrollActor == null || scrollActor.getWorld() == null) return;
danpost danpost

2021/10/3

#
Try adding the following line into your world constructor:
setActOrder(Player.class);
This is needed so that the Objects type actors can determine collisions on the same act cycle as when the player moves. That is, collisions were not being resolved immediately (on the same act cycle as when collision occurred).
Gbasire Gbasire

2021/10/3

#
Thank you really much, I didn't know setActOrder was a thing in Greenfoot, but it works perfectly ! By the way, your Scrolling class is truly amazing, and it doesn't even lag or bug on the greenfoot website :)
There are more replies on the next page.
1
2