我正在使用Corona制作一个太空游戏,我的代码中的一个函数是用来发射激光束的。这些光束应该在它们完成转换时消失,但是我有一个问题:当我同时发射不止一个(使用按钮小部件(每次点击一个))时,只有最后一个点火消失了,就在第一个小部件完成其转换之后。
这是我现在的代码:
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)
发布于 2014-07-03 18:23:35
只需将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
https://stackoverflow.com/questions/24556800
复制相似问题