Hi I’m having trouble with making a first 2-D shooting game. Ok my problem is I have a CrossHair and when I drag the cross hair in the middle of the enemy the enemy does the die. The enemy only dies when collision of CrossHair is began or ended. I’m trying to make so that the CrossHairs can collided any were in the body. Here is the code
[code] local physics = require(“physics”)
physics.start()
physics.setDrawMode(“hybrid”)
physics.setGravity(0,0)
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “static”
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “static”
end
end
end
– Stop further propagation of touch event!
return true
end
–Hero is the crosshair
local hero = display.newCircle( 40, 40, 30 )
hero.name = “hero”
physics.addBody(hero, “dynamic”)
local function moveHero (event)
hero.x = event.x
hero.y = event.y
end
hero:addEventListener(“touch”, moveHero)
–Bk3 is the button to shoot
local bk3 = display.newRect(0,0,30,30)
bk3.x = 100
bk3.y = 400
bk3.hit = “bk3”
bk3.isSensor = true
–Below this code is the function for the crosshair to shoot
–when the button is touch the crosshair should destroy the enemy
local colorSensor = display.newCircle( 160, 240, 80 )
physics.addBody(colorSensor, {isSensor = true})
colorSensor.alpha = 0.2
local function shoot (event)
–if event.phase == “began” then
colorSensor:addEventListener(“collision”, colorSensor)
function colorSensor:collision (event)
if event.other.name == “hero” and event.phase == “began” then
red = math.random(0,255)
green = math.random(0,255)
blue = math.random(0,255)
display.remove(colorSensor)
hero:setFillColor(red, green, blue)
end
end
end
bk3:addEventListener(“touch”, shoot)[/code]
If is confusing ask thanks for the help [import]uid: 17058 topic_id: 25010 reply_id: 325010[/import]