So I have a run animation that I'd like to be able to mirror depending on which direction the player is moving within Greenfoot, rather than creating separate mirrored images for moving left and right. Currently, the way I'm using mirrorHorizontally() is constantly flipping the image between left and right with every run of the act method. What am I doing wrong?
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | public void act() { if (Greenfoot.isKeyDown( "left" )){ facingRight = false ; checkCollision(); if (!isCollidingWall) setLocation(getX() - speed, getY()); isMoving = true ; } else if (Greenfoot.isKeyDown( "right" )){ facingRight = true ; checkCollision(); if (!isCollidingWall) setLocation(getX() + speed, getY()); isMoving = true ; } if (canMove && Greenfoot.isKeyDown( "up" ) && !isJumping){ velocityY = 14 .0f; isJumping = true ; } if (!Greenfoot.isKeyDown( "left" ) && !Greenfoot.isKeyDown( "right" )){ animationTimer = 0 ; isMoving = false ; } gravity(); checkCollision(); //image changing code if (isMoving && !isJumping){ if (animationTimer == 0 ){ setImage(playerRunningRight1); } else if (animationTimer == 6 ){ setImage(playerRunningRight2); } else if (animationTimer == 12 ){ setImage(playerRunningRight3); } else if (animationTimer == 18 ){ setImage(playerRunningRight2); } else if (animationTimer == 24 ){ animationTimer = - 1 ; } animationTimer++; } else if (!isJumping) setImage(playerStandingRight); else if (isJumping) setImage(playerJumpingRight); if (!facingRight){ getImage().mirrorHorizontally(); } } |