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

2014/4/2

Enemy explosions / general explosions

dbz3396 dbz3396

2014/4/2

#
I'm trying to make enemies explode when they are hit by my bullet class, I have it on the player when it is killed, but I use a delay technique to go through the sprites of the animation this is fine for dying but using this for enemies means that the game gets delayed every time I shoot someone, here is the code;
 public void eat()
    {
        Actor food = getOneIntersectingObject(bullet.class);
        if(food !=null)
        {
            setImage("ex1.png");
            Greenfoot.delay(4);
            setImage("ex2.png");
            Greenfoot.delay(4);
            setImage("ex3.png");
            Greenfoot.delay(4);
            setImage("ex4.png");
            Greenfoot.delay(4);
            setImage("ex5.png");
            Greenfoot.delay(4);
            setImage("ex6.png");
            Greenfoot.delay(4);
            setImage("ex7.png");
            Greenfoot.delay(4);
            setImage("ex8.png");
            Greenfoot.delay(4);
            setImage("ex9.png");
            Greenfoot.delay(4);
            setImage("ex10.png");
            Greenfoot.delay(4);
            getWorld().removeObject(food);
            getWorld().removeObject(this);
        } 
         
    }
ex1-10 are the frames for the explosion. Any help would be appreciated such as a way to make the delay only happen to the object instead of the entire game. Thank you in advanced.
lordhershey lordhershey

2014/4/8

#
You have to cycle through the images in different act cycles. have something like this in the enemy act class:
int deathCount = 0;
boolean isDying = false;

 public void eat()
    {
        Actor food = getOneIntersectingObject(bullet.class);
        if(food !=null)
        {
            //Start the actor's dying sequence
            deathCount = 0;
            isDying = true;
            getWorld().removeObject(food);
        } 
         
    }

void act()
{

  if(isDying)
  {
            deathCount++;
            if(1 == deathCount)
              setImage("ex1.png");
            else if(2 == deathCount)
              setImage("ex2.png");
            else if(3 == deathCount)
              setImage("ex3.png");
            else if(4 == deathCount)
              setImage("ex4.png");
            else if(5 == deathCount)
              setImage("ex5.png");
            else if(6 == deathCount)
              setImage("ex6.png");
            else if(7 == deathCount)
              setImage("ex7.png");
            else if(8 == deathCount)
              setImage("ex8.png");
            else if(9 == deathCount)
              setImage("ex9.png");
            else if(10 == deathCount)
              setImage("ex10.png");
            else
              getWorld().removeObject(this);

    return;
  }

  //The rest of your act code here
}
Or something like that.
dbz3396 dbz3396

2014/4/8

#
Thanks I got it sorted :) I ended up with a very similar method essentially the same just swapped out the else if's for timer sets :)
danpost danpost

2014/4/8

#
@dbz3396, you should post what you ended up. It might help others out.
dbz3396 dbz3396

2014/4/9

#
public class Explosion extends Actor
{
    private int timer = 0;
    private int counter=0;
// this is the explosion made when an enemy and a bullet intersect, the animation is played with timers frame by frame
    /**
     * 
     * Act - do whatever the Explosion wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        timer ++;
        if(timer == 1)
        {
            setImage("ex1.png");
        }
        if(timer == 5)//as seen here the frame of the animation changes every few ticks.
        {
            setImage("ex2.png");
        }
        if(timer == 9)
        {
            setImage("ex3.png");
        }
        if(timer == 13)
        {
            setImage("ex4.png"); 
        }
        if(timer == 17)
        {
            setImage("ex5.png"); 
        }
        if(timer == 21)
        {
            setImage("ex6.png");  
        }
        if(timer == 25)
        {
             setImage("ex7.png");
        }
        if(timer == 29)
        {
             setImage("ex8.png");
        }
        if(timer == 33)
        {
             setImage("ex9.png");
        }
        if(timer == 37)
        {
            setImage("ex10.png");
        }
        if(timer == 41)
        {
            getWorld().removeObject(this);
        }

}
}
sorry bout that, here it is for anyone else
danpost danpost

2014/4/9

#
Ok. This would work the same:
public class Explosion extends Actor
{
    private int timer;

    public void act()
    {
        if (timer/4 == 10)
        {
            getWorld().removeObject(this);
            return;
        }
        if (timer%4 == 0) setImage("ex"+(timer/4+1)+".png");
        timer++;
    }
}
You need to login to post a reply.