Collision Problems

So my friend and i are making a game with zombies, a laser button, and a happy face. When the happy face collides with the zombie, we want the happy face to disappear (we accomplished this). When the laser collides with the zombie, we want the zombie to disappear. Any help would be GREAT.

function onCollision(event)

–print(“collide!”)

happy:removeSelf()

end

Runtime:addEventListener(“collision”, onCollision)

end

local total_zombie = 100

tmr = timer.performWithDelay(2000, spawnzombie, total_zombie);

– BUTTON code

local widget = require( “widget” )

local function handleButtonEvent( event )

        local phase = event.phase

        if “ended” == phase then

                print( “you pressed and released a button!” )

        end

end

– guns

local function fireLasers()

  local laserbeam = display.newImage(“laserbeam.jpg”)

  laserbeam:scale( 0.5, 0.5 )

  laserbeam.x = happy.x

  laserbeam.y = happy.y

  transition.to(laserbeam, {time=1000, x = math.random() ,y = math.random(), onComplete=movelaserbeam});

end

local fireButton = display.newImage( “firebutton.jpg” )

fireButton.x = 50

fireButton.y = display.contentHeight-50

local function handleFireButton( event )

        if ( event.phase == “began” ) then

                fireLasers()

        elseif ( event.phase == “ended” ) then

                fireLasers()

        end

        return true

end

fireButton:addEventListener( “touch”, handleFireButton )

local needToFire = false

local function handleEnterFrame( event)

        if ( needToFire == true ) then

                fireLasers()

        end

end

Runtime:addEventListener( “enterFrame”, handleEnterFrame )

local fireButton = display.newImage( “firebutton.jpg” )

fireButton.x = 50

fireButton.y = display.contentHeight-50

local function handleFireButton( event )

                if ( event.phase == “began” ) then

                        needToFire = true

                elseif ( event.phase == “ended” and needToFire == true ) then

                needToFire = false

        end

        return true

end

fireButton:addEventListener( “touch”, handleFireButton )

– ugh

local needToFire = false

local function handleEnterFrame( event )

   if ( needToFire == true ) then

      fireLasers()

   end

end

Runtime:addEventListener( “enterFrame”, handleEnterFrame )

local function handleFireButton2( event )

   if ( event.phase == “began” ) then

      – fire the weapon

      needToFire = true

   elseif ( event.phase == “ended” and needToFire == true ) then

      – stop firing the weapon

      needToFire = false

   end

   return true

end

local fireButton2 = widget.newButton{

   width = 64,

   height = 64,

   defaultFile = “firebutton.jpg”,

   overFile = “button1.jpg”,

   onEvent = handleFireButton

}

fireButton.x = 50

Oh this isn’t all the code, just where we have one collision

I don’t see anything in your code that looks like it’s handling collisions with the zombies, I only see the one function that I assume removes the happy face. Check out this tutorial if you are not using physics to handle collision detection.

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Once you detect the collision between laser and zombie, you would just do the same as what you’re doing for the happy face.

I also recommend this tutorial to learn about Object Oriented Programming basics in Corona/Lua, and how you can dispatch your own custom events. The concepts are a little advanced but will be incredibly useful as you get further along in creating your game.

http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk

Oh this isn’t all the code, just where we have one collision

I don’t see anything in your code that looks like it’s handling collisions with the zombies, I only see the one function that I assume removes the happy face. Check out this tutorial if you are not using physics to handle collision detection.

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Once you detect the collision between laser and zombie, you would just do the same as what you’re doing for the happy face.

I also recommend this tutorial to learn about Object Oriented Programming basics in Corona/Lua, and how you can dispatch your own custom events. The concepts are a little advanced but will be incredibly useful as you get further along in creating your game.

http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk