简体   繁体   English

Corona Sdk-删除一个具有相同名称的对象实例

[英]Corona Sdk - Removing one instance of objects with the same name

I'm making a space game using Corona Sdk and one of the functions in my code is used to fire laser beams. 我正在使用Corona Sdk进行太空游戏,并且代码中的功能之一用于发射激光束。 These beams are supposed to disappear when they finish their transition, but I have one problem: when I fire more than one at the same time (using a button widget(one per click)) only the last one fired disappears, right after the first one finishes its transition. 这些光束应该在完成过渡后消失,但是我有一个问题:当我同时发射多个光束(使用按钮小部件(每次单击一次))时,最后发射的光束消失了,就在第一个光束之后一个人完成了过渡。

This is my code right now: 这是我现在的代码:

local function removeLaser(event)
    --[[
    this doesn't work -> display.remove(laser)
    this returns an error (main.lua:34: attempt to call method 'removeSelf' (a
    nil value)) -> laser.removeSelf()
    --]]
end

local function fire(event)
    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

What should I do about the function removeLaser(event) ? 我应该如何处理function removeLaser(event)

Just put the removeLaser into fire function: 只需将removeLaser置于fire函数中:

local function fire(event)
    local laser=display.newImageRect("laser.png",75,25) -- always declare objects as locals
    laser.x=spaceship.contentWidth+spaceship.x/2+3
    laser.y=spaceship.y 

    local function removeLaser(target)  -- `onComplete` sends object as a parameter
        target:removeSelf()
        target = nil
    end

    transition.to(laser,{time=1000,x=display.contentWidth, onComplete = removeLaser})
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM