hey everybody
i have a small but simple thing i cant seem to work, its about a collision detection in my game. first of all i have created a display object called ground and enemy balloon. what i want is, when the enemy balloon touches or collides with the ground, i want the enemy balloon to be removed. can anyone help me do this?
thanks, hazza
my code can be found below
-- -- Project: sticky balloons -- Description: -- -- Version: 1.0 -- Managed with http://CoronaProjectManager.com -- -- Copyright 2013 Harry Newton. All Rights Reserved. -- display.setStatusBar(display.HiddenStatusBar) local physics = require "physics" physics.start();physics.setGravity(0,2.7) local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY) local cw, ch = display.contentWidth, display.contentHeight local stage = display.getCurrentStage() physics.setDrawMode("hybrid") local foreGround = display.newGroup() local Balloons = display.newGroup() Balloons.isVisible = true --------------------------------------------------------------------- --FOREGROUND OBJECTS -------------------------------------------------------------------- local background = display.newImage(foreGround,"background.jpg") local ground = display.newImage(foreGround,"floor.png") physics.addBody(ground,"static") ground.y = 480 ground.type = "removeObject" local turret = display.newImageRect(foreGround,"BalloonTurret.png",134,134) turret.x = 160 turret.y = 420 local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY) local cw, ch = display.contentWidth, display.contentHeight local wallL = display.newRect( -ox, -oy,1.5, ch+oy+oy ) wallL.isVisible = false physics.addBody(wallL, "static", { bounce=0.6, friction=1.0 } ) wallL.type = "notSticky" local wallR = display.newRect( cw-1.5+ox, -oy, 1.5, ch+oy+oy ) wallR.isVisible = false physics.addBody(wallR, "static", { bounce=0.6, friction=1.0 } ) wallR.type = "notSticky" ------------------------------------------------------------- ------------------------------------------------------------- local projFiring = false local proj local scaleFactor = 1.0 local physicsData = (require "Balloon").physicsData(1.0) local function newProj() proj = display.newImage(Balloons,"Balloon.png"); proj.x = turret.x ; proj.y = turret.y - 20 physics.addBody( proj, "dynamic",physicsData:get("balloon") ) proj.gravityScale = -5 projFiring = false proj.isBullet = true proj.isBodyActive = false proj.isVisible = false end local enemyBalloon local function spawnEnemy() enemyBalloon = display.newImage(Balloons,"Balloon.png") physics.addBody(enemyBalloon,"dynamic", physicsData:get("balloon") ) enemyBalloon.x = math.random(0,320) enemyBalloon.y = -60 enemyBalloon:setFillColor( 214, 0, 19 ) enemyBalloon:applyTorque( 50 ) return enemyBalloon end local t --collision handler local function projCollide( self,event ) if ( event.phase == "began" ) then --get world coordinates of projectile for joint reference self:removeEventListener( "collision", self ) ; self.collision = nil --delay function to resolve collision local function resolveColl( timerRef ) if ( timerRef.source.action == "makeJoint" ) then local weldJoint = physics.newJoint( "weld", self, event.other, self.x, self.y ) end end --check if velocity of projectile is sufficient to "stick" local vx,vy = self:getLinearVelocity() local dirVel = math.sqrt( (vx\*vx)+(vy\*vy) ) if ( dirVel \> 100 ) then --if sufficient, stop velocity and trigger joint creation self:setLinearVelocity( 0,0 ) t = timer.performWithDelay( 10, resolveColl, 1 ) ; t.action = "makeJoint" else --if not sufficient, "break" projectile and create new t = timer.performWithDelay( 10, resolveColl, 1 ) ; t.action = "none" end if (event.other.type == "notSticky") then --local t = timer.performWithDelay(2,resolveColl,1) t.action = "none" end end end local function touchAction(event) if ( event.phase == "began" and projFiring == false ) then projFiring = true proj.isBodyActive = true proj.isVisible = true local px,py = event.x-proj.x, event.y-proj.y proj:applyLinearImpulse( px/8, py/8, proj.x, proj.y ) proj:applyTorque( 50 ) proj.gravityScale = -5 proj.collision = projCollide ; proj:addEventListener( "collision", proj ) end if (projFiring == true) then newProj() end end newProj() stage:addEventListener( "touch", touchAction ) local spawnTmr local spawnTime = 5000 local difficultyLevels = 4 --local spawnTimer = timer.performWithDelay(5000,spawnEnemy, 0 )---- THIS IS AN INFINITE LOOP HARRY ,U MUST YOU'SE THIS A LOT local function increaseDifficulty() if spawnTmr ~= nil then timer.cancel(spawnTmr) spawnTmr = nil end spawnTime = spawnTime - 500 -- just to be safe, set this to whatever spawn rate would be the -- fastest, so game never spawns to fast if spawnTime \< 500 then spawnTime = 500 end -- spawn at this rate until this timer is canceled spawnTmr = timer.performWithDelay(spawnTime, spawnEnemy, 0) end -- this timer loops only till spawning at most difficult speed is reached local difficultyTmr = timer.performWithDelay(5000, increaseDifficulty,difficultyLevels )