Tap sometimes don't work for my game

I’m trying to create a simple game that involves a ball moving around randomly on the screen, and the player have to click it. Each time the player clicks it, they get 1 point. However, when I test out my code, the tap function did not go very well. When I click on the ball, sometimes I don’t get points and sometimes I do. Here is the code and can anyone check what is wrong with my code? thanks very much

local ball = display.newImage( “ball.png” )

ball.x = math.random(0,w)

    ball.y = math.random(0,h)

    physics.addBody( ball)

function moveRandomly()

ball:setLinearVelocity(math.random(-50,0), math.random(-50,50));

end

timer.performWithDelay(1000, moveRandomly, -1);

ball.rotation = 180

local reverse = 1

local function rotateball()

    if ( reverse == 0 ) then

        reverse = 1

        transition.to( ball, { rotation=math.random(0,360), time=500, transition=easing.inOutCubic } )

    else

        reverse = 0

        transition.to( ball, { rotation=math.random(0,360), time=500, transition=easing.inOutCubic } )

    end

end

timer.performWithDelay( 600, rotateball, 0 )

local myText, changeText, score

score = 0

function changeText( event )

score = score + 1

print( score…“taps”)

myText.text = “Score:” …score

return true

end

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

myText:setFillColor( 0, 1, 1)

myText:addEventListener( “tap”, changeText)

ball:addEventListener(“tap”, changeText)

Wheres your tap to remove function? Try this.

local function removeBall(event) if event.phase == "began" then local removeIt = event.target display.remove(removeIt) end end ball:addEventListener( "touch", removeBall )

–SonicX278

Thanks for your help, but I don’t want the ball to be removed after its clicked. Basically the score is how many times some one has clicked the ball. Yet, the ball is very hard to click on for some reason.

Can you make a sample project that I can put into a main.lua and play? How big is your ball?

–SonicX278

I think your problem is that you’re using a “tap” listener instead of a “touch” listener. Try this:

function changeText(event) if event.phase == "began" then score = score + 1 print(score .. " taps") myText.text = "Score: " .. score return true end end myText:addEventListener("touch", changeText) ball:addEventListener("touch", changeText)

A “tap” listener is triggered when the user touches a spot, doesn’t move their finger, and lifts it up from that spot. A “touch” listener allows for much more flexibility - with just a “began” phase, this listener’ll just listen for when the user touches their finger to the screen.

https://docs.coronalabs.com/guide/events/touchMultitouch/index.html

  • Caleb

Sonicx278 thanks very much! Caleb p has solve the problem. Thank both of you !!

Wheres your tap to remove function? Try this.

local function removeBall(event) if event.phase == "began" then local removeIt = event.target display.remove(removeIt) end end ball:addEventListener( "touch", removeBall )

–SonicX278

Thanks for your help, but I don’t want the ball to be removed after its clicked. Basically the score is how many times some one has clicked the ball. Yet, the ball is very hard to click on for some reason.

Can you make a sample project that I can put into a main.lua and play? How big is your ball?

–SonicX278

I think your problem is that you’re using a “tap” listener instead of a “touch” listener. Try this:

function changeText(event) if event.phase == "began" then score = score + 1 print(score .. " taps") myText.text = "Score: " .. score return true end end myText:addEventListener("touch", changeText) ball:addEventListener("touch", changeText)

A “tap” listener is triggered when the user touches a spot, doesn’t move their finger, and lifts it up from that spot. A “touch” listener allows for much more flexibility - with just a “began” phase, this listener’ll just listen for when the user touches their finger to the screen.

https://docs.coronalabs.com/guide/events/touchMultitouch/index.html

  • Caleb

Sonicx278 thanks very much! Caleb p has solve the problem. Thank both of you !!