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:
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
}
}
}
}

