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

2014/2/21

Multiple Actors are Created

sghulam sghulam

2014/2/21

#
Hi, I am new to Greenfoot game development. I have an application with two actors and one of them has the code below. The problem is that the following statement is creating multiple actors instead of one! For each execution of this statement, it produces double the number of actors on the world instead of one: getWorld().addObject (new Invador(),Greenfoot.getRandomNumber (560), Greenfoot.getRandomNumber (60)); public class Invador extends Actor { int counter=1; public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); Greenfoot.playSound("bomb1.wav"); } } public void act() { counter++; if(canSee(Boy.class)) { eat(Boy.class); getWorld().addObject (new Boy(),Greenfoot.getRandomNumber (560), 320); } if(counter % 300==0) { getWorld().addObject (new Invador(),Greenfoot.getRandomNumber (560), Greenfoot.getRandomNumber (60)); counter=1; } setRotation(90); move(1); } } Any help is appreciated. Thanks.
danpost danpost

2014/2/21

#
Move the counter and the code related to it (the incrementing and the 'if' statement and block) to the world class.
You need to login to post a reply.