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

2014/2/26

Creating objects

sghulam sghulam

2014/2/26

#
Hi, If the act method has just one simple statement to create an object, like the following and you click on the button Act, I would think that it creates one object at the specified coordinates: public void act() { getWorld().addObject (new Baloon(), Greenfoot.getRandomNumber (300), Greenfoot.getRandomNumber (300)); } What is happening is the following: If the world has no objects, nothing happens when you click the Act button. If the world has one object then it creates one more object with the first click and two objects with the second click, 4 objects with the third click and so on. i.e. doubling the number of objects with each click. I need to write code to create one object only. How can I do that? Many thanks.
danpost danpost

2014/2/26

#
sghulam wrote...
If the world has no objects, nothing happens when you click the Act button.
Correct. If the world has no objects (actors), then the act methods of those actors are not executed. An actor must be in the active world for the act method to be executed.
If the world has one object then it creates one more object with the first click and two objects with the second click, 4 objects with the third click and so on. i.e. doubling the number of objects with each click.
Each act cycle, you are doubling the number of actors in the world. Each act of each actor in the active world is executed each cycle. So each actor will produce a double on each click. Referring back to the first part, if no actors are in the world, zero doubled is zero.
I need to write code to create one object only. How can I do that?
Move the spawning code to the world class constructor. Download and review the editing panes of my Overview: subclassing in greenfoot scenario.
sghulam sghulam

2014/2/27

#
Thank you danpost. The constructor of the world is called once. I am not sure how to create the objects dynamically based on certain events on the world. The code seems more logical to be placed within the actor's code and I don't know how to move it to the world. Kindly show me an example of the code in the world. Best regards.
danpost danpost

2014/2/28

#
sghulam wrote...
I am not sure how to create the objects dynamically based on certain events on the world.
You were not showing any conditions on spawning the object while the code was in an actor class ('based on certain events'). The code will probably need to be moved to an act method (but still, probably not in an actor class). The world act method will be probably where it will go. However, the conditions or events that spawning is based on will need to be incorporated:
1
2
3
4
5
// in world class
public void act()
{
    if (/* whatever conditions are required */) addObject(...
}
danpost danpost

2014/2/28

#
From your new discussion thread, I can see why you wanted to put it in the actor class. Move it back and add 'getWorld().' before 'addObject(...'. It seems you were just missing the conditional part (the 'if' clause).
sghulam sghulam

2014/3/1

#
Dear danpost, Thank you very much. I am aware that I need to use an if statement but when I posted the code fragment, I removed the if part. I simply want to create an abject if, let's say there are no actors in the world, like using the following if statement in the act method of the actor: if(getWorld().numberOfObjects()==0) { getWorld().addObject(new Worm(), Greenfoot.getRandomNumber (300), Greenfoot.getRandomNumber (300)); } This is not working. Moving it to the act method inside the world class did not change anything. Appreciate your help.
danpost danpost

2014/3/1

#
Do not remove things when you post code. It leads other to believe that you are further from the solution than you really are and misleads them in other ways also. Again, you cannot place the spawning code in the Worm class to produce worms. If no worms are in the world, the code will never be executed (the act method of an object is only executed if the object is in the active world). The following will add a Worm object into the world if no worm objects are in the world. This would be in your world subclass:
1
2
3
4
5
public void act()
{
    // any code you may already have in the world act method
    if (getObjects(Worm.class).isEmpty()) addObject(new Worm(), Greenfoot.getRandomNumber(300), Greenfoot.getRandomNumber(300));
}
If you really mean 'no actors in the world', which I would understand to mean no actors of any kind, then change 'Worm.class' back to 'null' (but, I get the feeling this is why you are not getting the results you expect).
danpost danpost

2014/3/1

#
If you still do not get the results you want, you will have to post some of your class codes so we can see what is actually going on. Particularly, your world subclass and the Worm class. When posting code, use the 'code' tag below the 'Post a reply' input box to insert your code in.
You need to login to post a reply.