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

2021/2/9

Retry button for pause menu and movement in platformer game

BogdanMicu1 BogdanMicu1

2021/2/9

#
I started a discussion (https://www.greenfoot.org/topics/64022/0) on these 2 topics a few days ago but I got banned momentarily from replying anymore there so I will continue it here. I'm trying to make a platformer and the movement I was previously using was buggy and broken. Danpost suggested me to use this movement: https://www.greenfoot.org/scenarios/11302 , and while I'm sure it generally works, in my particular case, after 1 second of movement, it freezes the entire project and has to be restarted. Regarding the retry button, the main issue which I did not know how to solve was: how do I know from which world the was the pauseMenu called so that I know which one has to be reloaded. My try was to create a method in each level which returns whether that was the last level or not through a return statement, but that did not work. Danpost suggested that I use if(getWorld() instanceof Lume), where lume is the first level, but I do not know how to use it properly :) . I cannot say that I know how to properly program in Java, but I master C++ and JavaScript, from where I use most of my knowledge. That's why I might get stuck with certain things that seem obvious, but aren't really that obvious to me if they're not implemented in one of the 2 programming languages listed above as well. So, can you help me debug the movement and implement the retry button, please? Thanks in advance! (sorry for the long essay, but I'm trying to compress as much information as possible so that I don't get disabled again by the admin's unterminabale "check for spam messages")
BogdanMicu1 BogdanMicu1

2021/2/9

#
This is how I implemented the movement from the link aforementioned:
static final int gravitatie = 2;
static final int hop = 30;
public int animationCounter = 0;
public int check = 1;
public int speedX = 4;
public int speedY = 0;
public void act()
{
    orizontala();
    verticala();
}

public void orizontala()
{
    int worldWidth = getWorld().getWidth();
    int myWidth = getImage().getWidth();
    int dx=0;
    if (Greenfoot.isKeyDown("d")) 
    {
        dx++;
        animationCounter++;
        mersAnm();
        check = 1;
    }
    if (Greenfoot.isKeyDown("a"))
    {
        dx--; 
        animationCounter++;
        mersInvAnm();
        check = 2;
    }
    if(dx==0) {idleAnm();return;}
    setLocation(getX()+dx*speedX, getY());
    if(getX()<myWidth/2)setLocation(myWidth/2,getY());
    if(getX()>worldWidth-myWidth/2)setLocation(worldWidth-myWidth/2, getY());
    while(isTouching(Actor.class))setLocation(getX()-dx,getY());
}

public void verticala()
{
    int worldHeight=getWorld().getHeight();
    int myHeight=getImage().getHeight();
    boolean onGround = false;
    speedY+=gravitatie;
    setLocation(getX(), getY()+speedY);
    if(getY() > worldHeight-myHeight/2)
    {
        setLocation(getX(), worldHeight-myHeight/2);
        speedY=0;
        onGround = true;
    }
    int dy = (int) Math.signum(speedY);
    while(isTouching(Actor.class))
    {
        setLocation(getX(), getY()-dy);
        if(dy>0)onGround = true;
        speedY=0;
    }
    if(onGround&&Greenfoot.isKeyDown("space"))speedY=-hop;
}
BogdanMicu1 BogdanMicu1

2021/2/9

#
I have to mention that all of my levels have a ground actor that covers the bottom, so it's not the default one. I tested it and it only works in the cases when the ground is the default bottom of the screen. Another example of the issue previously explained was encountered now: when the actor walks a few pixels on any platform, he gets teleported back to its margin. I think this is the cause of the crash as well. The algorithm tries to teleport him off the ground actor, but since it covers the whole bottom portion of the screen, it tries to teleport him out of it, which leads to the inevitable crash.
danpost danpost

2021/2/9

#
I think the problem is due to the animation. If the images are not all of the same dimensions, then that complicates matters dramatically.
BogdanMicu BogdanMicu

2021/2/9

#
So, I tried the retry button implementation like this:
public class retryButton extends Buttons
{
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            if(getWorld() instanceof level1)Greenfoot.setWorld(new level1());
            if(getWorld() instanceof level2)Greenfoot.setWorld(new level2());
            if(getWorld() instanceof level3)Greenfoot.setWorld(new level3());
            if(getWorld() instanceof level4)Greenfoot.setWorld(new level4());
            if(getWorld() instanceof level5)Greenfoot.setWorld(new level5());
        }
        
    }    
}
But it would only work in the case it would exist in the world I'm trying to reset. The problem is: it is not. I added a functionality to each level so that, when ESC is pressed, it switches to the pauseMenu world so, the getWorld() would result every time in pauseMenu and all the if's would be false. How can I remember the world I call the pauseMenu from so that I can reset/resume it from there?
danpost danpost

