Hi everyone.
I’m trying to do something pretty simple. I have some circles which spawn randomly and move down the screen. When they collide with an object at the bottom of the screen, they need to be removed/deleted. I’ve extensively searched the forums and no matter what approach I try it always kicks up an error.
I’ve tried putting the collision event inside the spawn function, changing the syntax numerous ways, creating a function to delete the objects, etc. I can’t use transitions or “onComplete” to remove them, so am stuck.
Can someone please tell me what I’m doing wrong? It’s probably something really simple, but I just can’t seem to figure out what.
Here’s my code:
--hides iphone status bar
--display.setStatusBar(display.HiddenStatusBar)
physics = require("physics")
physics.start()
physics.setGravity(0,0)
physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.
--physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.
--cache content dimensions and math.random for better performance
\_H = display.contentHeight;
\_W = display.contentWidth;
mRand = math.random;
terrainspeed = 5
maxCrater = 5
crad30 = 30
crad60 = 60
crad100 = 100
tsmod = 0
craterRate = 1000
craterMod = 0
local centerX = display.contentWidth/2;
local hitdet = display.newRect(0, 0, \_W, 5)
hitdet.x = \_W / 2
hitdet.y = \_H - 5
hitdet:setFillColor(0, 0, 255)
physics.addBody( hitdet, { friction=0, bounce=0,} )
hitdet.isSensor = true
hitdet.myName = "hitDet"
local buggy = display.newRect(0, 0, 30, 30)
buggy.x = \_W / 2
buggy.y = \_H / 3 + \_H / 3
buggy.strokeWidth = 3
buggy:setFillColor(140, 140, 140)
buggy:setStrokeColor(180, 180, 180)
function tsmodifier() --picks a random value by which to modify terrainspeed
tsmod = mRand(0, 10)
--print (tsmod)
end
function spawnCrater() -- spawns a crater, checks its y position and moves it if necessary.
local crater = display.newCircle (mRand(0, \_W), \_H / \_H, mRand(30, 100))
physics.addBody( crater, { friction=0, bounce=0, radius = crater.radius} )
crater.isSensor = true
crater.myName = "Crater"
local function moveCrater() -- moves the crater
if crater.y \< \_H + crater.contentHeight then
crater.y = crater.y + terrainspeed + tsmod
end
end
local function onCollision( event )
if ( event.phase == "began" ) then
print( "began: " .. event.object1.myName .. " & " .. event.object2.myName )
if ( crater ) then
crater:removeSelf()
crater = nil
end
elseif ( event.phase == "ended" ) then
print( "ended: " .. event.object1.myName .. " & " .. event.object2.myName )
end
end
Runtime:addEventListener( "collision", onCollision )
Timer2 = timer.performWithDelay(10,moveCrater, 0)
end
Runtime:addEventListener( "collision", onCollision )
Timer1 = timer.performWithDelay(craterRate, spawnCrater, 0)
Timer3 = timer.performWithDelay(5000,tsmodifier, 0)
Thanks
Dan [import]uid: 67933 topic_id: 12865 reply_id: 312865[/import]