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

1 2 3 4 5 6 7 8 9 10 | 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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 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 ); } } } |