Hey, i am making a game, where i need to know if the actor isen't moving.
If i have been moving left, and is no longer moving, it should play the gif: "IdleLeftGif.gif".
If i have been moving right, and is no longer moving, it should play the gif: "IdleRightGif.gif".
I already know how to tell what side it is looking, but i need to know if there is a way to tell if the actor is moving or not.
Thank you in advance :)
Here is the code for my actor:
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Giftest here. * * @author (your name) * @version (a version number or a date) */ public class Giftest extends Actor { /** * Act - do whatever the Giftest wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ GifImage gifImage = new GifImage( "idleLeftGif.gif" ); GifImage2 gifImage2 = new GifImage2( "moveLeftGif.gif" ); GifImage3 gifImage3 = new GifImage3( "moveRightGif.gif" ); GifImage4 gifImage4 = new GifImage4( "idleRightGif.gif" ); GifImage5 gifImage5 = new GifImage5( "attackLeftGif.gif" ); GifImage6 gifImage6 = new GifImage6( "attackRightGif.gif" ); int isLooking = 0 ; public void act() { getImage().scale( 70 , 70 ); moveLeft(); moveRight(); kill(); attackLeft(); } public void moveLeft() { if (Greenfoot.isKeyDown( "left" )) { move(- 2 ); setImage(gifImage2.getCurrentImage()); getImage().scale( 70 , 70 ); isLooking = 0 ; } } public void moveRight() { { if (Greenfoot.isKeyDown( "right" )) { move( 2 ); setImage(gifImage3.getCurrentImage()); getImage().scale( 70 , 70 ); isLooking = 1 ; } } } public void attackLeft() { if (isLooking == 0 ) { if (Greenfoot.isKeyDown( "space" )) { setImage(gifImage6.getCurrentImage()); } } } public void attackRight() { if (isLooking == 1 ) { if (Greenfoot.isKeyDown( "space" )) { setImage(gifImage5.getCurrentImage()); } } } public void kill() { if ( this .isTouching(Enemy. class )) //Hvis bullet kommer i kontakt med Enemy { Actor enemy; enemy = getOneObjectAtOffset( 0 , 0 , Enemy. class ); //Tager Enemys koordinater if (enemy != null ) { World world; world = getWorld(); world.removeObject(enemy); //Fjerner Enemy } } } } |