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

2015/1/29

Whats Wrong?

Pr0grammer Pr0grammer

2015/1/29

#
if(amRandDerWelt()||kollidiertMit(Asteroid.class)){
            getWorld().removeObject(this,Asteroid.class);
            
            
        
    
        }

public boolean amRandDerWelt(){
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight()- 20)
            return true;
        else
            return false;
    }
    
     public boolean kollidiertMit(Class c){
        Actor a = getOneObjectAtOffset(0, 0, c);
        return a != null;        
    }
i need too remove my Shot AND my Asteroid at the same time
danpost danpost

2015/1/30

#
The main thing that is wrong is that you cannot pass more than one actor to the 'removeObject' method (see line 2). The only thing you want to do if either condition in line 1 is true is remove the shot. This means that if colliding with an asteroid, the asteroid should be removed within the method that checks for its intersection::
if (amRandDelWelt() || kollidierMitAsteroid())
{
    getWorld().removeObject(this);
}


public boolean kollidierMitAsteroid()
{
    Actor asteroid = getOneIntersectingObject(Asteroid.class);
    if (asteroid != null) getWorld().removeObject(asteroid);
    return asteroid != null;
}
You need to login to post a reply.