Adding Balls over time

6 views
Skip to first unread message

MarpoRi

unread,
Nov 25, 2011, 6:52:21 PM11/25/11
to ruby4kids
What would be the best way to keep adding balls (difficulty) over time?

MarpoRi

unread,
Nov 27, 2011, 1:23:41 AM11/27/11
to ruby4kids
I am not sure I found a good way (based on time - increasing
difficulty),

if ( rand(1000) == 7 )
@balls.pop = Ball.new
end

This isn't really time based, but it kind of acts like it.

Alberto Morales

unread,
Nov 27, 2011, 1:35:36 AM11/27/11
to ruby...@googlegroups.com
Good try.

The way to do this with time is have an instance variable (starts with @) in the initialize method, then add more balls as the time passes.

The example below adds a ball every 3 seconds for example.

Alberto.
===================

def initialize()
....
@time = Time.now
end

def update
if(Time.now - @time) > 3 #if 3 seconds have passed
@balls << Ball.new
@time = Time.now #reset the time
end
end

Reply all
Reply to author
Forward
0 new messages