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

2018/7/1

How to create objects after a certain period of time

grnft grnft

2018/7/1

#
I need to create an object after 40 seconds. In which class do I put the code and how does it look like? In my example, there are some jokers that appear after a certain period of time in my world. Those actors have to act as defined in their class. Thanks in advance!!
Super_Hippo Super_Hippo

2018/7/1

#
The code should be placed in the act method of your world subclass in which it should be added. Create an int variable as a timer initialized as the number of act cycles which should pass before you want to add the object. Reduce it by one each act cycle and when the variable reaches 0, add the object.
grnft grnft

2018/7/1

#
But when I use this code:
public void act() { Timer = Timer + 1; if (Timer <= 60) { addObject(voldemort, 400, 400); } }
(I initialized the "Timer variable" and the "Voldemort variable" before) The object appears at the moment I run the scenario, not "60 seconds" after I pressed run.
danpost danpost

2018/7/1

#
grnft wrote...
I initialized the "Timer variable"
You did not say what you initialized Timer to. Maybe you meant that you declared it. It should be initialized to something like 2400 and it should be decremented (not incremented) and tested for a 0 value in the act method.
grnft grnft

2018/7/1

#
I'm sorry, I am new in this. Could you show me how the code actually looks like?
danpost danpost

2018/7/1

#
grnft wrote...
I'm sorry, I am new in this. Could you show me how the code actually looks like?
There really is nothing new to show. Just adjust your act method and Timer declaration line as indicated in my last post.
grnft grnft

2018/7/2

#
Of course, it was simple. Thanks for your help anyway.
You need to login to post a reply.