I’m absolutely newbie. I’m looking the code here and I don’t understand how we set how much asteroids to be created.
local function createAsteroid() local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 ) table.insert( asteroidsTable, newAsteroid ) physics.addBody( newAsteroid, "dynamic", { radius=40, bounce=0.8 } ) newAsteroid.myName = "asteroid" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newAsteroid.x = -60 newAsteroid.y = math.random( 500 ) newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) elseif ( whereFrom == 2 ) then -- From the top newAsteroid.x = math.random( display.contentWidth ) newAsteroid.y = -60 newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) elseif ( whereFrom == 3 ) then -- From the right newAsteroid.x = display.contentWidth + 60 newAsteroid.y = math.random( 500 ) newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) end newAsteroid:applyTorque( math.random( -6,6 ) ) end
I’m trying to write similar function in my project, for creating multiple, static objects from one image file. My code so far.
local ballTable = {} math.randomseed( os.time() ) local Ball = display.newImageRect( "ball.png", 50, 50 ) local function createBall() local newBall = Ball newBall.x = math.random(display.contentWidth) newBall.y = math.random(display.contentHeight) for i = 1, 30 do table.insert(ballTable, newBall) end end
But the result is that only one Ball appears, in the same place after relaunch and a part of it is outside the screen. What I do wrong? How to create many balls and all of them to be in the visible area?