Hi folks,
I’m new to Corona and Lua and I’m setting up a pretty simple ‘shooter’ game where a series of objects (actually flying zombie heads) travel across the screen and need to be ‘shot’ by moving a targeting reticle on top of them and pressing the fire button.
Most things are working and the game is almost playable but I’m stuck on getting the collision between the targeting reticle and the zombie head to actually register and play the ‘death’ function (which is obviously pretty key).
The targeting reticle works in that it can be moved by dragging the finger anywhere on the screen, and the fire button works in that it plays the animation of the gun firing, so I feel like I’m close and just can’t quite tie it all together.
Anyway, here’s some snippets of what I have:
[code]–Set Gun
local spriteSheet = sprite.newSpriteSheet(“pistol.png”, 256, 256)
local pistolSet = sprite.newSpriteSet(spriteSheet, 1, 8)
sprite.add(pistolSet, “shoot”, 1, 8, 200, 1)
local pistol = sprite.newSprite(pistolSet)
pistol.x = 440; pistol.y = 225
displayReticle:insert(pistol)
–Fire button
fireBtn = display.newImage( “fire.png” )
fireBtn.x = 50
fireBtn.y = 270
displayGUI:insert(fireBtn)
function fireBtn:tap( event )
pistol:prepare(“shoot”)
pistol:play(“shoot”)
if event.phase == “began” then --Here I’m trying to create a boolean for the fireBtn state to call later when checking to see if the button is presssed
fireBtnDown = true
elseif event.phase == “ended” then
fireBtnDown = false
end
end
fireBtn:addEventListener( “tap”, fireBtn)
–Targeting Reticle
reticle = display.newImage( “Reticle.png” )
physics.addBody( reticle, { isSensor = true } )
reticle.bodyType = “kinematic”
reticle.isAwake = true;
reticle.name = “reticle”
displayReticle:insert(reticle)
–function makes it so that the reticle is moved around the screen by dragging a finger anywhere on the background
local function onTouch( event )
if “began” == event.phase then
reticle.isFocus = true
reticle.x0 = event.x - reticle.x
reticle.y0 = event.y - reticle.y
elseif reticle.isFocus then
if “moved” == event.phase then
reticle.x = event.x - reticle.x0
reticle.y = event.y - reticle.y0
–coerceOnScreen( reticle )
elseif “ended” == phase or “cancelled” == phase then
reticle.isFocus = false
end
end
return true
end
– Only the background receives touches.
background:addEventListener( “touch”, onTouch)
–This below function doesn’t seem to do anything, but I was strying to get it to check if both the target was over the head and the button was pressed
function fireCheck( event )
if ( event.phase == “began” ) then
if fireBtnDown == true then
killHead()
end
end
end
reticle.collision = fireCheck
reticle:addEventListener( “collison”, fireCheck )
–Destroy Head Function
function killHead( event )
zombie1:removeSelf()
display.newImage(“Zombie01dead.png”, zombie1.x - 50, zombie1.y - 50)
end
–Create head as physics object
function randomZombieHead()
zombie1 = display.newImage( “Zombie01.png”)
zombie1.x = math.random (30, 450); zombie1.y = 350
zombie1.myName = “Zombie Head”
displayHeads:insert(zombie1)
physics.addBody( zombie1, { density = 1.0, friction = 0.3, bounce = 0.2, radius = 25 } )
– Apply random force to zombie heads
zombie1:applyForce( math.random (-250, 250), math.random (-900, -800), zombie1.x, zombie1.y )
end[/code]
Sorry for the super noob code guys but I figured the best way to learn (after doing a few tutorials) is to try and get my own thing going.
I know that I have another issue where I need to call the random zombie heads into a table so that they can each register a hit if more than one are on the screen at the same time, but I’ll be happy if I can figure out how to make one ‘killed’ properly at this point.
Thanks! [import]uid: 36682 topic_id: 27604 reply_id: 327604[/import]
[import]uid: 52491 topic_id: 27604 reply_id: 112166[/import]