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

2015/10/3

Can An Actor Use Add Object

Elexios Elexios

2015/10/3

#
first of all im brand new to greenfoot and have only played around for maybe 6-10 hours. what im doing is when the key "o" is pressed this class called snake will spawn an obect from the class venom in a set location.
danpost danpost

2015/10/3

#
You asked if an Actor can use addObject. You really mean one of two things: (1) can an Actor object add a new object of a different type into the world; or, (2) can the addObject method be used in an Actor subclass; Answers: (1) yes, any Actor object of any type, new or otherwise, can be added into the world by any Actor object that is either in the world or that contains, or has access to, a reference to the world the object is to be added into; an Actor object already in the world can use 'getWorld' to get the reference and the line would look like this:
1
getWorld().addObject(< Actor actor >, < int x >, < int y >);
(2) from (1), we see that it is possible; to use a reference, here is code to remove and add back in the actor itself:
1
2
3
World world = getWorld(); // the reference to a world (the one the actor is in, in this case)
world.removeObject(this);
world.addObject(this, 50, 200);
You need to login to post a reply.