"Double tapping" an object help

Hey Corona Users! So I have this:

function moleHit:tap(e)     lastmole.isVisible = false local t = e.target if t.type == "mole" then audio.play(hit) molesHit = molesHit + 50 moleHit = display.newText('+50', 380, 45, native.systemFontBold, 18) moleHit:setTextColor(255, 0, 0) transition.to( moleHit, { time=800, alpha=.01 } ) score.text = molesHit  end

That basically is my hit function for a mole that pops up and once you hit it, you score 50 points. Okay what I need help is on this, What if I want my mole to be “hit” twice before you can gain the 50 points, how would I go about that, any ideas? thanks and happy coding!

You can check event.numTaps for “tap” events.

What ingmar told you is what people need when they want to “double tap” an object, but I don’t think that’s what you mean.

So when you create your mole, add a property to it like this:

mole.hitNum = 0 

…and then change your code (from above) to look like this:

local t = e.target if t.type == "mole" then audio.play(hit) t.hitNum = t.hitNum + 1 if t.hitNum \> 1 then molesHit = molesHit + 50 moleHit = display.newText('+50', 380, 45, native.systemFontBold, 18) moleHit:setTextColor(255, 0, 0) transition.to( moleHit, { time=800, alpha=.01 } ) score.text = molesHit t.hitNum = 0 end end

Basically, you’re just keeping track of the number of times you hit the mole and when you’ve reached that limit, do the scoring.

The last line of that inner if/then block also resets the hitNum property back to zero but if you’ll never need to whack that mole again you can skip that.

 Jay

@JAWhye

Yeah, you’re correct. Double-tapping is different than tapping twice. Tapping twice is most likely what the OP is after…

you want to have 

if e.phase == “ended” then

if t.type == “mole” then
audio.play(hit)
molesHit = molesHit + 50
moleHit = display.newText(’+50’, 380, 45, native.systemFontBold, 18)
moleHit:setTextColor(255, 0, 0)
transition.to( moleHit, { time=800, alpha=.01 } )
score.text = molesHit 

end

end

bracketed around the code block, otherwise you are likely to tap mole once and the event will fire multiple times on that one touch … which you do not want in this situation.

If you’re using a touch event, then yes, you need to worry about the phase. If you are using a tap event, then phase doesn’t enter into it. Jay

Personally I’d choose to use “touch” events for objects that require a quick reaction, otherwise you can experience a bit of a lag.

Also I’d trigger on the “began” phase instead of “ended”

@J.A. Whye, for the mole.hitNum = 0 property, would that be inserted into the hit function or seperately as its own function? thanks for your replies everyone!

I’d put that right in with the code that creates the mole. Something like:

local mole = display.newImage("mole.png") mole.hitNum = 0 ...more stuff here...

Yeah I got it thanks! However, what happens is that the mole has to pop out twice before I get the points, my dilema was to hit it twice simeoutanesly before it “hides” again then I get that points, get me? @J.A. Whye Thanks A lot appreciate your contribution!

You can check event.numTaps for “tap” events.

What ingmar told you is what people need when they want to “double tap” an object, but I don’t think that’s what you mean.

So when you create your mole, add a property to it like this:

mole.hitNum = 0 

…and then change your code (from above) to look like this:

local t = e.target if t.type == "mole" then audio.play(hit) t.hitNum = t.hitNum + 1 if t.hitNum \> 1 then molesHit = molesHit + 50 moleHit = display.newText('+50', 380, 45, native.systemFontBold, 18) moleHit:setTextColor(255, 0, 0) transition.to( moleHit, { time=800, alpha=.01 } ) score.text = molesHit t.hitNum = 0 end end

Basically, you’re just keeping track of the number of times you hit the mole and when you’ve reached that limit, do the scoring.

The last line of that inner if/then block also resets the hitNum property back to zero but if you’ll never need to whack that mole again you can skip that.

 Jay

@JAWhye

Yeah, you’re correct. Double-tapping is different than tapping twice. Tapping twice is most likely what the OP is after…

you want to have 

if e.phase == “ended” then

if t.type == “mole” then
audio.play(hit)
molesHit = molesHit + 50
moleHit = display.newText(’+50’, 380, 45, native.systemFontBold, 18)
moleHit:setTextColor(255, 0, 0)
transition.to( moleHit, { time=800, alpha=.01 } )
score.text = molesHit 

end

end

bracketed around the code block, otherwise you are likely to tap mole once and the event will fire multiple times on that one touch … which you do not want in this situation.

If you’re using a touch event, then yes, you need to worry about the phase. If you are using a tap event, then phase doesn’t enter into it. Jay

Personally I’d choose to use “touch” events for objects that require a quick reaction, otherwise you can experience a bit of a lag.

Also I’d trigger on the “began” phase instead of “ended”

@J.A. Whye, for the mole.hitNum = 0 property, would that be inserted into the hit function or seperately as its own function? thanks for your replies everyone!

I’d put that right in with the code that creates the mole. Something like:

local mole = display.newImage("mole.png") mole.hitNum = 0 ...more stuff here...

Yeah I got it thanks! However, what happens is that the mole has to pop out twice before I get the points, my dilema was to hit it twice simeoutanesly before it “hides” again then I get that points, get me? @J.A. Whye Thanks A lot appreciate your contribution!