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

2014/10/22

Error when Bullet hits Asteroid.

CKnox CKnox

2014/10/22

#
I have a problem in my space shooter game where when the Bullet hits the Asteroid the Asteroid is supposed dissapear and debris is supposed to appear. However when a Bullet hits the Asteroid, the Asteroid instead moves backwards a bit and then the entire game slows down and just stops. No errors come up but its acting as though the RAM was so full it just slowed down to nothing. This is the code in my Asteroid class.
public class Asteroid extends Actor
{
    int margin = getImage().getWidth()/2 - 8;
    private int size;
    /**
     * Act - do whatever the Asteroid wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-7);
//         if (checkEdge() == true){
//             getWorld().removeObject(this);
//         }
        checkHit();
    }  
    public boolean checkEdge()
    {
         if (getX() - margin < 0) {
            return true;
         }
         return false;
    }
    public void checkHit()
    {
        Bullet b =  getOneIntersectingObject(Bullet.class);
        if(b != null) {    
            
            Asteroid a1 = new Asteroid();
            Asteroid a2 = new Asteroid();
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move(11);
            a2.move(11);
            getWorld().removeObject(this);
        }
    }
}
//     public boolean checkEdge()
//     {
//          if (getX() - margin < 0) {
//             return true;
//          }
//          return false;
//     }
danpost danpost

2014/10/22

#
I do not see where the bullet is removed when an asteroid is hit. If the bullet remains, then the new asteroids will detect the bullet and split and then those will, etc. This will cause an over-abundance of asteroids which will overwork the system and slow it down.
CKnox CKnox

2014/10/23

#
Thankyou you for pointing that out, I checked my Bullet code and there is no removal code there either. Thankyou
You need to login to post a reply.