简体   繁体   English

破坏Corona SDK上的已生成对象

[英]Destroy spawned objects on touch Corona SDK

I want destroy objects by touching them that I spawn every 1 sec randomly in the screen. 我想通过触摸我每隔1秒钟在屏幕上随机生成的物体来销毁它们。 I wrote something but it destroys only the first object that spawns. 我写了一些东西,但它只会破坏产生的第一个对象。 I am very noob at this things, someone can help me please? 我在这件事上非常菜鸟,有人可以帮我吗? I wrote this code: (thunder is my object) 我写了这段代码:(雷电是我的对象)

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)

I solved my problem with this: 我用这个解决了我的问题:

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)

What ever the code you have is correct, just do this one modification. 无论您使用的代码是正确的,只需进行一次修改即可。

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM