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

2017/1/25

a loop

biem biem

2017/1/25

#
Please help me in the following problem: I want to create objects that appear one after another, while running the game, for a given number. I tried the code: getWorld().addObject(new Mar(),80,10);
danpost danpost

2017/1/25

#
What kind of objects? How do you mean by 'one after another' (in what context)? Obviously, the code you had shown will create the same type object at the same location each time the line is executed (from an Actor subclass).
biem biem

2017/1/25

#
Please help me in the following problem: I want to create objects that appear one after another, while running the game, for a given number. I tried the code: getWorld().addObject(new Mar(),100,100)); but my schedule does not recognize metod getWorld(). I tried sequence: int i=0; while(i<3) { addObject(new Mar(),160,10); addObject(new Mar(),240,10); addObject(new Mar(),320,10); addObject(new Mar(),400,10); addObject(new Mar(),480,10); i++; } but it has run only once, not repeated. This code is from subclass MyWorld. Thank you
danpost danpost

2017/1/25

#
biem wrote...
This code is from subclass MyWorld.
The 'getWorld' method is an Actor class method and can only be used on an Actor object (usually within an Actor subclass). The 'addObject' method is a World class method and can only be used on an World object (usually within a World subclass). When these methods are executed from the class that the method belongs to, you usually do not need to specify the object you are executing them on -- although, you could, using the 'this' keyword:
this.addObject(nw Mar(), 160, 10);
The 'this', when in a World subclass, refers to the World object that the code is executing on. Enough on trying to explain that. You will find that the code you gave in your last post actually does place 3 Mar objects at 5 different locations in your world (drag them while the scenario is paused).
biem biem

2017/1/25

#
Thank you
You need to login to post a reply.