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

2020/3/24

Help: it doesn't spawn an enemy when my score = 4

Inothar Inothar

2020/3/24

#
that's the code in my world "playground"
Counter counter = new Counter();
private void prepare(){
int playerScore =  counter.getScore();
        if (playerScore == 4)
        {
            Enemies enemies7 = new Enemies();
            addObject(enemies7,Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
        }

}
that's the code in my actor class names "Counter"
int score = 0;
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.WHITE,null));
    }    

    public void addScore()
    {
        score++;
    }

    public int getScore()
    {
        return score;
    }
i don't understand why it doesn't spawn anthing.
RcCookie RcCookie

2020/3/24

#
If the prepare method is called like usually (only in the constructor), it won´t be executed eact frame but only once at the beginning. At that point the score is obviously still zero. You may want to put the dode from prepare into the act method of playground or, if there isn´t anything else in prepare, just call the prepare method by the act method.
Inothar Inothar

2020/3/24

#
It works thank you, but now i have another problem. Once i reached score = 4, it keeps spawning an infinite amount of enemies until I reached score = 5. What should I write to spawn only 1 enemy?
danpost danpost

2020/3/24

#
Show act method.
Inothar Inothar

2020/3/24

#
I found it by myself, thank you so much <3
You need to login to post a reply.