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

2017/10/28

I am trying to get my Enemy class to explode when hit with a rocket. Would anyone know the best way to do this? Right now all they are doing is disappearing when hit.

pzb0024 pzb0024

2017/10/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Helicoptor
{   private int direction, speed;
    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
    public Rocket(int dir) {
        direction = dir;
        speed = 25;
    }    

    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRotation(direction);
        move(speed);
        if (isTouching(Enemy.class)) {
           removeTouching(Enemy.class);
          }
        timer ++;
        
    }
}   




import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{  
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public int shotDelay = 0;
    public int deSync = Greenfoot.getRandomNumber(200);
    public void act() { 
         if (shotDelay >= 150 && shotDelay > deSync) {
            getWorld().addObject(new Bullet(360), getX(), getY());
            shotDelay = 0;
        }
        shotDelay++;
        move(-1);
        if (getX() == 0) {
            getWorld().removeObject(this);
        }
     }     
}  

danpost danpost

2017/10/28

#
Just create (or get) a class to create objects for the explosions. Then, add one at the enemy's location before removing the enemy from the world.
pzb0024 pzb0024

2017/10/31

#
Awesome, Thanks!
You need to login to post a reply.