Hi
I’m trying to learn about collisions and have hit a snag. In the example code I have an object called “bullet” which is spawned by a function on tapping the “player1.” Additionally, I have an “alien” object, spawned by a function to randomly generate it’s position off screen.
The player1 object detects collisions between the others, but crucially, the other objects don’t listen for collisions. The code causing me problems is between lines 68-75.
If I put the listeners for either object outside of their functions I get an “attempt to global bullet (a nil value)” error.
If I make the functions global it doesn’t make any difference.
I know there’s extensive documentation on the subject of Collision Detection, but I just can’t seem to find an answer to this particular problem.
physics = require("physics")
physics.start()
physics.setGravity(0,5)
\_H = display.contentHeight;
\_W = display.contentWidth;
mRand = math.random;
local player1 = display.newImage("player.png")
player1.x = \_W / 2
player1.y = \_H - player1.contentHeight
--player1:setFillColor(0, 0, 255)
physics.addBody(player1, "kinematic", {density = 1.0, friction = 1, bounce = 0.2})
player1.myName = "player1"
local function dragplayer1 (event)
player1.x = event.x
--player1.y = event.y
end
local function shoot (event)
local bullet = display.newRect(0, 0, 5, 20)
bullet.x = player1.x
bullet.y = player1.y - 20
bullet:setFillColor(255, 0, 0)
physics.addBody(bullet, "dynamic", {density = 10.0, friction = 0, bounce = 0})
bullet:setLinearVelocity( 0, -1000)
bullet.myName = "bullet"
bullet.collision = onLocalCollision
bullet:addEventListener( "collision", bullet )
end
player1:addEventListener ("touch", dragplayer1)
player1:addEventListener ("tap", shoot)
local function spawnAlien()
local alien = display.newRect(0, 0, 60, 60)
alien.x = mRand(0, \_W)
alien.y = \_H / \_H - 50
alien:setFillColor(255, 0, 0)
physics.addBody(alien, {density = 1.0, friction = 1, bounce = 0.2})
alien.myName = "alien"
alien.collision = onLocalCollision
alien:addEventListener( "collision", alien )
end
local ground = display.newRect(0, 0, \_W, 5)
ground.x = \_W / 2
ground.y = \_H - 5
ground:setFillColor(0, 255, 0)
physics.addBody(ground, "static", {density = 1.0, friction = 1, bounce = 0.2})
ground.myName = "ground"
local function onLocalCollision( self, event )
if ( event.phase == "began" ) then
if (self.myName == "player1" and event.other.myName == "alien") then
print("yipee")
local function deleteSelf()
player1:removeSelf()
end
Timer1 = timer.performWithDelay(1,deleteSelf, 1)
end
if (self.myName == "bullet" and event.other.myName == "alien") then
print("ZAP!")
local function deleteBoth()
bullet:removeSelf()
alien:removeSelf()
end
Timer1 = timer.performWithDelay(1,deleteBoth, 1)
end
print( self.myName .. ": collision began with " .. event.other.myName )
end
end
player1.collision = onLocalCollision
player1:addEventListener( "collision", player1 )
Timer1 = timer.performWithDelay(2000,spawnAlien, 0)
Thanks [import]uid: 67933 topic_id: 13659 reply_id: 313659[/import]