Game Development Community

Small Sprite Dragging Problem

by CharlesL · in iTorque 2D · 09/05/2012 (1:50 pm) · 6 replies

I have a small sprite that is supposed to follow the current touch position. Whenever I move the mouse(in the simulator) quickly, sometimes the mouse will leave the sprite, and the sprite will not move until I move the mouse back over it. Is there a way to to fix this? It also happens in all the examples.

#1
09/05/2012 (2:58 pm)
Alot of new people to torque seem to have this problem.

The problem is the dragged function does not poll fast enough to follow quick movements, so there is nothing you can do about it. It's a performance issue.

You will have to work around the issue. For example you can lock onto the sprite when player touches it and use a bigger drag area behind it.
#2
09/05/2012 (3:07 pm)
Thanks for the answer @Johnny Vo.

I've got two questions:
Is there a way of turning it into a global touch?
Is it good enough for making a joystick?
#3
09/05/2012 (3:14 pm)
What do you mean by global touch? If you mean dragging with more than one touche then thats a little tricky and I haven't done it myself yet.

If you are planning to make on screen joysticks, I believe torque has multiple touch support for drag. I haven't tried myself yet so I can't comment on it, but it looks doable. I'm actually still using a old engine.

Torques touch controls api is not super easy so you going to have to dig in and try to learn it.
#4
09/05/2012 (8:01 pm)
Are you going to have multiple touches or just one? If it's just one, you can use setMouseLocked(true) to lock the sprite to the touch (mouse). Do %this.setMouseLocked(true) in your onTouchDown function and then %this.setMouseLocked(false) in your onTouchUp function. You'll need some other code to handle if the user lifts their finger when it happens to be off of the sprite, though. The user might also see some wacky behavior if they happen to touch the screen with more than one finger. So this method is quick and dirty, but not necessarily ideal.

If you want to make it a global touch, you can enable mouse events on sceneWindow2D, which will enable the onTouchXXX functions for the screen. The touch functions on the device already kind of work like this, but enabling mouse events for the window will let you see the same behavior in the preview as well as on the device.

Personally, I've sort of solved this kind of problem myself by enabling touch for the screen and touch for my sprites. Then I have all of my sprites set to report if they're touched and my sceneWindow2D touch functions then handle the rest. The code for that ends up looking something like this:

//variable that denotes the last sprite touched
$activeSprite = null;

//sprite touch function
function sprite::onTouchDown( %this, %touchID, %worldPos )
{
    $activeSprite = this;
}

//screen touch functions
function sceneWindow2D::onTouchDragged( %this, %touchID, %worldPos )
{
    //do stuff here with $activeSprite, such as...
    if( $activeSprite != null )
    {
        $activeSprite.setPosition( %worldPos );
    }
}

function sceneWindow2D::onTouchUp( %this, %touchID, %worldPos )
{
    $activeSprite = null;
}

You can also make $activeSprite an array that sprites report to. That would allow you to use multitouch with them and move one around on each finger. That could look like this:

//active sprite array
$activeSprites[0] = null;

function nullSpriteArray()
{
    for( %i = 0; %i < 11; %i++ )
    {
        $activeSprites[%i] = null;
    }
}

nullSpriteArray();

//sprite reporting
function sprite::onTouchDown( %this, %touchID, %worldPos )
{
    $activeSprites[%touchID] = %this;
}

//screen touch functions
function sceneWindow2D::onTouchDragged( %this, %touchID, %worldPos )
{
   if( $activeSprites[%touchID] != null )
   {
       $activeSprites[%touchID].setPosition( %worldPos );
   }
}

function sceneWindow2D::onTouchUp( %this, %touchID, %worldPos )
{
    $activeSprites[%touchID] = null;
}
#5
09/05/2012 (10:47 pm)
I have found this problem as well with touchDrag.cs. In fact it gets so bad at times the objects stop responding to touch. It would be nice if this could be fixed, as really, it's a fundamental control mechanism to make iPod games right?
#6
09/06/2012 (10:06 am)
Thanks for taking time to answer my question, @Joe Williams, your answer helps a lot! I think I'll use the second example, because I do want to use some multitouch.

@Andy Hawkins, I agree. This really should be fixed.