I’m trying to set up a simple game where I have several objects on screen that I want to be able to attack. They don’t move. I have one object, the attacker that I will touch, drag to one of the targets and destroy. Once I let up from the touch, I will to a transition.to(attacker, {x = event.x, y = event.y, time=1000}).
I have all of this working. The attacker moves to where I have told it to go to. But I’m not detecting that it collided with the destination (or any other targets in between). The event handler has a print() at the top of it and I’m not getting any output.
I was hoping I could do this without the physics engine, just having the transition.to interrupted and trip my event handler (I don’t need physics for the game)
But I’ve added physics:
[blockcode]
local physics = require(“physics”)
physics.start()
function onAttackerCollision( self, event )
print("collision detected between " … self.myName … " and " … event.other.myName)
if ( event.phase == “began” ) then
print( self.myName … ": collision began with " … event.other.myName )
elseif ( event.phase == “ended” ) then
print( self.myName … ": collision ended with " … event.other.myName )
end
end
…
attacker = display.newImage(“attacker.png”);
attacker.x = display.contentWidth - 15;
attacker.y = math.random((display.contentHeight - 45)) + 45;
physics.addBody( attacker, “static”, { density=1.6, friction=1.0, bounce=0.0 } )
attacker:addEventListener( “touch”, onArmyTouch )
attacker.collision = onAttackerCollision
attacker:addEventListener( “collision”, attacker)
attacker.myName = “attacker”
[/blockcode]
After looking at various forum posts and reading through the docs more and more, I ended up adding the targets in as well:
[blockcode]
target = display.newImage(“target.png”);
– has to be on left third of the board and on screen
target.x = math.random((display.contentWidth / 3) - target.width) + target.width;
target.y = math.random((display.contentHeight - 30) - target.height) + target.height;
target.myName = “target”
physics.addBody( target, “static”, { density=1.6, friction=1.0, bounce=0.0, radius=24, isSensor = true } )
[/blockcode]
so I’m not sure why this isn’t working. I’ve not tried turning the attacker into a bullet yet, but my event handler isn’t firing.
Any ideas?
[import]uid: 19626 topic_id: 6281 reply_id: 306281[/import]