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

2021/2/17

Movement debug

BogdanMicu BogdanMicu

2021/2/17

#
I've got a platformer game with 2 main characters. One has the usual parkour movement, while the other is a little tweaked to resemble the flappy bird one. It works perfectly in all cases but one: moving platforms in opposite direction. By that, I mean that, for the flappy bird movement, when flying in the side of one such platform, which is going the opposite way, the platform somehow gets inside of the character, resulting in a crash from the whole compiler. This would be a more graphic example: flying towards the side of the platform moving the opposite way character ----------------------------------------->crash<---------------------------------platform When the platform is going the same way as the character, it behaves the right way. The same story could be said about ascending platforms as well. When the character is standing still on it, there's no problem at all, but when moving, he is instantly teleported to the side, resulting in his fall. How would you recommend to modify the surface checks in order to solve these issues? Here's the movement of the first character(the basic one):
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(Surface.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(Surface.class))
    {
        setLocation(getX(), getY()-dy);
        if(dy>0)onGround = true;
        speedY=0;
    }
    if(onGround&&Greenfoot.isKeyDown("space"))speedY=-hop;
}
And this is the movement of the second character:
public void act()
{
        orizontala();
        verticala();
}
public void orizontala()
{
    int worldWidth = getWorld().getWidth();
    int myWidth = getImage().getWidth();
    int pltWidth = 60;
    int dx=0;
    String key = Greenfoot.getKey();
    if(Greenfoot.isKeyDown("d") && ((level)getWorld()).c>0) 
    {
        dx++;
        fataAnm();
        check=1;
        if(!Greenfoot.isKeyDown("a"))
        {
            counter++;
            temp();
        }
    }
    if(Greenfoot.isKeyDown("a") && ((level)getWorld()).c>0) 
    {
        dx--;
        spateAnm();
        check=2;
        if(!Greenfoot.isKeyDown("d"))
        {
            counter++;
            temp();
        }
    }
    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(Surface.class))setLocation(getX()-dx,getY());
}
public void temp()
{
    if(counter%5==0)
    ((level)getWorld()).c--;
}
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(Surface.class))
    {
        setLocation(getX(), getY()-dy);
        if(dy>0)onGround = true;
        speedY=0;
    }
    if(Greenfoot.isKeyDown("space") && ((level)getWorld()).c>0)
    {
        speedY=-hop;
        if(check == 1)fataAnm();
        else spateAnm();
        counter++;
        temp();
    }
}
I thought of adding an additional object checker in a certain radius, but it would be much better if I could somehow modify the first one.
RcCookie RcCookie

2021/2/18

#
BogdanMicu wrote...
if(dx==0) {idleAnm();return;}
You really went all in with the one-lineing on that one ;-) For your problem, you said there was a „compiler crash“? I suppose you mean an Exception or Error was thrown. I suppose it’s a StackOverflowError. While loops are prone to cause these if the condition stays true. Also, the Greenfoot error console really does not like the StackOverflowErrors and looks like it’s crashed. But could you please show the error message? Btw even if it crashed that’s not the compiler. The compiler translates source code into bytecode (before anything runs), the JVM (Java Virtual Machine) runs that bytecode. It may crash.
danpost danpost

2021/2/19

#
RcCookie wrote...
Btw even if it crashed that’s not the compiler. The compiler translates source code into bytecode (before anything runs), the JVM (Java Virtual Machine) runs that bytecode. It may crash.
If you want to get technical, it is your program that crashes. The JVM runs fine -- it catches the exception and produces the error trace that is sent to the terminal.
BogdanMicu BogdanMicu

2021/2/19

#
There isn't an error message thrown. I think it gets stuck in a loop, as it can't be resetted or manipulated in any way until I restart it. This happened once before, when the frames I was using for an animation were different dimensions and were spawning into walls and such. It was the same effect. He gets stuck and I think a modification to the collisionCheck could solve the issue, but to be frank, I have no idea how to fix it.
BogdanMicu BogdanMicu

2021/2/19

#
So, after some different attempts to bug the game, I think I found the cause. The first character moves fine on all platforms, except the vertical ones ascending when he is automatically teleported down. The second one is the one causing the bugs, because of its jump function. His movement is the same as the first character's, but he can use the jump function while in the air as well. The jump function turns the speedY to its maximum value, while the check function turns it to 0 to avoid the collision, so if you hold space it won't be turn to 0 and will get inside of the platform. I think that a function which could enforce the platforms to move the characters in any direction would be the solve to the bug.
BogdanMicu BogdanMicu

2021/2/19

#
Ok, so I solved the ascending platform issue, but the platform moving in opposite direction of character collision resulting in a crash remains unsolved still. I think that that jump function has to be modified somehow, but I'm not sure what the conditions of that disabling should be.
danpost danpost

2021/2/19

#
Needs collision checking in (moving) Platform class. Must keep platform off actor.
You need to login to post a reply.