【问题标题】:Removing moving object in Corona移除 Corona 中的移动物体
【发布时间】:2016-03-25 23:40:25
【问题描述】:
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 对象时删除它。但是当我触摸它时它并没有消失。如何确保被触摸的物体消失?

【问题讨论】:

    标签: android lua coronasdk


    【解决方案1】:

    应该是touch而不是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

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 2021-07-10
      • 1970-01-01
      • 2014-03-25
      • 2016-08-20
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多