Sorry if my title is vague, I’m not sure how else to put this. I have this concept I want to create, but I’m not sure how I could implement it. So on my screen I want to have blocks run down the screen each generating at a random position that descend until it goes off screen. Here is an example of what I want to have done:
I don’t think you need physics for that, but you can. Take a look at the crates example in the physics directory for help on physics and random generation.
here is a non physics example but bear in mind, you cant maniuplate each box individualy, to do that that i would insert it into a table
display.setStatusBar( display.HiddenStatusBar ) ----------------------------------------------------------------------------------------- -- Localize to improve performance ----------------------------------------------------------------------------------------- local W = display.contentWidth local H = display.contentHeight local XCentre = display.contentCenterX local YCentre = display.contentCenterY local screenLeft = display.screenOriginX local screenRight = display.viewableContentWidth + display.screenOriginX local screenTop = display.screenOriginY local screenBottom = display.viewableContentHeight + display.screenOriginY local random = math.random ---------------------------------------------------------------------------------------- -- a simple function to destroy the boxes ---------------------------------------------------------------------------------------- local function Destroy\_Box (obj) print("obj remove") display.remove( obj ) obj = nil end ---------------------------------------------------------------------------------------- -- Display, add Physics and Move the Boxes! ---------------------------------------------------------------------------------------- local function SpawnBox () local BadGuy = display.newRect(random(screenLeft,screenRight), 0, 40,40 ) transition.to( BadGuy,{time = random (1000,3000), y = screenBottom + 20, onComplete = Destroy\_Box} ) return BadGuy end timer.performWithDelay( random (120,500), SpawnBox , -1)
Thank you so much! although I’m not quite sure how i would insert this into a table. Lets say i wanted to click on the rectangle to perform a certain task. how would i go about doing that?
oh ok that makes life a little more difficult, i assumed you were making a background, in that case i would create a finite no. of boxes and then you can individually manipulate them.
----------------------------------------------------------------------------------------- -- Localize to improve performance ----------------------------------------------------------------------------------------- local W = display.contentWidth local H = display.contentHeight local XCentre = display.contentCenterX local YCentre = display.contentCenterY local screenLeft = display.screenOriginX local screenRight = display.viewableContentWidth + display.screenOriginX local screenTop = display.screenOriginY local screenBottom = display.viewableContentHeight + display.screenOriginY local random = math.random ---------------------------------------------------------------------------------------- -- This is a forward reference to a funciton i call later on ---------------------------------------------------------------------------------------- local Fall ---------------------------------------------------------------------------------------- -- Resets the postion of the boxes ---------------------------------------------------------------------------------------- local function Restart\_Box (obj) print("obj restarted") obj.x = random(screenLeft,screenRight) obj.y = -20 local FallerCaller = Fall (obj) end ---------------------------------------------------------------------------------------- -- Makes the boxes fall ---------------------------------------------------------------------------------------- function Fall (obj) transition.to( obj ,{time = random (3000,9000), delay = random (100,900), y = screenBottom + 20, onComplete = Restart\_Box} ) end local function onBoxTouch (event) local target = event.target target:setFillColor( 0,1,1 ) print("lol") end ---------------------------------------------------------------------------------------- -- A table to hold our boxes ---------------------------------------------------------------------------------------- local Boxes = {} for i = 1,10 do Boxes[i] = display.newRect(random(screenLeft,screenRight), -20, 40,40 ) Boxes[i]:addEventListener( "touch", onBoxTouch ) local Faller = Fall (Boxes[i]) end
btw if you wanted them to fall at the same speed, just change the time of the transition and if you dont want them to reset then you can remove the OnComplete phrase from the transition. ! Hope i helped
I don’t think you need physics for that, but you can. Take a look at the crates example in the physics directory for help on physics and random generation.
here is a non physics example but bear in mind, you cant maniuplate each box individualy, to do that that i would insert it into a table
display.setStatusBar( display.HiddenStatusBar ) ----------------------------------------------------------------------------------------- -- Localize to improve performance ----------------------------------------------------------------------------------------- local W = display.contentWidth local H = display.contentHeight local XCentre = display.contentCenterX local YCentre = display.contentCenterY local screenLeft = display.screenOriginX local screenRight = display.viewableContentWidth + display.screenOriginX local screenTop = display.screenOriginY local screenBottom = display.viewableContentHeight + display.screenOriginY local random = math.random ---------------------------------------------------------------------------------------- -- a simple function to destroy the boxes ---------------------------------------------------------------------------------------- local function Destroy\_Box (obj) print("obj remove") display.remove( obj ) obj = nil end ---------------------------------------------------------------------------------------- -- Display, add Physics and Move the Boxes! ---------------------------------------------------------------------------------------- local function SpawnBox () local BadGuy = display.newRect(random(screenLeft,screenRight), 0, 40,40 ) transition.to( BadGuy,{time = random (1000,3000), y = screenBottom + 20, onComplete = Destroy\_Box} ) return BadGuy end timer.performWithDelay( random (120,500), SpawnBox , -1)
Thank you so much! although I’m not quite sure how i would insert this into a table. Lets say i wanted to click on the rectangle to perform a certain task. how would i go about doing that?
oh ok that makes life a little more difficult, i assumed you were making a background, in that case i would create a finite no. of boxes and then you can individually manipulate them.
----------------------------------------------------------------------------------------- -- Localize to improve performance ----------------------------------------------------------------------------------------- local W = display.contentWidth local H = display.contentHeight local XCentre = display.contentCenterX local YCentre = display.contentCenterY local screenLeft = display.screenOriginX local screenRight = display.viewableContentWidth + display.screenOriginX local screenTop = display.screenOriginY local screenBottom = display.viewableContentHeight + display.screenOriginY local random = math.random ---------------------------------------------------------------------------------------- -- This is a forward reference to a funciton i call later on ---------------------------------------------------------------------------------------- local Fall ---------------------------------------------------------------------------------------- -- Resets the postion of the boxes ---------------------------------------------------------------------------------------- local function Restart\_Box (obj) print("obj restarted") obj.x = random(screenLeft,screenRight) obj.y = -20 local FallerCaller = Fall (obj) end ---------------------------------------------------------------------------------------- -- Makes the boxes fall ---------------------------------------------------------------------------------------- function Fall (obj) transition.to( obj ,{time = random (3000,9000), delay = random (100,900), y = screenBottom + 20, onComplete = Restart\_Box} ) end local function onBoxTouch (event) local target = event.target target:setFillColor( 0,1,1 ) print("lol") end ---------------------------------------------------------------------------------------- -- A table to hold our boxes ---------------------------------------------------------------------------------------- local Boxes = {} for i = 1,10 do Boxes[i] = display.newRect(random(screenLeft,screenRight), -20, 40,40 ) Boxes[i]:addEventListener( "touch", onBoxTouch ) local Faller = Fall (Boxes[i]) end
btw if you wanted them to fall at the same speed, just change the time of the transition and if you dont want them to reset then you can remove the OnComplete phrase from the transition. ! Hope i helped