Problems with touch event and counter

Hi there,

I’m making a game with a small boss. My boss has 5hp.

I set a touch event listener to my boss. And I want to reduce my boss’hp by 1 each time the player touches or slides his finger on the boss. But the hp of my boss goes down to 0. Immediately since the touch event triggers the code multiple times.

[lua]local function bossHit(event)
   if ( event.phase == “began” or event.phase == “moved” or event.phase == “ended” ) then
   bossHP = bossHP -1
   end
end

boss:addEventListener( “touch”, bossHit )[/lua] 

Is there a way the counter just go down by 1 each time the event it’s trigger?

Thanks a lot for your help.

How does it work if you only use “began” like below?

  1. local function bossHit(event)
  2.    if ( event.phase == “began”) then
  3.    bossHP = bossHP -1
  4.    end
  5. end
  6. boss:addEventListener( “touch”, bossHit )

Or something like this:

  1. local bossTouched= false
  2. local function bossHit(event)
  3.    if ( bossTouched== false and (event.phase == “began” or "eventPhase == “moved”) ) then
  4.      bossHP = bossHP -1
  5.      bossTouched= true
  6.    end
  7.    if ( event.phase == “ended”) then
  8.      bossHP = bossHP -1
  9.      bossTouched = false
  10.    end
  11. end
  12. boss:addEventListener( “touch”, bossHit )

This won’t work, however, if an “ended” event is not happening for every time your finger slips of the boss. Not sure if this can happen…

I think the “began” phase alone would be best. This way, whether the user “taps” on the boss OR touches the boss and slides the touch off, the event will only trigger once… and if the player touches somewhere off the boss and then slides the touch on, then nothing will happen (which seems logical to me, since the point is to actually touch the actual boss, not slide the touch onto the boss from somewhere else).

Brent

Thanks for your help both runewinse and Brent Sorrentino.

If I just use the “began” phase the event will only trigger when the player touches directly the boss. I want the event triggers as well when the player slides the touch on the boss since its kind of a slicing game.

Any hint on how to achieve that?

Once again thanks a lot for your help.

Hi @betofantasioso,

The “began” phase should work for a slicing motion too except if you consider a “slice” as beginning somewhere outside of the boss, then moving onto the boss. In this case, you will probably need to implement the “moved” phase as well, but combine it with variable flags/properties on the boss, such that you detect only the first"moved" event on him (or the “began” phase instead). It’s definitely possible, and not too complicated, but it takes a bit more effort.

Brent

Actually, the function that @runewinse presented looks to be a good start to this, but it would need some extra stuff. First, on line 4, it looks like a small typo needs fixing, so change it to:

[lua]

if ( bossTouched== false and (event.phase == “began” or event.phase == “moved”) ) then

[/lua]

Then, as noted, the “ended” event won’t occur if the touch ends outside of the boss. So, you’d probably need to detect the overall state of the user’s touch, and then sense the “ended” phase on the overall Corona stage, and when that occurs, reset “bossTouched” to false. And also, in that case, lines 9-12 could be removed.

Brent

How does it work if you only use “began” like below?

  1. local function bossHit(event)
  2.    if ( event.phase == “began”) then
  3.    bossHP = bossHP -1
  4.    end
  5. end
  6. boss:addEventListener( “touch”, bossHit )

Or something like this:

  1. local bossTouched= false
  2. local function bossHit(event)
  3.    if ( bossTouched== false and (event.phase == “began” or "eventPhase == “moved”) ) then
  4.      bossHP = bossHP -1
  5.      bossTouched= true
  6.    end
  7.    if ( event.phase == “ended”) then
  8.      bossHP = bossHP -1
  9.      bossTouched = false
  10.    end
  11. end
  12. boss:addEventListener( “touch”, bossHit )

This won’t work, however, if an “ended” event is not happening for every time your finger slips of the boss. Not sure if this can happen…

I think the “began” phase alone would be best. This way, whether the user “taps” on the boss OR touches the boss and slides the touch off, the event will only trigger once… and if the player touches somewhere off the boss and then slides the touch on, then nothing will happen (which seems logical to me, since the point is to actually touch the actual boss, not slide the touch onto the boss from somewhere else).

Brent

Thanks for your help both runewinse and Brent Sorrentino.

If I just use the “began” phase the event will only trigger when the player touches directly the boss. I want the event triggers as well when the player slides the touch on the boss since its kind of a slicing game.

Any hint on how to achieve that?

Once again thanks a lot for your help.

Hi @betofantasioso,

The “began” phase should work for a slicing motion too except if you consider a “slice” as beginning somewhere outside of the boss, then moving onto the boss. In this case, you will probably need to implement the “moved” phase as well, but combine it with variable flags/properties on the boss, such that you detect only the first"moved" event on him (or the “began” phase instead). It’s definitely possible, and not too complicated, but it takes a bit more effort.

Brent

Actually, the function that @runewinse presented looks to be a good start to this, but it would need some extra stuff. First, on line 4, it looks like a small typo needs fixing, so change it to:

[lua]

if ( bossTouched== false and (event.phase == “began” or event.phase == “moved”) ) then

[/lua]

Then, as noted, the “ended” event won’t occur if the touch ends outside of the boss. So, you’d probably need to detect the overall state of the user’s touch, and then sense the “ended” phase on the overall Corona stage, and when that occurs, reset “bossTouched” to false. And also, in that case, lines 9-12 could be removed.

Brent