Corona Sdk - 移除同名对象的一个实例

我正在使用Corona Sdk制作太空游戏,我的代码中有一个用于发射激光束的函数。这些激光束应该在完成过渡后消失,但是我有一个问题:当我同时发射多个激光束时(每次点击一个按钮小部件),只有最后一个被发射的会消失,正好在第一个完成其过渡后。

这是我现在的代码:

local function removeLaser(事件)
    - -[[
    这不起作用 - >display.remove(激光)
    这返回一个错误(main.lua:34:尝试调用方法'removeSelf'(a
    空值)->激光删除()
    - -]]
end

local function fire(事件)
    laser = display.newImageRect(“laser.png”,75,25)
    laser.x = spaceship.contentWidth + spaceship.x / 2 + 3
    laser.y = spaceship.y
    transition.to(laser,{time = 1000,x = display.contentWidth,onComplete = removeLaser})
end

local function createButton()
    buttonFire = widget.newButton
    {
        defaultFile =“buttonUNP.png”,
        overFile =“buttonP.png”,
        width = 130,
        height = 130,
        emboss = true,
        onPress = fire,
        id =“buttonFire”
    }
    buttonFire.x = display.contentWidth-buttonFire.contentWidth / 2-10
    buttonFire.y = display.contentHeight-buttonFire.contentHeight / 2-10
end

关于“function removeLaser(event)”应该怎么办?

点赞
用户1925928
用户1925928

只需要将 removeLaser 放入 fire 函数中:

local function fire(event)
    local laser=display.newImageRect("laser.png",75,25) -- 始终将对象声明为本地变量
    laser.x=spaceship.contentWidth+spaceship.x/2+3
    laser.y=spaceship.y

    local function removeLaser(target)  -- `onComplete` 将对象作为参数发送
        target:removeSelf()
        target = nil
    end

    transition.to(laser,{time=1000,x=display.contentWidth, onComplete = removeLaser})
end
2014-07-03 18:23:35