2015-12-19 3 views
0
local heli = display.newCircle(x_pos, y_pos, radius) 

local y_direction = 1 
local y_speed = 5 

local function animate(event) 
    y_pos = y_pos + (y_direction * y_speed); 
    if(y_pos > screen_bottom - radius) then 
     y_direction = y_direction * -1; 
     heli.fill = green_paint; 
    end 
    if(y_pos < screen_top + radius) then 
     y_direction = y_direction * -1; 
     heli.fill = red_paint; 
    end 
    heli:translate(x_pos - heli.x, y_pos - heli.y) 
end 

local function touchpop(self, event) 
    if(event.phase == "began") then 
     self:removeSelf() 
    end 
    return true 
end 

Runtime:addEventListener("enterFrame", animate); 
heli.onTouch = touchpop 
heli:addEventListener("touchpop", heli) 

Я пытаюсь удалить объект heli, когда он будет затронут. Но это не исчезает, когда я прикасаюсь к нему. Как убедиться, что затронутый объект исчезает?Удаление движущегося объекта в Короне

ответ

0

Это должно быть сенсорным и не onTouch:

heli.touch = touchpop 
heli:addEventListener("touch", heli) 

Вот еще один рабочий вариант, который я предпочитаю:

function touchpop(event) 
    -- Now we don't have self but instead of self we have event.target 
    local self = event.target 
    if(event.phase == "began") then 
     self:removeSelf() 
    end 
    return true 
end 

-- Here we need to add the function as the 2nd parameter 
heli:addEventListener("touch", touchpop) 

Дополнительная информация: https://docs.coronalabs.com/api/event/touch/index.html

Смежные вопросы