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

2021/2/18

Gif Animation

Genota Genota

2021/2/18

#
I´ve got a problem. I got a gif to animate my players shooting action. I want, if I press my mouse button, that the whole gif played once. I hope somebody can help/tell me what I have to do. Here is my player code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int vSpeed = 0;
    private int accel = 1;
    private int speed = 5;
    private int jumpHeight= -20;
    private boolean jumping = false;
    private int attacking = -1;
    private int animationSpeed = 2;
    private int shotDelay;
    
    private GreenfootImage run1 = new GreenfootImage("Glumanda1right1.png");
    private GreenfootImage run2 = new GreenfootImage("Glumanda2right1.png");
    private GreenfootImage run3 = new GreenfootImage("Glumanda3right1.png");
    private GreenfootImage run4 = new GreenfootImage("Glumanda1right2.png");
    private GreenfootImage run5 = new GreenfootImage("Glumanda2right2.png");
    private GreenfootImage run6 = new GreenfootImage("Glumanda3right2.png");
    private GreenfootImage run7 = new GreenfootImage("Glumanda1left1.png");
    private GreenfootImage run8 = new GreenfootImage("Glumanda2left1.png");
    private GreenfootImage run9 = new GreenfootImage("Glumanda3left1.png");
    private GreenfootImage run10 = new GreenfootImage("Glumanda1left2.png");
    private GreenfootImage run11 = new GreenfootImage("Glumanda2left2.png");
    private GreenfootImage run12 = new GreenfootImage("Glumanda3left2.png");
    private GreenfootImage run13 = new GreenfootImage("Glumandamouth1.png");
    private GreenfootImage run14 = new GreenfootImage("Glumandamouth2.png");
    private GreenfootImage run15 = new GreenfootImage("Glumandamouth3.png");
    private GreenfootImage run16 = new GreenfootImage("Glumandamouth4.png");
    private int frame = 1;
    private int animationCounter = 0;
    GifImage myGif = new GifImage ("Glgif.gif");
    
    public void act() 
    {
        checkFalling();
        fall();
        jump();
        moveAround();
        animationCounter ++;
        fireProjectile();
    }  
    public void moveAround()
    {
   
      if(Greenfoot.isKeyDown("right"))
      
      {
          setLocation(getX() + speed, getY());
          if(animationCounter % 8 == 0)
          animateRight();
      }
      if(Greenfoot.isKeyDown("left"))
      {
         setLocation(getX() - speed, getY());
         if(animationCounter % 8 == 0)
         animateLeft();
      }
  
    }
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
    }
    public void checkFalling()
    {
        if (!isTouching(Ground.class))
        {
            vSpeed++;
        }
        else 
            vSpeed = 0;
        
    }
    public void jump()
    {
        if(Greenfoot.isKeyDown("space")&& (onGround()==true))
        {
            vSpeed = jumpHeight;
            fall();
        }
    }
    boolean onGround()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
    public void moveRight()
    {
        setLocation (getX()+speed, getY());
    }
    public void animateRight()
    {
        if (frame == 1)
        {
            setImage(run1);
        }
        else if(frame ==2)
        {
            setImage(run2);
        }
        else if(frame ==3)
        {
            setImage(run3);
        }
        else if(frame ==4)
        {
            setImage(run4);
        }
        else if(frame ==5)
        {
            setImage(run5);
        }
        else if(frame ==6)
        {
            setImage(run6);
            frame = 1;
            return;
        }
        
        frame ++;
    }
    public void animateLeft()
    {
        if (frame == 1)
        {
            setImage(run7);
        }
        else if(frame ==2)
        {
            setImage(run8);
        }
        else if(frame ==3)
        {
            setImage(run9);
        }
        else if(frame ==4)
        {
            setImage(run10);
        }
        else if(frame ==5)
        {
            setImage(run11);
        }
        else if(frame ==6)
        {
            setImage(run12);
            frame = 1;
            return;
        }
        
        frame ++;
    }
    public void fireProjectile()
    {
        if (shotDelay > 0) shotDelay--; 
        if (shotDelay == 0 && Greenfoot.mousePressed(null))
        {           
            shotDelay = 30;
            Projectile projectile = new Projectile();
            getWorld().addObject(projectile, getX(), getY());
            projectile.turnTowards(15000,0);
            projectile.move(110.1500);
            setImage( myGif.getCurrentImage());
            
        }
    }
    public void animatemouth()
    {
        if (frame == 1)
        {
            setImage(run13);
        }
        else if(frame ==2)
        {
            setImage(run14);
        }
        else if(frame ==3)
        {
            setImage(run15);
        }
        else if(frame ==4)
        {
            setImage(run16);
            frame = 1;
            return;
        }
        
        frame ++;
    }
}
danpost danpost

2021/2/18

#
The GifImage class is not designed to run an animation one time through and stop.
Genota Genota

2021/2/18

#
Is there any other way/action to implement the feature?
danpost danpost

2021/2/18

#
Genota wrote...
Is there any other way/action to implement the feature?
It would take some doing to implement it with just the GifImage class. An option is to add my Animation class which allows for a single run through the images. You can find it here. Use the GifImage class to decode the images; then get the images to create an Animation object at the time animation is to begin. Use:
// add field
private Animation animation;

// at time animation is to begin
animation = new Animation(this, myGif.getImages());
animation.setCycleActs(30); // adjust as needed

//in act
if (animation != null && animation.run()) animation == null;
You need to login to post a reply.