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

2020/10/24

Adding only one actor

NikoAa NikoAa

2020/10/24

#
I'm making a game where i want to add another actor every time the score is divisible 5. The problem is that if i do not add an extra point every time the condition is met, actors will continue to be added until the condition is no longer met, which results in there being added hundreds of actors. So: How do i add only a single actor when the condition is met instead of hundreds being added? I want to have the following happen without the "counter.add(1);" part.
    public void tryToEat()
    {   if( canSee(Fisk.class))
        {
            eat(Fisk.class);
            counter.add(1);
            createNewFisk();
        }
        if(counter.getValue() % 5 == 0 & counter.getValue() > 0) 
        {
            counter.add(1);
            addFarligFisk();
        }
    }
danpost danpost

2020/10/24

#
NikoAa wrote...
I'm making a game where i want to add another actor every time the score is divisible 5. << Code Omitted >>
Move line 7 to after line 12. This will limit the score divisibility check to only when the score changes.
You need to login to post a reply.