Lua代码中的对象未响应点击事件。

我正在制作一个简单的游戏,在游戏中玩家可以点击一个随机移动的球来获得分数。然而,当我测试我的游戏时,我发现点击功能没有正常工作。当我试图点击移动的球时,有时分数会增加,但有时没有反应。以下是我使用的代码。感谢帮助!

    local ball = display.newImage(“ball.png”)
        球。x = math.random0,w)
        球。y = math.random0,h)
        物理。addBody(球)

    function moveRandomly()
    ballsetLinearVelocitymath.random(-50,50),math.random(-50,50));
    end
    计时器。performWithDelay(500,moveRandomly,0);

    球。旋转= 180
    当地= 1

    当地函数 rotateball()
        如果(reverse == 0)则
            reverse = 1
            transition.to(球,{旋转= math.random(0,360),time = 500,transition = easing.inOutCubic })
        否则
            reverse = 0
            transition.to(球,{旋转= math.random(0,360),time = 500,transition = easing.inOutCubic })
        结束
    end

    计时器。performWithDelay(500,rotateball,0)

    local myTextchangeTextscore

    得分= 0

    function changeTextevent)
        得分=得分+ 1
        printscore ..“taps”)
        myText.text =“Score:”..score
        返回true
    end

    myText = display.newText(“Score:0”,w,20,Arial,15)
        myTextsetFillColor(0,1,1)

    myTextaddEventListener(“tap”,changeText)
    球。addEventListener(“tap”,changeText
点赞
用户1440756
用户1440756

你的代码看起来很好,尤其是因为你说它有时可以工作。

我能想到的唯一可能是,也许你应该使用 "touch" 事件而不是 "tap" 事件,因为 "tap" 事件不会触发,直到你将手指从屏幕上拿开。请参见https://docs.coronalabs.com/guide/events/touchMultitouch/index.html

ball:addEventListener("touch", changeText)

你只需要使用事件的 "began" 阶段,这样它只会在触摸开始时增加分数(每次只增加一次)。

function changeText( event )
    if ( event.phase == "began" ) then
        score = score + 1
        print( score.."taps")
        myText.text = "Score:" ..score
    end
    return true
end
2015-12-16 17:58:08