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

2020/1/31

IllegalStateException

realsaifalrawie realsaifalrawie

2020/1/31

#
hi, can someone help me? i am trying to do a star wars game with an tie fighter. the tie fighter has to shoot the asteroids. but everytime when an asteroid is at edge the IllegalStateException fault shows up. Code of asteroid class:
public class Asteroid extends Actor
{
    int speed;
    public Asteroid(){
        speed = Greenfoot.getRandomNumber(5)+2;
    }

    public void act()
    {
        setLocation(getX()-speed, getY());
        turn(speed);
        if (getX()<=2){
            getWorld().removeObject(this); 
        }

        //if(isTouching(GruenerLaser.class)){
        //getWorld().removeObject(this);
        //}

        if(isTouching(RoterLaser.class)){ // here is always the IllegalStateException problem
            getWorld().removeObject(this);
        }
        else 
        if(isAtEdge()){
            getWorld().removeObject(this);
        }
    }
}
and the code of the tie fighter:
public class Tie extends Actor
{
    private int velocity = 4;
    int delay=50;
    GreenfootSound powerup = new GreenfootSound("powerup.mp3");

    public void act() 
    {
        if(Greenfoot.isKeyDown("w")){
            //setRotation(270);
            move(velocity);
        }
        if(Greenfoot.isKeyDown("a")){
            turn(-velocity);
            //move(2);
        }
        if(Greenfoot.isKeyDown("d")){
            //setRotation(0);
            turn(velocity);
        }

        if(delay==50){

            if(Greenfoot.isKeyDown("space")){
                getWorld().addObject(new RoterLaser(getRotation()), getX(), getY());
                delay=0;
            }

        }
        else{
            delay = delay +1;
        }

        if (isTouching(Asteroid.class)){
            getWorld().addObject(new GameOverScreen(), 450, 300);
            getWorld().addObject(new KleineExplosion(),getX() ,getY() );
            getWorld().removeObject(this);
            Greenfoot.playSound("game over.mp3");

        }

        
    }
}
thanks
realsaifalrawie realsaifalrawie

2020/1/31

#
the problem is always in line 20 of the asteroid code
danpost danpost

2020/1/31

#
realsaifalrawie wrote...
the problem is always in line 20 of the asteroid code
Add the following line after line 13 in Asteroid class:
return;
so the rest of the code in act method is skipped when an asteroid is removed from world.
realsaifalrawie realsaifalrawie

2020/1/31

#
thanks!! youre a genius ;D
You need to login to post a reply.