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

2020/3/4

How do I respawn things

Cris______ Cris______

2020/3/4

#
I have an actor class named balloons and I am trying to make it to when it gets deleted form the world it'll respawn this is my run method where the balloons initially get spawned
public void run(){
        int y = 0;
        balloons = Greenfoot.getRandomNumber(5);
        lives = "Lives: "+liv;
        scoreText = "Score: "+score;
        addObject(new Cannon(),100,720);
        for(int i =0;i<=balloons;i++){
            y = Greenfoot.getRandomNumber(400)+300;
            addObject(new Balloon(randomClr(),"a"),1400,y);
        }
    }
This code is what deletes the balloons
if(isAtEdge(getX())==true){
            MyWorld.liv--;
            MyWorld.updateLives();
            MyWorld.balloons = 0;
            getWorld().showText(MyWorld.lives,100,50);
            getWorld().removeObjects(getWorld().getObjects(Balloon.class));
         }
        if(Greenfoot.mouseClicked(this)){
            MyWorld.score++;
            MyWorld.updateScore();
            getWorld().showText(MyWorld.scoreText,1300,50);
            getWorld().removeObjects(getWorld().getObjects(Balloon.class));
        }
danpost danpost

2020/3/4

#
First, line 1 in the deleting code uses an "isAtEdge(int)" call. What is that? Second, it appears you are using static values in your MyWorld class that should probably not be static. Third, initializing the world values should be done in the world constructor. Once moved, you can use the run method to respawn balloons. Forth, line 3 of the run method may generate a random value for zero balloons, which would basically leave the scenario in limbo.
You need to login to post a reply.