Game Development Community

Alternative to onTouchDragged ?

by Chase Webb · in Torque 2D Beginner · 07/04/2013 (6:03 am) · 5 replies

Hey all, new question. So I've got my player object controlled via clicks: When the player clicks, the player object rotates and moves to the location. It looks like this:

function GameCore::MoveThePlayerCreature(%this, %touchID, %worldPosition)
{

if($MouseIsHeldDown){

    // Calculate the angle to the mouse.
    %origin = GameCore.ThePlayerObject.getPosition();
    %angle = -mRadToDeg( mAtan( %worldPosition.x-%origin.x, %worldPosition.y-%origin.y ) );
//    echo("The angle is " SPC %angle);
    //Rotate to the touched angle.
    
	if(GameCore.ThePlayerObject.Class $= "TriangleJObject"){
	GameCore.ThePlayerObject.RotateTo( (%angle +30), 100 , true, false);
	}
    
	if(GameCore.ThePlayerObject.Class $= "SquareJObject"){
	GameCore.ThePlayerObject.RotateTo( %angle, 200 , true, false );
	}
	
		if(GameCore.ThePlayerObject.Class $= "HexagonJObject"){
	GameCore.ThePlayerObject.RotateTo( %angle, 200 , true, false );
	}

		if(GameCore.ThePlayerObject.Class $= "creatureHeart"){
	GameCore.ThePlayerObject.RotateTo( %angle, 200 , true, false );
	}	

	%vectorangle = Vector2AngleToPoint( GameCore.ThePlayerObject.nodeEngine.getWorldCenter(), GameCore.ThePlayerObject.getWorldCenter()); 

	GameCore.ThePlayerObject.setLinearVelocityPolar(%vectorangle, 50); 
    GameCore.ThePlayerObject.MoveTo( %worldPosition, 50, false, false);
	
	schedule(128, 0, "GameCore::MoveThePlayerCreature", %this, %touchID, $currentWorldPosition); 
	}


}


function InputManager::onTouchDown(%this, %touchID, %worldPosition)
{

$currentWorldPosition = %worldPosition;
$MouseIsHeldDown = true;

GameCore::MoveThePlayerCreature(%this, %touchID, %worldPosition);

}

function InputManager::onTouchUp()
{   
$currentWorldPosition = %worldPosition;
	$MouseIsHeldDown = false;
}

function InputManager::onTouchDragged(%this, %touchId, %worldposition)
{    
$currentWorldPosition = %worldPosition;


}

This way I can drag my mouse and it updates the position, and when I unclick the mouse it stops moving and the function dies.

Here's my problem: If I click, and drag, but then stop the mouse while still holding down, the object moves to the last location before the mouse stopped moving and just wiggles there. I'd like it to keep updating where the object should go to according to where the mouse is located, even when it stops moving (aka it keeps going in that direction).

I looked through the commands in scenewindows.cc, but nothing seems to be exactly what I am looking for, or I don't know how to use some of them. Any suggestions? Either that, or a modified version of onTouchDragged that doesn't stop updating when the mouse stops moving.

#1
07/04/2013 (7:15 am)
onTouchDown() callback gets called once when you click the mouse and will never be called unless you release the mouse and click it again, which resulted in the player moving once to the position of the mouse click and then eventually stop moving, on the other hand onTouchDragged() will be called multiple times while you are dragging the mouse, that's why you should call
GameCore::MoveThePlayerCreature(%this, %touchID, %worldPosition);

inside onTouchDragged() not inside onTouchDown().

Sorry, and you don't need to schedule MoveThePlayerCreature() because it's already being called from onTouchDragged().
#2
07/04/2013 (8:04 am)
Sounds like you just want to poll the mouse position as long as a button is held - which nothing in the engine currently does.

I'm thinking you could use one of those new-fangled ScriptTickObjects (or whatever they're called) but now I can't recall if those are in T2D or T3D.... Anyway, if T2D has them, then create one and have it update your call in it's callback.
#3
07/04/2013 (10:53 pm)
If I understand you right, you want the object to keep moving past the last mouse location as long as the button is still held down. Is that right? If so, can you first also explain why you're rotating your player creature to face the world center and applying a linear force in that direction?
#4
07/06/2013 (2:02 am)
Still working on this one. Initial Responses:

Amjad: The MoveThePlayerCreature is a looping function that turns itself off once the Mouse comes up and $MouseIsHeldDown is set to false. OnTouchDragged only updates the Mouse's World Position.

I suppose another way to ask my question: Is there a way to get the coordinates of the mouse's current world position even when it's not being clicked?

Richard: Being able to attached some kind of object to the mouse might function just as well, but I don't know anything about ScriptTickObjects.

Joe: The first sentence, yes that is correct. The rotating isn't towards the world center, it is to face the object towards the mouse. The linear force is me testing out a propulsion system of movement... unfortunately it's proving to be less fun than regular old MoveTo style movement.
#5
07/06/2013 (2:11 am)
Solved! A search through the code (a lucky guess) revealed the existence of MySceneWindow.GetMousePosition(). MySceneWindow obviously being whatever you name your scene window. I replaced all instances of %worldposition from the mouse clicks with that, and now it keeps going even after I stop moving the mouse. Woot!