ok basically we have a button that fires lasers and when it collides with zombies, the zombies remove itself. we have a pre-existing collision functions (a few actually) and for some reason, this one wont work. here is all of the code
display.setStatusBar(display.HiddenStatusBar)
local physics = require “physics”
physics.start()
physics.setGravity(0,0)
local pWidth, pHeight = display.actualContentWidth, display.actualContentHeight
– create wall objects
local topWall = display.newRect( 0, 0, display.contentWidth+20, 20 )
local bottomWall = display.newRect( 0, display.contentHeight - 10, display.contentWidth, 20 )
local leftWall = display.newRect( 0, 0, 10, display.contentHeight )
local rightWall = display.newRect( display.contentWidth - 10, 0, 10, display.contentHeight+20 )
– make them physics bodies
physics.addBody(topWall, “static”, {density = 1.0, friction = 1, bounce = 40, isSensor = false})
physics.addBody(bottomWall, “static”, {density = 1.0, friction = 1, bounce = 40, isSensor = false})
physics.addBody(leftWall, “static”, {density = 1.0, friction = 1, bounce = 40, isSensor = false})
physics.addBody(rightWall, “static”, {density = 1.0, friction = 1, bounce = 40, isSensor = false})
local background = display.newImage(“background.jpg”)
local happy = display.newImage(“happy.png”)
happy.x = 100
happy.y = 100
physics.addBody(happy, “dynamic”)
– TOUCH EVENTS FOR HAPPY MOVEMENT
function touchScreen(event)
if event.phase == “ended” then
transition.to(happy,{time=700, x=event.x, y=event.y})
end
end
Runtime:addEventListener(“touch”, touchScreen)
– ok shlok, trying to make collisions work anything after this if it doesn’t work u can delete
local backgroundmusic = audio.loadStream("_ElecTechno.m4a")
local backgroundmusic2 = audio.play( backgroundmusic, { loops=-1, fadein=2000 } )
– every x seconds
local function listener(me)
transition.to (me, {time = math.random(1000,4000), x = math.random(10,310), y = -30, onComplete = function()listener(me)end});
end
–Spawning multiple objects in randoms locations
local function spawnzombie()
local zombie = display.newImageRect(“zombie.jpg”, 45, 45);
zombie.x = 200;
zombie.y = 200;
function movezombie()
transition.to(zombie,{time=7000, x=math.random(0,500), y=math.random(0,600), onComplete=movezombie});
end
physics.addBody(zombie, “dynamic”, {density = 0.1, bounce = 0.1, friction = .1, radius = 0});
movezombie()
--Adding touch event
zombie:addEventListener(“touch”, zombie);
zombie.collType = “dynamic”
physics.addBody( zombie, { bounce=10.0, friction=1.0 } )
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
local myButton = widget.newButton
{
left = 800,
top = 400,
width = 100,
height = 100,
defaultFile = “button1.jpg”,
overFile = “smallbutton.png”,
label = “GRENADE”,
onEvent = handleButtonEvent,
}
– guns
local function fireLasers()
local laserbeam = display.newImage(“laserbeam.jpg”)
laserbeam:scale( 0.5, 0.5 )
laserbeam.x = 200
laserbeam.y = 200
transition.to(laserbeam, {time=100, x = 600 ,y = 200, 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
— zombie collisions?
function onCollision4(event)
print(“collide”)
zombie:removeSelf()
end