-- -- Abstract: CollisionDetection sample project -- Demonstrates global and local collision listeners, along with collision forces -- -- Version: 1.2 (revised for Alpha 3, demonstrates "collision" event and new "preCollision" and "postCollision" events) -- -- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license -- Copyright (C) 2010 ANSCA Inc. All Rights Reserved. local physics = require( "physics" ) physics.start() --Set gravity to 0 since this is a top-down game physics.setGravity(0,0) local sky = display.newImage( "bkg_clouds.png" ) sky.x = display.contentWidth / 2 sky.y = 195 local ground = display.newImage( "ground.png" ) ground.x = display.contentWidth / 2 ground.y = 445 ground.myName = "ground" --construct player character, for now an egg local eggCollisionFilter = { categoryBits = 3, maskBits = 1 } local eggBody = { density=1.0, friction=0.1, bounce=0.5, radius=25, filter = eggCollisionFilter } local egg = display.newImage( "egg.png" ) egg.x = display.contentWidth / 2 egg.y = 200 egg.myName = "Egg buddy" physics.addBody(egg, eggBody) --physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } ) local coconuts = {} local waves = 0 local function spawnProjectile(posx, posy, velx, vely) local coconutCollisionFilter = { categoryBits = 1, maskBits = 2 } local coconutBody = { density=3.0, friction=0, bounce=1.0, filter=coconutCollisionFilter} local coconut = display.newImage( "coconut.png" ) coconut.x = posx; coconut.y = posy; physics.addBody( coconut, coconutBody) coconut:applyLinearImpulse( velx * (egg.x - coconut.x) , vely * (egg.y - coconut.y), coconut.x, coconut.y ) coconuts[#coconuts + 1] = coconut end local function projectileManager() local leftright = 1 --Fillers: local a = .1 spawnProjectile(320, 380, a ,a) --Waves: --if(#coconuts % 10 == 0) then --local i = 0 --local waveSize = 10 -- while(i < 10) do -- leftright = math.random(0, 1) * 2 - 1 -- local randomY = math.random(400) -- spawnProjectile(300 * leftright, randomY, egg.x, egg.y) -- i = i + 1 -- end --end end --Defined outside of function to represent last touch location local lastTouchX = 0 local lastTouchY = 0 local function moveCharacter(event) print("Eggx:", egg.x, " Eggy:", egg.y) local targetPositionX = display.contentWidth / 2 local targetPositionY = 200 local moveAcceleration = 10 local moveAccelerationX = 0 local moveAccelerationY = 0 local deltaX = targetPositionX - egg.x local deltaY = targetPositionY - egg.y if (math.abs(deltaX) > math.abs(deltaY)) then if(deltaX < 0) then moveAccelerationX = moveAcceleration * -1 else moveAccelerationX = moveAcceleration end elseif (math.abs(deltaX) < math.abs(deltaY)) then if(deltaY < 0) then moveAccelerationY = moveAcceleration * -1 else moveAccelerationY = moveAcceleration end end egg:applyForce(moveAccelerationX, moveAccelerationY, egg.x, egg.y) end local function onLocalCollision( self, event ) --egg.isBodyActive = false print("GAME OVER") end timer.performWithDelay( 1000, projectileManager, 1400 ) egg.collision = onLocalCollision egg:addEventListener( "collision", egg ) sky:addEventListener( "touch", moveCharacter )