在调用onComplete之前,我需要检查“regalo”是否为nil,因为有时会被碰撞事件删除,当我调用onComplete = removeRegalo时,它会返回错误nil值。
错误:尝试调用方法'removeSelf'(零值)
有什么想法吗?
local function removeRegalo(event)
event:removeSelf()
event = nil
end
local function tiraregalo()
regalo = display.newImageRect("images/regalo.png", 30, 30)
regalo.x = ship.x
regalo.y = ship.y
regalo:toFront()
regalo.name = "regalo"
physics.addBody( regalo, {isSensor = true } )
grupoCasas:insert(regalo)
local wind = 10
transition.to(regalo,{time=1500, y = screenH + 30, x = regalo.x + wind,rotation= math.random(-20,60), onComplete=removeRegalo})
end
function onCollision( event )
if(event.object1.name == "casa" and event.object2.name == "regalo") then
display.remove( event.object2 )
end
end
答案 0 :(得分:1)
local function removeRegalo(event)
if event == nil then return end
event:removeSelf()
-- event = nil Not really needed, but okay if you want it here.
end
答案 1 :(得分:0)
这样的事情会起作用吗?
if regalo ~= nil then
-- object exists so do some code
end
希望有所帮助:)
答案 2 :(得分:0)
也许这可以胜任。
local function removeRegalo(event)
-- Check if event:removeSelf exists
if event:removeSelf then
event:removeSelf()
event = nil
end
end
答案 3 :(得分:0)
以下是解决方案:
local function removeRegalo(event)
display.remove( event )
event = nil
end
event = nil - 这是可选的,因为@ 111WARLOCK111说。 谢谢!
答案 4 :(得分:0)
local function removeRegalo(event)
local tempObject=event.target
if tempObject then
tempObject:removeSelf()
tempObject = nil
end
end