简体   繁体   中英

Removing moving object in Corona

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)

I am trying to remove the heli object when it is touched on. But it is not disappearing when I touch it. How to make sure the touched object disappears?

It should be touch and not onTouch :

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

Here is another working version that I prefer:

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 )

More information: https://docs.coronalabs.com/api/event/touch/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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