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

2017/10/7

Play GIF once?

Recorsi Recorsi

2017/10/7

#
Hi, i made a scenario where you control a spaceship that has to destroy asterioids and if you hit one you explode. and if you explode a gif ( ) starts playing at that position. but my problem is that the gif loops infinite but i only want it to play once or for a certain amount of seconds. how can i do that? The code for the explosion:
   public void TriggerEx()
{
    Rock rock = (Rock)getOneIntersectingObject(Rock.class);;
    if (rock != null && getNeighbours(getImage().getWidth()*8/10, false, Rock.class).contains(rock) && rock.hitsShip(this))
    {
        getWorld().addObject(explosion, getX()+1, getY()+1);
        //Ton6.play();
        explode2();  
    }
}
and the whole spaceship code:
import greenfoot.*;  

/**
 * The main actor.
 * 
 * @author (your name) 
 * @version 23.09
 */
public class Spaceship extends Actor
{
   private static final int NUM_FRAGMENTS = 20;
   GifImage myGif = new GifImage("spaceship.gif");
   private int dx;
   private int dy;
   Laser laser = new Laser();
   GreenfootSound Ton1 = new GreenfootSound("Laser.wav");
   private int horizontalSpeed = 5;
   private int verticalSpeed = 5;
   explosion explosion = new explosion();
   GreenfootSound Ton6 = new GreenfootSound("Explosion.mp3");
   private double v = 0;
   private int speed = 0;
   private static final int MAX_SPEED = 15;
   private int delay;
   private static final int MAX_DELAY = 20;
   GreenfootSound Ton = new GreenfootSound("Engine.mp3");
   public Spaceship()
   {
      GreenfootImage image = getImage();  
      image.scale(75, 52);
      setImage(image);
   }    
   public void act() 
   {
      Bewegen();
      pruefeKontaktRand();
      shoot();
      GIF();
      TriggerEx();
   }
   public void TriggerEx()
{
    Rock rock = (Rock)getOneIntersectingObject(Rock.class);;
    if (rock != null && getNeighbours(getImage().getWidth()*8/10, false, Rock.class).contains(rock) && rock.hitsShip(this))
    {
        getWorld().addObject(explosion, getX()+1, getY()+1);
        //Ton6.play();
        explode2();  
    }
}
   private void GIF()
   {
       setImage( myGif.getCurrentImage() );
       GreenfootImage image = getImage();
       image.scale(75, 52);
       setImage(image);
    }
   private void Bewegen()
   {
    

      if (Greenfoot.isKeyDown("a"))
       {
            turn(-4);
       } 
      if (Greenfoot.isKeyDown("d"))
       {
            turn(4);
       }
      if (Greenfoot.isKeyDown("w"))
       {
           
           speed++;
           if (speed > MAX_SPEED) speed = MAX_SPEED;
       } else if (speed > 0) speed--; move(-speed/3); 
    if (Greenfoot.isKeyDown("w"))
    {
        Ton.playLoop();
        Ton.setVolume(100);
    } else 
    {
        Ton.pause();
    }
      
      
      /*if (Greenfoot.isKeyDown ("w") && Greenfoot.isKeyDown("a"))
      {
          setRotation(310);
          
        }
      if (Greenfoot.isKeyDown ("w") && Greenfoot.isKeyDown("d"))
      {
          setRotation(50);
         
        } 
      if (Greenfoot.isKeyDown ("s") && Greenfoot.isKeyDown("a"))
      {
          setRotation(225);
         
        }
      if (Greenfoot.isKeyDown ("s") && Greenfoot.isKeyDown("d"))
      {
          setRotation(140);
          
        }  
  */ }
  private void pruefeKontaktRand()
  {
    if (getX() >=626) {
        dx = -dx;
        }  
    if (getX() <=14) {
        dx = -dx;
        }
    if (getY() >=466) {
        dy = -dy;
        }
    if (getY() <=14) {
        dy = -dy;
        }
}
/*private void shoot()
{
    if(Greenfoot.isKeyDown("space"))
    {
       World Spielfeld = getWorld();
       Spielfeld.addObject(laser, 0, 0);
       laser.setLocation(getX(), getY());
       laser.setRotation(getRotation()-90);
       Ton1.play();
       Ton1.setVolume(75);
    }
}*/
private void shoot()
 {
  if("space".equals(Greenfoot.getKey())){
    Laser laser = new Laser();
    getWorld().addObject(laser, getX(), getY());
    laser.setRotation(getRotation()-180);
    laser.move(40);
    Ton1.play();
  }
 }
public void explode2()
 {
   placeDestroyed (getX(), getY(), NUM_FRAGMENTS);
   getWorld().removeObject(this);
   Ton.stop();
 }
private void placeDestroyed(int x, int y, int numFragments)
{
   for (int i=0; i < numFragments; i++) {
       getWorld().addObject(new Destroyed(), x ,y );
    }
 }
}
Thanks :)
danpost danpost

2017/10/7

#
Why do you supply the Spaceship class code when it is the images of the explosion that is giving problems? Show the proper class code (please).
Recorsi Recorsi

2017/10/13

#
the explosion class is almost empty, i "spawn" the gif in the "triggerex" method. the explosion class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class explosion here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class explosion extends Actor
{
    GifImage explosion = new GifImage("explosion.gif");
    public void act() 
    {
       setImage(explosion.getCurrentImage());
       GreenfootImage image = getImage();
       image.scale(75, 75);
       setImage(image); 
    }    
}
danpost danpost

2017/10/13

#
The only thing you have to go on are the images that the GifImage object uses. That is, the only method that is available in the GifImage class that gives you any information about the animation is the 'getImages' method that returns a List object containing the images it uses. Fortunately, that is enough as you just need to be able to check when the first image is used and stop the animation when it is used the second time. Okay, to do that, you will need to retain a reference to the first image and a booleen that tracks when it is not being used (set to false when the first image is being used, otherwise set to true). A comparison is made every act to see if the first image is not that which is set the actor; that result is compared to the tracking field. If they are not the same, then a change was made either removing or setting the first image. In other words, the value of the boolean needs changed at that time. After changing its value, it can be checked to see if it was to remove or set the image. If the new value is false, then the image was set, so it is time to remove the explosion object from the world:
import greenfoot.*;
 
public class explosion extends Actor
{
    GifImage explosion = new GifImage("explosion.gif");
    GreenfootImage imageOne;
    boolean notImageOne;
    
    public explosion()
    {
        List<GreenfootImage> images = explosion.getImages(); // get list of images
        imageOne = images.get(0); // get reference to first image
        for (GreenfootImage img : images) img.scale(75, 75); // scale all images
        setImage(explosion.getCurrentImage()); // set initial image
    }

    public void act() 
    {
        setImage(explosion.getCurrentImage()); // animate
        if ((getImage() != imageOne) != notImageOne) // was there a change involving first image
        {
            notImageOne = ! notImageOne; // record change
            if (! notImageOne) getWorld().removeObject(this); // if setting first image, remove explosion from world
        }
    }
}
You need to login to post a reply.