It looks like you're new here. If you want to get involved, click one of these buttons!
Swipe = Core.class(Sprite) function Swipe:init() self:addEventListener(Event.TOUCHES_BEGIN, self.touchesBegin, self) self:addEventListener(Event.TOUCHES_MOVE, self.touchesMove, self) self:addEventListener(Event.TOUCHES_END, self.touchesEnd, self) end function Swipe:touchesBegin(event) self.startX = event.touch.x end function Swipe:touchesMove(event) end function Swipe:touchesEnd(event) if(event.touch.x > self.startX + 20) then print("swipe right") else if(event.touch.x < self.startX - 20) then print("swipe left"); end self.startX = nil end end |
Comments
Likes: Tom2012
What happens if the user has actually swiped upwards or downwards and has the x threshold more than 20? i.e. it was a slightly diagonal upwards or downwards swipe, it will still give a false positive of swipe left or right.
You will have to consider the Y swipe as well to see if the movement on the Y-axis was greater or not. Just in case you want to make it more accurate and might have to consider a couple of more scenarios to eliminate the false positives.
Likes: Tom2012
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
http://appcodingeasy.com/Gideros-Mobile/Detecting-Gestures-in-Gideros
@Talis, the gesture code is great, but only says it's a "Line," no matter which way I swipe.
I even added new gesture types, but only "A Line" is detected, no matter which way I swipe:
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
http://www.giderosmobile.com/forum/discussion/1513/trash-the-roadmap-please#Item_20
Dislikes: OZApps
Our delete button has been forgotten to be added to our holder.
self.stage:addchild(deletebutton)
:-B
Likes: OZApps
Here's the code I am using for my app that seems to work well.
well forgive me for stupid question. if i have a button / sprite . and i want to detect what kind of swipe is that ( is it right,left,up,or down ) using your class. how i do that ?
edited.. got it..
so i need to copy the class above to swipe.lua and edited a bit to :
and in my main.lua i do this :
I've got some improved code that I'm using on my game. I'll get it together and post it along with a sample game when I get back from the office this afternoon. :-)
Tom
thanks for the help. and rite now i have a bit problem.
i managed to make it detect where i swipe using your class . and lets say i swipe to right. i want the object ( image file ) move to left or right with some moving animation.
what i do now is that object teleport to A point to B point. but what i want is that object move with certain speed. I'm still checking some code available on this forum and tutorial. but still stuck a bit.. hopefully you can enlighten me a bit.
Thanks.
1) Box2D - you can use sprite:setLinearVelocity(xVel, yVel) -- this is a slightly complex method if you're doing a simple game like say space invaders or mario. But is an option.
2) The next option is the GTween library which lets you specify different things to be 'tweened'. So you can specify x or y to be tweened. Probably not ideal for a game with lots of swiping and movement.
3) Probably the best one would be to have an enterframe loop where movement is made each frame. So on the sprite, in the init part of the code put:
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
self.speed = 2
... then have
if(self:getX() < self.swipeX) then
-- make the x move
.. so your sprite will only move when it has not reached the swipe's final destination...
I've got this written up in an example now. Hope it helps.
http://www.giderosmobile.com/forum/discussion/2202/make-a-sprite-move-left-or-right-when-swiped#Item_1
thanks man..
btw some question. i try to play that in my device ( note 2 ). but it seems not really smooth comparing to the sample bird animation project on gideros..
but well its a progress for me.. thanks alot.
--------------------
btw man. i bump into your tutorial here
http://giderosmobile.com/forum/discussion/2122/make-a-character-move-to-a-mouse-or-touch-then-stop#Item_1
and when i try it quite smooth on my player and note2...
or just my eyes fooling on me.
and for your box2d solution. is it better solution ?
what i have in mind is lets say i have a object and if i swipe it left / right. ill will move till it collide with a wall / other object.
so what i need is :
1. to detect swipe left,right,up,down ( done )
2. move the object according to swipe ( left,right, etc ) ( done )
3. stop when hit wall / object.
so according to this. is it needed to use box2d or no need ?
@ar2rsawseen
About my previous classes I could not say that they are supported by the Gideros, because well even I don't have enough time to support them.
I always put ideas aside to do on the weekends, like update something etc, but recently, all the free time I spent on weekends I also spent on Gideros related work.
I totally understand that you are busy with lot of Gideros-related things.
My question was more like : If a new user -or a studio evaluating Gideros- wanted to build a prototype quickly, gestures recognition being fairly used in mobile apps, what would be the closest implementation?
From what I have seen, it's almost on everyone's todo list and people I pointed to Gideros are asking me about it, so I wish there was a link I could give everyone to get them started. (That would be, indeed, useful for me too).
Without updating anything, where would you redirect a studio currently evaluating Gideros?
http://giderosmobile.com/tools/gestures
because it covers from simple line gestures to more advanced ones, and for multi gesture combinations, here:
http://www.bowerhaus.eu/blog/files/multistroke_gestures.html
I can't make it work to recognize swipe up, down, left, right.
My questions are :
- What am I doing wrong?
- If I want some gestures to recognize allowRotation and others that don't, should I create several instances of the Gestures class?
- Wouldn't it be a good idea to provide beginners with "plug and play" code for swipe gestures? (at least a code sample)
thanksAnd it might take some time to re-understand mathematically overally complicated code which I wrote when I was beginner for both Lua and Gideros.
Ok @Mells, I see your point. I actually need to rework on every class/lib I have posted, because in most cases with current lua and Gideros knowledges I would be able to create more efficient, more flexibly designed and more error prone/foolproof code.
That is the only thing stopping me from adding them to official Gideros support, because they are not good enough by my current standards
After book project is finished I will try dedicating every weekend to one single library to rewrite and improve it and add it to Gideros repo (if @atilim won't mind)
But currently, if you need simple swipes and not complex figures, you maybe even better of using @Tom2012 or @adipose code