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

2021/12/3

Method depending on the world youre in

HighIqE HighIqE

2021/12/3

#
I want to code an Explosion on death, which depending on the world youre in restarts it. But an error message always show up marking the "this.getWorld().getClass() == Level3.class" line, when th eplayer dies. Any help?
private int counter = 0;
public void act()
    {
        explode();
        counter = counter + 1;
        if(counter>24 && this.getWorld().getClass() == Level1.class)
        {
            this.getWorld().removeObject(this);
            Greenfoot.setWorld(new Level1());
        }
        if(counter>24 && this.getWorld().getClass() == Level2.class)
        {
            this.getWorld().removeObject(this);
            Greenfoot.setWorld(new Level2());
        }
        if(counter>24 && this.getWorld().getClass() == Level3.class)
        {
            this.getWorld().removeObject(this);
            Greenfoot.setWorld(new Level3());
        }
}
HighIqE HighIqE

2021/12/3

#
public void explode()             //Animation der Explosion
    {
        if (frame == 1)         //wenn frame 1 besteht, dann wird img 1 gezeigt und auf frame 2 gewechselt usw
        {
            setImage(image1);
            Greenfoot.delay(2);
            frame = 2;
        }
        else if (frame == 2)
        {
            setImage(image2);
            Greenfoot.delay(2);
            frame = 3;
        }
        else if (frame == 3)
        {
            setImage(image3);
            Greenfoot.delay(2);
            frame = 4;
        }
        else if (frame == 4)
        {
            setImage(image4);
            Greenfoot.delay(2);
            frame = 5;
        }
        else if (frame == 5)
        {
            setImage(image5);
            Greenfoot.delay(2);
            frame = 6;
        }
        else if (frame == 6)
        {
            setImage(image6);
            Greenfoot.delay(2);
            frame = 7;
        }
        else if (frame == 7)
        {
            setImage(image7);
            Greenfoot.delay(2);
            frame = 8;
        }
        else if (frame == 8)
        {
            setImage(image8);
            Greenfoot.delay(2);
            frame = 9;
        }
        else if (frame == 9)
        {
            setImage(image9);
            Greenfoot.delay(2);
            frame = 10;
        }
        else if (frame == 10)
        {
            setImage(image10);
            Greenfoot.delay(2);
            frame = 11;
        }
        else if (frame == 11)
        {
            setImage(image11);
            Greenfoot.delay(2);
            frame = 12;
        }
        else if (frame == 12)
        {
            setImage(image12);
            Greenfoot.delay(2);
            frame = 13;
        }
        else if (frame == 13){
            setImage(image13);
            getImage().setTransparency(0);
        }
        
    }
private int frame = 1;
Definition of explode if youre wondering
danpost danpost

2021/12/3

#
HighIqE wrote...
an error message always show up marking the "this.getWorld().getClass() == Level3.class" line, when th eplayer dies.
Use:
if (getWorld() instanceof Level3)
HighIqE HighIqE

2021/12/3

#
ty very much man.
You need to login to post a reply.