Iphone touch vs drag

Previous topic - Next topic

marovada

Hi,

There are a few clues about this around the forum but I haven't been able to put it together. I have a game where:

- the player sprite moves across the screen with a 'drag' across the screen by the user
- in GLB, I'm using MOUSEAXIS to move the player sprite using the horizontal velocity
- there is another sprite for an action (eg shooting) that the user taps to trigger the action

In Objective-C, you can determine with code whether there is a touch or drag and only move the player on a drag, not a touch. I'm worried that the with GLB the MOUSEAXIS value will continue to move the player sprite even when the user taps the action sprite. The user may be using two fingers and that requires recognition of multitouch as well (GETMOUSECOUNT).

Sorry if I've got this all mixed up. Can someone please help me with this?

Thanks




bigtunacan

GETMOUSECOUNT will track how many touches you currently have.  On a touch down event a new mouse count is assigned and continues to track to the same touch until the up event.  So you need to get your mouse count then step through logic for each one using SETACTIVEMOUSE.

There is no drag event, instead you will capture the mouse down for an active mouse (touch) and it's initial location using MOUSESTATE.  Set some variable to indicate the mouse down/up state and x/y coordinate and each frame update your sprites position based off of the difference between the current position and last position.

Here is a function I use to do something like this.

Code (glbasic) Select

GLOBAL m_old_x, m_off_x
GLOBAL m_old_y, m_off_y

FUNCTION SetMouseOffset: x, y
IF x > m_old_x
m_off_x = x - m_old_x
ELSE
m_off_x = (-1)*(m_old_x - x)
ENDIF

IF y > m_old_y
m_off_y = y - m_old_y
ELSE
m_off_y = (-1)*(m_old_y - y)
ENDIF
ENDFUNCTION


I call this each frame update and move my selected object by m_off_x, m_off_y.
In my case I'm only interested in a single touch/mouse event at a time so I always am going to get the MOUSESTATE for mouse 1.  If you want to support multiple touches just change the offsets to be arrays sized to the number of touches you wish to support instead.  Now you loop through all where the mouse is down to find your delta values.  Track your down/up state for each mouse, this way when the initial touch down event occurs you can clear previous offsets so you don't end up with an initial jittery/jerky motion from prior touch values.


marovada

Thanks. This is a great idea (pity about no drag event but hey  =D)

So:

- I'll not use MOUSEAXIS to move the player
- I'll track touch down and move via offset until there is a touch up event
- if touch down co-ordinates are on action button, then do action and not move player
- given touch down on action button may be on another mouse (ie multitouch) track at least one other mouse (the two are interchangeable ie either mouse could be used for either action)
- I'll reset offset at each touch up

Cool.

marovada

Also is there a way of detecting a touch on a sprite with a single function?

Otherwise it would be necessary to determine in each case whether the touch is within the current area of the sprite - much harder than for native api functions.


bigtunacan

Just use BOXCOLL and pass in the mouse x/y coord and then use 1 for it's width/height.  Then pass in the x,y and width,height values of your sprite.

BOXCOLL(xa%, ya%, sxa%, sya%, xb%, yb%, sxb%, syb%)

marovada

Thanks again bigtunacan!

ampos

Take a look at my ZONES lib at CODE SNIPETS subforum.