So I have coded an animation which animates the players movement left, right and jump. However, I had also planned to make him stand still and look left and right depending on which last key was pressed. But I'm getting no where, sometimes it worked only one way, sometimes it worked but when moving right or left it will also animate the standing image and many times it wouldn't work at all. I will show my jumping code first, because part of it is included in the animation.
Animation code (The left animation code is the same but replaced "Right" with "Left" on image name and X-5)
So I had different ways of animating last image, but like I said many times it wouldn't work properly and would even stop my character from jumping.
The latest way I tried:
This doesn't work, it won't show the Right standing no matter what, and will show Left standing only sometimes when the player is on platform and moved right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | int vSpeed = 0 ; int acceleration = 1 ; public void act() { Move(); Jump(); checkFalling(); checkFloor(); boom(); } public void Jump () { if ((onPlatform() || atWorldFloor()) && "space" .equals(Greenfoot.getKey())) { vSpeed = - 16 ; falling(); } } public void falling() { setLocation (getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void checkFalling() { if (onPlatform() || atWorldFloor() ) { vSpeed = 0 ; } else { falling(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public void right() { if (Greenfoot.isKeyDown( "right" )) { setLocation(getX()+ 5 , getY()); //Moves the actor right if (vSpeed == 0 ) //Checks if actor is jumping { if (imageNum < 4 ) { setImage( "Right " +imageNum+ ".png" ); Greenfoot.delay( 1 ); imageNum ++; } else { imageNum = 1 ; } } else { setImage( "Right jump.png" ); //Else sets right jump image } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void standing() { int sd = 0 ; if ((Greenfoot.getKey() == "right" )) sd++; if ((Greenfoot.getKey() == "left" )) sd--; switch (sd) { case 1 : setImage( "Right Standing.png" ); case - 1 : setImage( "Left standing.png" ); } } |