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:
and the whole spaceship code:
Thanks :)
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(); } }
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 ); } } }