Hi everyone! I’ve been on Corona for about 2 months now, and I’ve been working on my first game (Mostly learning as I add different components to the game). I’ve been stuck on a collision detection probably for several days, and any help would be greatly appreciated. Here’s a few chunks of code I’ve been having trouble with:
local function movePlayer (event) if event.phase == "moved" then transition.to(NinjaPlayer1, {time = 20, x = event.x, y = event.y}) if event.y \> 1750 then NinjaPlayer1.alpha = .4 else NinjaPlayer1.alpha = 1 end end end
function createNewObstacle() local generateNum = math.random(1,3) if generateNum == 1 and erVar ~= 1 then -- generate erVar = 1 generated1 = display.newGroup() local barrel1 = display.newRect(50, 1950, 100, 100) local barrel2 = display.newRect(50, 50, 100, 100) local barrel3 = display.newRect(950, 1950, 100, 100) physics.addBody( barrel1, "static", {density = .1, bounce = 0.1, friction = 0.1}) physics.addBody( barrel2, "static", {density = .1, bounce = 0.1, friction = 0.1}) physics.addBody( barrel3, "static", {density = .1, bounce = 0.1, friction = 0.1}) generated1:insert(barrel3) generated1:insert(barrel1) generated1:insert(barrel2) generated1.y = -2000 local myClosure = function() return scrollObstacle( generated1 ) end timer.performWithDelay(speedDelay, myClosure, 400) print ("Gen1Created") end if generateNum == 2 and erVar ~=2 then -- generate erVar = 2 generated2 = display.newGroup() local barrel2 = display.newRect(50, 1950, 100, 100) local barrel3 = display.newRect(50, 50, 100, 100) local barrel1 = display.newRect(950, 1950, 100, 100) physics.addBody( barrel1, "static", {density = .1, bounce = 0.1, friction = 0.1}) physics.addBody( barrel2, "static", {density = .1, bounce = 0.1, friction = 0.1}) physics.addBody( barrel3, "static", {density = .1, bounce = 0.1, friction = 0.1}) barrel2:setFillColor(100,0,0) barrel3:setFillColor(100,0,0) barrel1:setFillColor(100,0,0) generated2:insert(barrel3) generated2:insert(barrel1) generated2:insert(barrel2) generated2.y = -2000 local myClosure = function() return scrollObstacle( generated2 ) end timer.performWithDelay(speedDelay, myClosure, 400) print ("Gen2Created") end if generateNum == 3 and erVar ~=3 then -- generate erVar = 3 generated3 = display.newGroup() local barrel2 = display.newRect(50, 1950, 100, 100) local barrel3 = display.newRect(50, 50, 100, 100) local barrel1 = display.newRect(950, 1950, 100, 100) local building1 = display.newImage("greyBuilding1.png", 500,1000) physics.addBody( barrel1, "static", {density = .1, bounce = 0.1, friction = 0.1}) physics.addBody( barrel2, "static", {density = .1, bounce = 0.1, friction = 0.1}) physics.addBody( barrel3, "static", {density = .1, bounce = 0.1, friction = 0.1}) physics.addBody( building1, "static", {density = .1, bounce = 0.1, friction = 0.1}) barrel2:setFillColor(0,100,0) barrel3:setFillColor(0,100,0) barrel1:setFillColor(0,100,0) generated3:insert(building1) generated3:insert(barrel3) generated3:insert(barrel1) generated3:insert(barrel2) generated3.y = -2000 local myClosure = function() return scrollObstacle( generated3 ) end timer.performWithDelay(speedDelay, myClosure, 400) print ("Gen2Created") end end local function movePlayer (event) if event.phase == "moved" then transition.to(NinjaPlayer1, {time = 20, x = event.x, y = event.y}) if event.y \> 1750 then NinjaPlayer1.alpha = .4 else NinjaPlayer1.alpha = 1 end end end function scrollObstacle(specGen) if specGen.y == nil then return end if specGen.y \>= 1980 then specGen:removeSelf() specGen = nil print ("deleted") else specGen.y = specGen.y + 10 end end function onCollision( event ) if ( event.phase == "began" ) then composer.gotoScene( "menu" ) end end NinjaPlayer1 = display.newImage("NinjaPlayer1.png") NinjaPlayer1.x = 500 NinjaPlayer1.y = 1400 physics.addBody( NinjaPlayer1, "dynamic", {density=1.0, bounce = 0.1, friction = 0.1}) Runtime:addEventListener("touch", movePlayer) Runtime:addEventListener("collision", onCollision) createNewObstacle() obstacleGen = timer.performWithDelay(20000/3, createNewObstacle, 0)
Again, I have alot more code than this, this is just what I think is needed to solve my problem…
If NinjaPlayer1 collides with a individual item from one of the generated groups, ‘onCollision’ is supposed to be called. However it doesnt… I’ve played with a few things and I found that the NinjaPlayer1 will collide with the original place the item was placed, but not with the actual item as it scrolls down the screen.
I do realize this code is kinda bad and messy, and there may be more efficient ways to do what I made. So any suggestions would also be nice =)
Again, any help would be greatly appreciated!