collision problem

I have a error attempt to perform arithmetic on field ‘x’ (a nil value), when I press the button after the bullet collided with the enemy

player = display.newRect(0,0,50,50) player.x =110 player.y = 110 b1 = display.newCircle(0,0,30) b1.x ,b1.y = 210 , 110 function hasCollided( obj1, obj2 ) if ( obj1 == nil ) then --make sure the first object exists return false end if ( obj2 == nil ) then --make sure the other object exists return false end local dx = obj1.x - obj2.x local dy = obj1.y - obj2.y local distance = math.sqrt( dx\*dx + dy\*dy ) local objectSize = (obj2.contentWidth/3) + (obj1.contentWidth/3) if ( distance\< objectSize+3) then return true end return false end local function moveBullet(obj) if obj.y \< 500 then obj.y = obj.y + 8 if hasCollided(obj,E1) then E1:removeSelf() Runtime:removeEventListener("enterFrame",E1) obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) end else obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) obj = nil end end function mBullet(e) if e.phase == "ended" then bullet = display.newCircle(0,0,5) bullet.x =player.x bullet.y = player.y+ 40 bullet.enterFrame = moveBullet Runtime:addEventListener("enterFrame",bullet) end end function move(obj) if obj.y \>-30 then obj.y = obj.y - 2 else obj:removeSelf() Runtime:removeEventListener("enterFrame",obj) obj = nil end end function enemy() E1 = display.newRect(0,0,40,20) E1.y =400 E1.x = 110 E1.enterFrame = move Runtime:addEventListener("enterFrame",E1) end b1:addEventListener("touch",mBullet) timer.performWithDelay(2500,enemy,0)

could you tell me where / what line this error is on because i am having trubble reading your code

 thanks:

Sean:

from Boxing Studio Games

function hasCollided( obj1, obj2 ) if ( obj1 == nil ) then --make sure the first object exists return false end if ( obj2 == nil ) then --make sure the other object exists return false end local dx = obj1.x - obj2.x local dy = obj1.y - obj2.y local distance = math.sqrt( dx\*dx + dy\*dy ) local objectSize = (obj2.contentWidth/3) + (obj1.contentWidth/3) if ( distance\< objectSize+3) then return true end return false end

There have error when I press the button after the bullet collided with the enemy and the next enemy haven’t spawn

Hi,

some points:

  1. You’re creating a Runtime listener for each enemy in the function Enemy which doesn’t seem to do anything.

  2. You’re using global variables. Use local variables instead. Assign them at the top of the code.

  3. Instead of using your own collision detection I would use Coronas built-in.

Here is an optional version that I made:

require ("physics") physics.start() physics.setGravity(0,0) -- Local variables local shootButton = display.newRect(200,100,50,50) local weapon = display.newCircle(100,100,50,50) -- The shot comes from here -- Creates the bullet when shootButton is being pressed. local function createBullet() local bullet = display.newCircle(weapon.x, weapon.y, 5,5) bullet.name = "bullet" -- Will not be used in this example but in the collision listener you can check this name to see if a collision occurs between an enemy and a bullet bullet:setFillColor(1,0,0) physics.addBody(bullet, "dynamic") bullet:setLinearVelocity(0,100) -- Together with physics we can now move the bullet like this. end local counter = 0 local function enemyLoop() if counter % 90 == 0 then -- Create an enemy every 3 seconds (of the FPS is set to 30) local enemy = display.newRect(weapon.x,500,40,40) enemy:setFillColor(0,1,0) enemy.name = "enemy" physics.addBody(enemy, "dynamic") enemy:setLinearVelocity(0, -100) end counter = counter + 1 end local function onCollision(event) -- When object1 and object2 collides we should remove them both (i.e. the bullet and the enemy) event.object1:removeSelf() event.object2:removeSelf() end -- Event listeners shootButton:addEventListener("tap", createBullet) Runtime:addEventListener("collision", onCollision) Runtime:addEventListener("enterFrame", enemyLoop)

Best regards,

Tomas

I have a 200*200 sprite and I use the xscale and yscale ,the enemy is remove but the bullet is not overlap with the enemy

could you tell me where / what line this error is on because i am having trubble reading your code

 thanks:

Sean:

from Boxing Studio Games

function hasCollided( obj1, obj2 ) if ( obj1 == nil ) then --make sure the first object exists return false end if ( obj2 == nil ) then --make sure the other object exists return false end local dx = obj1.x - obj2.x local dy = obj1.y - obj2.y local distance = math.sqrt( dx\*dx + dy\*dy ) local objectSize = (obj2.contentWidth/3) + (obj1.contentWidth/3) if ( distance\< objectSize+3) then return true end return false end

There have error when I press the button after the bullet collided with the enemy and the next enemy haven’t spawn

Hi,

some points:

  1. You’re creating a Runtime listener for each enemy in the function Enemy which doesn’t seem to do anything.

  2. You’re using global variables. Use local variables instead. Assign them at the top of the code.

  3. Instead of using your own collision detection I would use Coronas built-in.

Here is an optional version that I made:

require ("physics") physics.start() physics.setGravity(0,0) -- Local variables local shootButton = display.newRect(200,100,50,50) local weapon = display.newCircle(100,100,50,50) -- The shot comes from here -- Creates the bullet when shootButton is being pressed. local function createBullet() local bullet = display.newCircle(weapon.x, weapon.y, 5,5) bullet.name = "bullet" -- Will not be used in this example but in the collision listener you can check this name to see if a collision occurs between an enemy and a bullet bullet:setFillColor(1,0,0) physics.addBody(bullet, "dynamic") bullet:setLinearVelocity(0,100) -- Together with physics we can now move the bullet like this. end local counter = 0 local function enemyLoop() if counter % 90 == 0 then -- Create an enemy every 3 seconds (of the FPS is set to 30) local enemy = display.newRect(weapon.x,500,40,40) enemy:setFillColor(0,1,0) enemy.name = "enemy" physics.addBody(enemy, "dynamic") enemy:setLinearVelocity(0, -100) end counter = counter + 1 end local function onCollision(event) -- When object1 and object2 collides we should remove them both (i.e. the bullet and the enemy) event.object1:removeSelf() event.object2:removeSelf() end -- Event listeners shootButton:addEventListener("tap", createBullet) Runtime:addEventListener("collision", onCollision) Runtime:addEventListener("enterFrame", enemyLoop)

Best regards,

Tomas

I have a 200*200 sprite and I use the xscale and yscale ,the enemy is remove but the bullet is not overlap with the enemy