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

2018/3/31

Need help with removing objects when method is called

Recorsi Recorsi

2018/3/31

#
Hello, I know this questions is weird, let me explain: I tried to implement missiles into my game, and they work, but you can shoot them indefinitely. Also I made 3 objects which are used as ammo indicators(remove one per missile, cant shoot if 0 are left) How do I implement that? Code for missiles(i know its not the best way to do it but im a noob at programming):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * A Missile you can shoot entities with.
 * 
 * @author Julian 
 * @version 30.03.18
 */
public class Missile extends Actor
{
    private boolean missileLaunched = false;
    int missileFlight = 50;
    GreenfootSound MissileImpactSound = new GreenfootSound("MissileImpact.mp3");
    MissileExplosion missileexplosion = new MissileExplosion();
    boolean MissileTriggered = false; 
    int ExTime = 40;
    int playTime = 1*60;
    private static boolean playingExSound = false;
    public Missile()
    {
        GreenfootImage image = getImage();  
        image.scale(40,80);
        setImage(image);
    }
    public void act() 
    {
        move(7);
        explodeMissile();
        if (getWorld() == null) return;
        hitsEdge();
    } 
    private void hitsEdge()
    {
        if (isAtEdge())
        {
            missileFlight = 50;
            getWorld().removeObject(this);
        }
    }
    public void explodeMissile()
    {   
       missileFlight--;
       if (missileFlight <= 0 & Spaceship.missileLaunched)
       {
           MissileImpactSound.play();
           missileFlight = 50;
           MissileTriggered = true;
           Spaceship.missileLaunched = false;
           getWorld().addObject(missileexplosion, getX()+1, getY()+1);
           setLocation(-900, -900);
           playingExSound = true;
           if (playingExSound)
           {
               playTime--;
           }
           if (playTime <= 0)
           {
               MissileImpactSound.stop();
               playingExSound = false;
               getWorld().removeObject(this);
           }
       }
    }
 
"ammo" code:
    MissileLoad missileload1 = new MissileLoad();
    MissileLoad missileload2 = new MissileLoad();
    MissileLoad missileload3 = new MissileLoad();
public void addMissileLoad()
    {
        addObject(missileload1, 680, 30);
        addObject(missileload2, 730, 30);
        addObject(missileload3, 780, 30);
    }
Thanks. EDIT: Forgot the launch code
public void launchMissile()
  {
      missileTime++;
       if (missileTime > missileShootCooldown)
       {
         if (Greenfoot.isKeyDown("r"))
         {
             getWorld().addObject(missile, getX(), getY());
             missile.setRotation(getRotation()-180);
             MissileLaunchSound.play();
             missileLaunched = true;
             missileTime = 0;
         }
      }
  }
danpost danpost

2018/3/31

#
I am presuming you wish to remove a MissileLoad object each time the launchMissile method is called. What have you tried?
Recorsi Recorsi

2018/4/1

#
Well i got it to work now, but the way it did it isnt very good. I just copied the same code 3 times and added booleans. I can't think of a better way to do it. Here is the code:
    //World class
    public static boolean firstShot = true;
    public static boolean secondShot = false;
    public static boolean thirdShot = false;
//Actor class
public void launchMissile()
  {
    Spielfeld spielfeld = (Spielfeld)getWorld();
    if (spielfeld.firstShot)
    {
      missileTime++;
       if (missileTime > missileShootCooldown)
       {
         if (Greenfoot.isKeyDown("r"))
         {
             getWorld().addObject(missile, getX(), getY());
             missile.setRotation(getRotation()-180);
             MissileLaunchSound.play();
             missileLaunched = true;
             missileTime = 0;
             getWorld().removeObject(spielfeld.missileload3);
             spielfeld.firstShot = false;
             spielfeld.secondShot = true;
         }
      }
    }
    if (spielfeld.secondShot)
    {
      missileTime++;
       if (missileTime > missileShootCooldown)
       {
         if (Greenfoot.isKeyDown("r"))
         {
             getWorld().addObject(missile, getX(), getY());
             missile.setRotation(getRotation()-180);
             MissileLaunchSound.play();
             missileLaunched = true;
             missileTime = 0;
             getWorld().removeObject(spielfeld.missileload2);
             spielfeld.secondShot = false;
             spielfeld.thirdShot = true;
         }
      }
    }
    if (spielfeld.thirdShot)
    {
      missileTime++;
       if (missileTime > missileShootCooldown)
       {
         if (Greenfoot.isKeyDown("r"))
         {
             getWorld().addObject(missile, getX(), getY());
             missile.setRotation(getRotation()-180);
             MissileLaunchSound.play();
             missileLaunched = true;
             missileTime = 0;
             getWorld().removeObject(spielfeld.missileload1);
             spielfeld.thirdShot = false;
         }
      } 
    }
  }
I know its stupid :D
Vercility Vercility

2018/4/1

#
Could've used an int to count the amount of missiles and then use a method that launches either of those depending on int value
Recorsi Recorsi

2018/4/1

#
Vercility wrote...
Could've used an int to count the amount of missiles and then use a method that launches either of those depending on int value
Hm you're right, maybe i will redo it some time.
You need to login to post a reply.