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

2021/2/22

Enemy Animation in both Directions

Genota Genota

2021/2/22

#
I am trying to animate, that if my enemy is moving to the right it plays the first gif and if he walks to the left an other gif... Here is my Enemy Code (all you need I think):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


public class Enemy1 extends Enemy
{
    private int speed = -3;
    private int count = 0;
    private int health = 1;
    private int vSpeed = 0;
    private int accel = 1;
    boolean hitByProjectile = false;
    private int animationCounter = 0;
    private int frame = 1;
    private GreenfootImage run1 = new GreenfootImage("Enemy1left.png");
    private GreenfootImage run2 = new GreenfootImage("Enemy2left.png");
    private GreenfootImage run3 = new GreenfootImage("Enemy3left.png");
    GifImage myGif = new GifImage ("Enemy1_gif.gif");
    GifImage myGif2 = new GifImage ("Enemy1_gif2.gif");
    /**
     * Act - do whatever the Enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        count++;
        moveAround();
        hitByProjectile();
        animationCounter ++;
    } 
    public void moveAround()
    {
        if(count < 120)
    {
        setLocation(getX() + speed,getY());
    }
        else
        {
         speed = - speed; 
         count = 0;
        }
    }
danpost danpost

4 days ago

#
Genota wrote...
if my enemy is moving to the right it plays the first gif and if he walks to the left an other gif
Rephrased: "If speed is positive, use right gif, else use left." Pretty much says it all.
Genota Genota

4 days ago

#
I tried that already before I started this thread. Like this:
    GifImage myGif = new GifImage ("Enemy1_gif.gif");
    GifImage myGif2 = new GifImage ("Enemy1_gif2.gif");
    /**
     * Act - do whatever the Enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        count++;
        moveAround();
        hitByProjectile();
        animationCounter ++;
    } 
    public void moveAround()
    {
        if(count < 120)
    {
        setLocation(getX() + speed,getY());
        setImage(myGif.getCurrentImage());
    }
        else
        {
         speed = - speed; 
         count = 0;
         setImage(myGif2.getCurrentImage());
        }
    }
danpost danpost

4 days ago

#
Genota wrote...
I tried that already before I started this thread. Like this: << Code Omitted >>
Your if-else conditions are not determining anything about whether speed is positive or negative. It only determines whether the actor is changing directions or not (regardless of direction).
Genota Genota

4 days ago

#
I edited my code like that now:
    public void moveAround()
    {
        if(count < 120)
        {
        setLocation(getX() + speed,getY());
        setImage(myGif.getCurrentImage());
        direction =1;
        }
        else if(count > 120)
        { 
         direction =-1;
         setLocation(getX() - speed,getY());
         setImage(myGif2.getCurrentImage());
        }
    }
He goes at first to the left and play the first gif... then he turn around and play gif2 (Thats what I want) BUT he doesnt turn back again...
danpost danpost

4 days ago

#
The direction field is not required and only complicates things. Just negate the speed value to turn around. You will only need one setLocaton line that way, also.
public void moveAround()
{
    setLocation(getX()+speed, getY()); // move
    count = (count+1)%120; // step count
    if (count == 0) speed = -speed; // turn around
    setImage((speed>0 ? myGif2 : myGif).getCurrentImage()); // image
}
Remove count++; from act.
You need to login to post a reply.