How to make shooting Crosshairs?

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]

I’m not going to dig into your code, but talk about your problem in general concerns.

Collision detection (both physics and non-physics based) happens when the to images first touch (the began event) and when they stop (the ended event). There is no other possibilities using common methods. In most cases this is sufficient. But lets say you want to record head shots or heart shots in your shooting game or simply when the crosshairs are in the middle of the body… which it sounds like what you are wanting.

To do this you need to do a couple of things. For Turkeys Revenge I had my larger cross-hair graphic, but I also added a small dot in the center as a separate object. It’s with this small dot that I use to detecting where I’m shooting. When I move the crosshair graphic, I also move the center circle graphic to the same X, Y and they stay together. So now my gun is a very small target to improve accuracy.

Now for your targets you have to do the same thing. The collision detection in the physics engine will help you here. Instead of using the whole graphic, you can define a custom shape that could be some where inside the larger graphic. You can even setup multiple shapes if you want to have multiple hit spots on the target. Using this you can make a smaller target in the middle of your larger graphic that will be what triggers the collision. Sure it will still be a began/ended events, but they won’t happen at the edge, but towards the center. [import]uid: 19626 topic_id: 25010 reply_id: 101623[/import]