[英]Corona Sdk - Removing one instance of objects with the same name
我正在使用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)
?
只需將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.