Auto move towards object

Hi all,

I have been following an YouTube tutorial, and I had a quick question on the code.

Basically, I want to have multiple color balls spawn from random locations outside the screen and move towards the center.  I have a code that works in spawning one ball and moving towards the center, however I do not know how do make it re-spawn continually with the random colored balls (each their own object). Right now the ball is only spawned once, and it is pretty much done after that.

function spawnBall() local ball1 = display.newRoundedRect( 10, 10, 10, 10, 5 ) ball1.x = 0 ball1.y = 0 ball1:setFillColor( 1, 1, 1 ) local ball2 = display.newRoundedRect( 10, 10, 10, 10, 5 ) ball2.x = 0 ball2.y = 0 ball2:setFillColor( 1, 0, 1 ) if math.random(2) == 1 then ball1.x = math.random ( -100, -10 ) else ball1.x = math.random ( display.contentWidth + 10, display.contentWidth + 100 ) ball1.xScale = -1 end ball1.y = math.random ( display.contentHeight ) ball1.trans = transition.to ( ball1, { x=centerX, y=centerY, time=math.random( 2500-speedBump, 4500-speedBump) } ) speedBump = speedBump + 50 end spawnBall()

Also, is there a way to detect when the ball hits an object so that I can trigger different events based off of it?

Thanks :slight_smile:

Check out this code, makes it a little bit easier to read:

----- GAME VARIABLES ----- local AMOUNT\_OF\_BALLS = 20 local MIN\_SPAWN\_TIME = 50 local MAX\_SPAWN\_TIME = 250 local BALL\_SPEED = 1000 local function returnRandomColor() local r = math.random( 1, 255 ) / 255 local g = math.random( 1, 255 ) / 255 local b = math.random( 1, 255 ) / 255 return { r, g, b } end local function createBall() local ball = display.newRoundedRect( 1000, 100, 50, 50, 15) ball:setFillColor( unpack( returnRandomColor() )) return ball end local start\_time = 0 local function start() for i = 1, AMOUNT\_OF\_BALLS, 1 do -- Return random time local time = math.random( MIN\_SPAWN\_TIME, MAX\_SPAWN\_TIME ) start\_time = start\_time + time -- Create ball local ball = createBall() -- Set random Y ball.y = math.random( display.contentHeight ) -- Start moving ball timer.performWithDelay( start\_time, function() transition.to( ball, { time = BALL\_SPEED, x = 0 } ) end ) end end start()

Best regards,

Tomas

** Update: FIXED LINKS BELOW **

I’m sure its just coincidence, but I’ve heard this question using a similar wording many times.  

I answered it and gave code in my (now free) answer pack: 

See the august pack (last question), and/or look at the code here:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015%20Answer%20Packs.zip

http://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2015%20Answer%20Packs/8_August/answers/homework

(At the time I answered it I heard this question 3 times in a week which made me think it was a homework question for a class.  Now I think maybe people are seeing a game they like and want to reproduce.)

Check out this code, makes it a little bit easier to read:

----- GAME VARIABLES ----- local AMOUNT\_OF\_BALLS = 20 local MIN\_SPAWN\_TIME = 50 local MAX\_SPAWN\_TIME = 250 local BALL\_SPEED = 1000 local function returnRandomColor() local r = math.random( 1, 255 ) / 255 local g = math.random( 1, 255 ) / 255 local b = math.random( 1, 255 ) / 255 return { r, g, b } end local function createBall() local ball = display.newRoundedRect( 1000, 100, 50, 50, 15) ball:setFillColor( unpack( returnRandomColor() )) return ball end local start\_time = 0 local function start() for i = 1, AMOUNT\_OF\_BALLS, 1 do -- Return random time local time = math.random( MIN\_SPAWN\_TIME, MAX\_SPAWN\_TIME ) start\_time = start\_time + time -- Create ball local ball = createBall() -- Set random Y ball.y = math.random( display.contentHeight ) -- Start moving ball timer.performWithDelay( start\_time, function() transition.to( ball, { time = BALL\_SPEED, x = 0 } ) end ) end end start()

Best regards,

Tomas

** Update: FIXED LINKS BELOW **

I’m sure its just coincidence, but I’ve heard this question using a similar wording many times.  

I answered it and gave code in my (now free) answer pack: 

See the august pack (last question), and/or look at the code here:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015%20Answer%20Packs.zip

http://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2015%20Answer%20Packs/8_August/answers/homework

(At the time I answered it I heard this question 3 times in a week which made me think it was a homework question for a class.  Now I think maybe people are seeing a game they like and want to reproduce.)