Game Development Community

Move Object To Position of MouseClick

by rennie moffat · in Torque Game Builder · 11/20/2009 (3:06 pm) · 5 replies

Hi there, I am working on this code, something I was attempting to do before, and I have been away for the part of a week so my skills are a little rusty.

What I am trying to do is move an object (which behavior is attached to) to whatever worldPos the mouse clicks on the sceneWindow2d.

So on mouse up, object moves to where ever mouse up occured, yet I currently can't get it to work. If someone could give me some help it would be appreciated. Thank you.



if (!isObject(moveToMouseClickPosBehavior))
{
   %template = new BehaviorTemplate(moveToMouseClickPosBehavior);
   
   %template.friendlyName = "Object move to Mouse pos when clicked";
   %template.behaviorType = "Input";
   %template.description  = "Set the object to move to the position of the mouse click";

   %template.addBehaviorField(speed, "Speed of object", float, 30);
  }

function sceneWindow2d::onBehaviorAdd(%this)
{
        ////i am unsure of a couple of things here, One, if my class is correct and if I should setUseObjectMouseEvents(%status)
	//// I am unsure if I should have two classes, one scenewindow2d::onBehaviorAdd and moveToMouseClickBehavior::onBehaviorAdd
        %this.setMouseEvents(true);
}

function sceneWindow2d::onMouseUp(%this, %modifier, %worldPos, %clicks)
{
        /// moveTo uses targetX and targetY. I am slightly unsure if these are defined correctly.
	%this.owner.moveTo(%worldPos.x, %worldPos.y, %speed);
}

About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#2
11/20/2009 (3:21 pm)
Thanks, I could not find this thread. I found more elusive ones, see if this helps.

thanks.
#3
11/20/2009 (5:11 pm)
I have tried this code but am unable to get it to work. If some one can help me out I would appreciate it.

Thanks
Ren


if (!isObject(moveToMousePosOnUp))
{
   %template = new BehaviorTemplate(moveToMousePosOnUp);
   
   %template.friendlyName = "Object move to Mouse pos when clicked";
   %template.behaviorType = "Input";
   %template.description  = "Set the object to move to the position of the mouse click";

   %template.addBehaviorField(speed, "Speed of object", float, 30);
  }

function moveToMousePosOnUp::onBehaviorAdd(%this)
{
	///this to insure sceneWindow passes mouse events to an object(hero)
	if(!sceneWindow2D.getUseObjectMouseEvents())
	{
		sceneWindow2D.setUseObjectMouseEvents(true);
	}
	
	///object/owner use mouse events	
	%this.owner.setUseMouseEvents(true);
}

function sceneWindow2D::onMouseUp(%this, %worldPos, %clicks, %modifier)
{
	///set worldPos to = newTarget
	%worldPos = %this.newTarget;
}


function moveToMousePosOnUp::newTarget(%this, %worldPos)
{
	if(!isObject(%this.owner))
	return;
	
	%this.owner.moveTo(%targetX, %targetY, %speed, true, false, true, 0.1);  
    %targetX = getWord(%this.newTarget, 0);
    %targetY = getWord(%this.newTarget, 1);
    %speed = %this.speed;
}





#4
11/20/2009 (5:18 pm)
A slightly modified version.

if (!isObject(moveToPosOnUp))
{
   %template = new BehaviorTemplate(moveToPosOnUp);
   
   %template.friendlyName = "Object move to Mouse pos when clicked";
   %template.behaviorType = "Input";
   %template.description  = "Set the object to move to the position of the mouse click";

   %template.addBehaviorField(speed, "Speed of object", float, 30);
  }

function moveToPosOnUp::onBehaviorAdd(%this)
{
	///this to insure sceneWindow passes mouse events to an object(hero)
	if(!sceneWindow2D.getUseObjectMouseEvents())
	{
		sceneWindow2D.setUseObjectMouseEvents(true);
	}
	
	///object/owner use mouse events	
	%this.owner.setUseMouseEvents(true);
}

function sceneWindow2D::onMouseUp(%this, %worldPos, %clicks, %modifier)
{
	///set worldPos to = newTarget
	%worldPos = %this.newTarget;
}


function moveToPosOnUp::newTarget(%this, %worldPos)
{
	if(!isObject(%this.owner))
	return;
	
	%this.owner.moveTo();
}
	
function moveToPosOnUp::moveTo(%targetX, %targetY, %speed)
{
     %targetX = getWord(%this.newTarget, 0);
     %targetY = getWord(%this.newTarget, 1);
     %speed = %this.speed;
}





#5
11/20/2009 (11:17 pm)
So when you put to use all of the debugging tips you were given, what problems did you see?