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

2013/12/13

Can anyone give me a quick random spawing code?

tomw tomw

2013/12/13

#
I want a code were my actor can randomly spawn (My actor is called seahorse1) can anyone give me a random spawing code? Help would be very helpful.
Zamoht Zamoht

2013/12/13

#
addObject(player, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
tomw tomw

2013/12/13

#
Thanks but it says cannot find symbol variable player Do you know whats wrong?
danpost danpost

2013/12/13

#
Replace 'player' with 'seahorse1' ( or 'new Seahorse1()' )
tomw tomw

2013/12/13

#
Thanks now that bits working but now the get width part is saying the same thing can you help me again please?
danpost danpost

2013/12/13

#
If you are coding this in an Actor subclass, you cannot directly use 'getWidth' (or 'getHeight'). You need to execute those methods on a World object. The Actor class API has a method that returns the world it is in. To elaborate, all non-static methods are executed on an object. If the object is not explicitly given, it is executed on the object that the 'this' keyword would refer to. In a World subclass, 'this' refers to a specific world of that class the method resides in; in anActor subclass, 'this' refers to a specific actor of that class the method resides in.
You need to login to post a reply.