Double Tap to Override Single Tap Rotation

Hi, I’m new to Corona SDK (trying out trial version), and starting work on a small game.
I want the user to be able to tap once on an image to make it rotate. That’s not a problem. What I am stuck on is getting two taps to rotate the image in the opposite direction without also triggering the single tap response. Right now two taps makes it rotate both ways, so it ends up back where it started.

(This way of rotating an object is the simplest interaction I could think of for rotation - similar to Bubble Ball but with the ability to rotate both ways. But I’m open to suggestions).

The issue is that two taps always *also* triggers the one-tap response. I tried adding a
“return true” (now commented out) after the first double tap conditional but a double tap still always re-triggers the single tap response – apparently a two-tap is broken into both a two-tap and one-tap events. So the whole if-then-elseif always runs twice for a double tap.

function myImage.tap(self, event)
if event.numTaps == 2 then
myImage:rotate( 5 )
– return true
elseif event.numTaps == 1 then
myImage:rotate( -5 )
end
end

myImage:addEventListener(“tap”, myImage)
Thanks,
Eric
[import]uid: 128346 topic_id: 23247 reply_id: 323247[/import]

The trick would be to give your image a property for taps, eg;
myImage.taps = 0

Then on tap, tap = tap +1

Use a timer, a short one - and at the end of that check how many times the image has been pressed and respond accordingly. (Remember to also resets taps to 0 in this function.)

Does this make sense? Let me know if not, can try to simplify :slight_smile:

Peach [import]uid: 52491 topic_id: 23247 reply_id: 93225[/import]

Thanks Peach!

Well I was pulling my hair out for a little while trying to figure out in my head where to put the timer, but I went ahead and put it right inside the myImage.tap function, and it worked.

I don’t know if this is the best implementation of your suggested technique, or if it will scale or generalize, but given the crude state of my knowledge, I’m happy it’s working at least.

The drawback to this technique is that there is a slight delay before the object responds (while the timer does it’s delay and test).

If you have some suggestions how to improves this, let me know.
(I put all the code here)

–Eric

halfW = display.contentCenterX  
halfH = display.contentCenterY  
  
local myImage = display.newImageRect( "myImage.png", 150, 117 )  
-- put the image in the center of the screen   
myImage:translate( halfW, halfH )  
  
-- Create a taps property – must come after object is created, otherwise it's a null value  
myImage.taps = 0  
  
function myImage.tap(self, event)  
 function tapListener (event)  
 if myImage.taps == 2 then  
 myImage:rotate( 5 )  
 elseif myImage.taps == 1 then  
 myImage:rotate( -5 )  
 end  
 myImage.taps = 0  
 end  
 timer.performWithDelay( 200, tapListener, 1 )  
 myImage.taps = myImage.taps + 1  
end  
  
-- Add Event Listeners  
myImage:addEventListener("tap", myImage)  
timer.performWithDelay( 500, tapListener, 3 )  

[import]uid: 128346 topic_id: 23247 reply_id: 93434[/import]

I don’t believe you need line 26 in there.

Glad it is working - I ran it just now and it seems solid, delay to me isn’t really noticeable, only when looking carefully for it. (It’s only 1/5th of a second after all.)

Could make it shorter but really that might make it harder on a double tap. [import]uid: 52491 topic_id: 23247 reply_id: 93456[/import]

Cool. OK then, onto the next thing!
BTW I really appreciate the help. It’s what makes Corona SDK a great platform - having support!

(Oh yeah, you’re right, haha: I left line 26 in there from an earlier version. Thanks).

Yes, 200 milliseconds seems tolerable. Not desirable though.

If I wanted to add this same interaction to multiple objects (images or drawings), should I just use the same code for every object, or is there a way to generalize and re-use the code?

A (possibly) related question: I couldn’t find code anywhere in the forums or samples that does a double tap response. I would think this would be kind of a standard thing?
However, I found “Pinch Zoom XL” (http://developer.anscamobile.com/code/pinch-zoom-xl) which uses “touch” to detect taps, in the context of multi-touch. But it was a bit overwhelming to untangle and extract just the tap part.

I just tested Pinch Zoom X though (inserting a print or rotate statement at line 149 and 153), and it does not have a delay, which is great.

However it does the same thing of triggering both single and double taps when a double tap is detected. I’ll have to see if there’s a way to change that.

Would it be preferable in the long run to use “Touch” to get multi-tap responses? I am going to want other interactions, like drag and maybe others.

Thanks,
Eric [import]uid: 128346 topic_id: 23247 reply_id: 93555[/import]

You can bump down the 1/5th of a second delay but you’d want to allow enough time for a second tap, that would be trial and error. (I tried it at 150ms mind you and it was pretty tricky then.)

Pinch Zoom XL is third party code so it’s worth playing around with to figure out what works for you - if there is a way to minimize code, etc.

For the touch question, it wouldn’t be better for counting taps, no - it would be about the same - so if you prefer it then by all means use it.

Peach :slight_smile: [import]uid: 52491 topic_id: 23247 reply_id: 93677[/import]

I’d like an alternative to the delay if possible, so…

I’ve been playing around with this, using the touch event instead of tap, and using event.time to test for a double tap. And I am now clear on what the situation is:

It will always get a single tap because the program doesn’t know in the future it’s going to get a double tap.

It’s a dilemna (if you don’t want a delay for a tap), because you have to wait to see if a double tap happens before you respond to the single tap! (That’s how yours work - it waits to see).

This fellow seems to have found a solution in the context of his storyboards, but I’m not clear yet on how it works or if it applies:
http://developer.anscamobile.com/forum/2012/01/25/doubletap-storyboard

I’ll look at it again later when it’s not so late at night, and my head is more clear. In the meantime if you have any insights, let me know.

[I just looked at tit again: it does introduce a delay. Nevermind!]

Thanks.

Eric [import]uid: 128346 topic_id: 23247 reply_id: 95059[/import]