[英]Destroy spawned objects on touch Corona SDK
我想通過觸摸我每隔1秒鍾在屏幕上隨機生成的物體來銷毀它們。 我寫了一些東西,但它只會破壞產生的第一個對象。 我在這件事上非常菜鳥,有人可以幫我嗎? 我寫了這段代碼:(雷電是我的對象)
local thunder = display.newImageRect( "thunder_icon.png",100,100)
thunder.x = larghezza / 2
thunder.y = altezza / 2
local spawnTimer
local function myToccoListener(event)
display.remove(thunder)
return true
end
local spawn = function()
local xCoord = math.random(display.contentWidth * 0, display.contentWidth * 1.0)
local thunder = display.newImageRect( "thunder_icon.png",100,100)
thunder.x = xCoord
thunder.y = 50
thunder:addEventListener("touch", myToccoListener)
end
spawnTimer = timer.performWithDelay(1000, spawn, -1)
我用這個解決了我的問題:
local spawnTimer
local spawn = function()
local xCoord = math.random(display.contentWidth * 0, display.contentWidth * 1.0)
local thunder = display.newImageRect( "thunder_icon.png",100,100)
thunder.x = xCoord
thunder.y = 50
local function myToccoListener(event)
display.remove(thunder)
return true
end
thunder:addEventListener("touch", myToccoListener)
end
spawnTimer = timer.performWithDelay(1000, spawn, -1)
無論您使用的代碼是正確的,只需進行一次修改即可。
local thunder = display.newImageRect( "thunder_icon.png",100,100)
thunder.x = larghezza / 2
thunder.y = altezza / 2
local spawnTimer
local function myToccoListener(event)
-- display.remove(thunder)
if event.target then
event.target:removeSelf()
return true
end
end
local spawn = function()
local xCoord = math.random(display.contentWidth * 0, display.contentWidth * 1.0)
local thunder = display.newImageRect( "thunder_icon.png",100,100)
thunder.x = xCoord
thunder.y = 50
thunder:addEventListener("touch", myToccoListener)
end
spawnTimer = timer.performWithDelay(1000, spawn, -1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.