2015-08-15 82 views
0

我想通过触摸它们来销毁物体,这些物体随机在屏幕上每1秒产生一次。 我写了一些东西,但它只销毁了第一个产生的对象。 我很喜欢这个东西,有人可以帮助我吗? 我写了这个代码:(雷霆是我的对象)摧毁触摸产生的物体Corona SDK

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) 

回答

0

什么都你的代码是正确的,只是这样做一个修改。

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) 
0

我解决我的问题与此:

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)