Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Tutorial - laser won't be removed on collision although display.remove is called
Started by Sheldonopolis Jul 17 2019 08:57 PM

4 replies to this topic
tutorial physics display

Best Answer Sheldonopolis , 15 August 2019 - 08:23 PM

I finally found what is wrong. I fired two lasers at the same time, nothing is wrong with the laser-removing code. Sorry if this topic wasted anyone's time.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Sheldonopolis

[GLOBAL: userInfoPane.html]
Sheldonopolis
  • Observer

  • 4 posts
  • Corona SDK

Hi guys,
 
I finished the tutorial and everything went well. Then I modified some code and found a problem.

Problem:

In the simulator, when the laser hits an asteroid, it will disappear either immediately or when it hits another asteroid ( so that's 2 asteroids in total before the laser disappears).

What I did:

1. I put laser related code into a "laser.lua"

2. In the "laser.lua" I created a laser:removeLaser method, in which the display.remove is called

3. In the collision listener in "game.lua", when a collision between a laser and an asteroid is detected, the laser:removeLaser is called. 

 

laser.lua:

local laser = {}
laser.laserTable = {}
laser.removeLaser = function(self, laserToRemove, index)
  if((not index) or (index <= 0)) then
    for i = #self.laserTable, 1, - 1 do
      if(self.laserTable[i] == laserToRemove) then
        index = i
        break
      end
    end
  end

  --here 'display.remove' is called
  display.remove( laserToRemove )

  table.remove(self.laserTable, index)
  laserToRemove = nil
end
--some other code --
return laser

game.lua:

local laser = require( "laser" )

local function findObjName(obj1, obj2, name1, name2)
  local result = {found = false}
  if(obj1.objName == name1 and obj2.objName == name2) then
    result.found = true
    result[name1] = obj1
    result[name2] = obj2
  elseif(obj1.objName == name2 and obj2.objName == name1) then
    result.found = true
    result[name1] = obj2
    result[name2] = obj1
  end
  return result
end

-- the collision listener.
local function onCollision(event)
  if event.phase == 'began' then
    local obj1 = event.object1
    local obj2 = event.object2
    local laserAndAsteroid = findObjName(obj1, obj2, 'laser', 'asteroid')
    local shipAndAsteroid = findObjName(obj1, obj2, 'asteroid', 'ship')
    if(laserAndAsteroid.found) then

    --here 'laser:removeLaser' is called
      laser:removeLaser(laserAndAsteroid.laser)

    --some other code--
    end
  end
end



[TOPIC: post.html]
#2

Sheldonopolis

[GLOBAL: userInfoPane.html]
Sheldonopolis
  • Observer

  • 4 posts
  • Corona SDK

Sorry for the code above, I did wrap them in the "lua" "/lua" tags, why aren't they formatted?



[TOPIC: post.html]
#3

Appletreeman

[GLOBAL: userInfoPane.html]
Appletreeman
  • Contributor

  • 444 posts
  • Corona SDK

Rather than wrap them in <lua> tags, highlight the code and press the blue <> code button in the toolbar to wrap them in code tags.



[TOPIC: post.html]
#4

Sheldonopolis

[GLOBAL: userInfoPane.html]
Sheldonopolis
  • Observer

  • 4 posts
  • Corona SDK



Rather than wrap them in <lua> tags, highlight the code and press the blue <> [code=auto:0] button in the toolbar to wrap them in code tags.

Thank you, Appletreeman! The code format looks good now.



[TOPIC: post.html]
#5

Sheldonopolis

[GLOBAL: userInfoPane.html]
Sheldonopolis
  • Observer

  • 4 posts
  • Corona SDK

  Best Answer

I finally found what is wrong. I fired two lasers at the same time, nothing is wrong with the laser-removing code. Sorry if this topic wasted anyone's time.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: tutorial, physics, display