2014-07-03 72 views
0

我正在使用Corona Sdk制作空间游戏,并且我的代码中的一个功能用于激发激光束。这些光束在完成过渡时应该消失,但我遇到了一个问题:当我同时触发多个光束时(使用按钮控件(每次点击一个按钮)),只有最后一个触发消失,在第一个一个完成其过渡。Corona Sdk - 删除一个具有相同名称的对象实例

这是我的代码现在:

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 

我该怎么办有关function removeLaser(event)

回答

0

只要把removeLaserfire功能:

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 
相关问题