2021/2/9

#
BogdanMicu wrote...
So, I tried the retry button implementation like this << Code Omitted >>]
Pass world to button:
public class retryButton extends Buttons
{
    World world;
    
    public retryButton(World w)
    {
        world = w;
    }
    
    public void act()
    {
        if(Greenfoot.mouseClicked(this))
        {
            if(world instanceof level1)Greenfoot.setWorld(new level1());
            if(world instanceof level2)Greenfoot.setWorld(new level2());
            if(world instanceof level3)Greenfoot.setWorld(new level3());
            if(world instanceof level4)Greenfoot.setWorld(new level4());
            if(world instanceof level5)Greenfoot.setWorld(new level5());
        }
    }
}
BogdanMicu1 BogdanMicu1

2021/2/9

#
danpost wrote...
I think the problem is due to the animation. If the images are not all of the same dimensions, then that complicates matters dramatically.
Yeah, I think the animation might be the problem since it doesn't "animate" the actor when he is moving. He freezes in his first frame but still moves a little, afterwards the project freezes as well. I'll redo all the frames to the same size and come back with a reply as soon as possible.
BogdanMicu1 BogdanMicu1

2021/2/9

#
The animations were the problem!!! Thank you so much! Now it works!
BogdanMicu BogdanMicu

2021/2/10

#
Ok, so I've got a second character whose animation has to be longer than its idle image. After implementing the movement for it as well, the only bug I found was this one: https://youtu.be/6YAjadzhNzE . How could I fix it, so that it teleports him forward, rather than backward, without breaking the mechanics that do still work?
danpost danpost

2021/2/10

#
BogdanMicu wrote...
I've got a second character whose animation has to be longer than its idle image. After implementing the movement for it as well, the only bug I found was this one: << Link Omitted >> How could I fix it, so that it teleports him forward, rather than backward, without breaking the mechanics that do still work?
I guess when a longer image is set from a shorter one , collision check and use difference in x-coordinates to determine where the actor needs its position adjusted to.
BogdanMicu1 BogdanMicu1

2021/2/10

#
The game has a second character whose animation has to be longer than the idle image. After implementing the movement for him as well, the only bug I found was that when it is right after a platform that would normally not stop him, he teleports behind it like this: https://youtu.be/6YAjadzhNzE Do you know of a way to fix this?
BogdanMicu BogdanMicu

2021/2/10

#
How could I get the object that my actor is touching, using an if statement to check first if it's part of a certain class
if(isTouching(Platform.class))
    {
        
    }
I first need to check whether it's touching an object of that class, and then obtain that particular object. The longer method with intersects(obj) wouldn't work because each level has a different number of platforms.
BogdanMicu1 BogdanMicu1

2021/2/10

#
Sorry for the spam, older posts who needed to get checked keep appearing.
danpost danpost

2021/2/10

#
BogdanMicu wrote...
How could I get the object that my actor is touching, using an if statement
Platform p = (Platform)getOneIntersectingObject(Platform.class);
if (p != null) ...
You need to login to post a reply.