mouse down vs up
by rennie moffat · in iTorque 2D · 07/20/2009 (7:21 am) · 3 replies
I recognise the difference between these obviously, but I am toying with a Maze gameand how to make the player/hero move in accordance to user input. I have used mouseDraggable which the object just moves exactly where the mouse is, and Follow Mouse, which gives some velocity difference between mouse and hero, in other words, there is a nicer feel to the Follow Mouse. So my question is, how would I make Follow Mouse, only happen on Mouse down events?!
I am sure this will take some minor scripting, but I am not sure where to plug it in.
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(FollowMouseExBehavior))
{
%template = new BehaviorTemplate(FollowMouseExBehavior);
%template.friendlyName = "Follow Mouse Ex";
%template.behaviorType = "Mouse Input";
%template.description = "Set the object to follow the position of the mouse. This version can be " @
"restricted by world limits and will obey collisions.";
%template.addBehaviorField(followX, "Follow the mouse's X position", bool, true);
%template.addBehaviorField(followY, "Follow the mouse's Y position", bool, true);
%template.addBehaviorField(trackingSpeed, "The rate at which the object will move toward the mouse", float, 15.0);
}
function FollowMouseExBehavior::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}
function FollowMouseExBehavior::onUpdate(%this)
{
if (!isObject(sceneWindow2d))
return;
%mousePos = sceneWindow2D.getMousePosition();
%position = %this.owner.position;
%difference = t2dVectorSub(%mousePos, %position);
%amount = t2dVectorScale(%difference, %this.trackingSpeed);
if (%this.followX)
%this.owner.setLinearVelocityX(%amount.x);
if (%this.followY)
%this.owner.setLinearVelocityY(%amount.y);
}
I am sure this will take some minor scripting, but I am not sure where to plug it in.
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(FollowMouseExBehavior))
{
%template = new BehaviorTemplate(FollowMouseExBehavior);
%template.friendlyName = "Follow Mouse Ex";
%template.behaviorType = "Mouse Input";
%template.description = "Set the object to follow the position of the mouse. This version can be " @
"restricted by world limits and will obey collisions.";
%template.addBehaviorField(followX, "Follow the mouse's X position", bool, true);
%template.addBehaviorField(followY, "Follow the mouse's Y position", bool, true);
%template.addBehaviorField(trackingSpeed, "The rate at which the object will move toward the mouse", float, 15.0);
}
function FollowMouseExBehavior::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}
function FollowMouseExBehavior::onUpdate(%this)
{
if (!isObject(sceneWindow2d))
return;
%mousePos = sceneWindow2D.getMousePosition();
%position = %this.owner.position;
%difference = t2dVectorSub(%mousePos, %position);
%amount = t2dVectorScale(%difference, %this.trackingSpeed);
if (%this.followX)
%this.owner.setLinearVelocityX(%amount.x);
if (%this.followY)
%this.owner.setLinearVelocityY(%amount.y);
}
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
07/21/2009 (10:20 am)
The FollowMouseExBehavior::onUpdate(%this), function is where the object is actually moving (which will be slow, but maybe that's why you're changing it). Rename it and disable the onUpdate callback, then call that function from a mouse event, e.g.:function t2dSceneWindow::onMouseDown(%this) {
%object.FollowMouseExBehavior.updatePos();//rename from onUpdate()
}
#3
%object.FollowMouseExBehavior.updatePos();//rename from onUpdate()
}
function t2dSceneWindow::\
this is a function on any sprite, or scene object?
and then below you have FollowMouseExBehaviour, does this mean, this behavior, is only called when Mouse Down is activated?
07/21/2009 (3:32 pm)
function t2dSceneWindow::onMouseDown(%this) { %object.FollowMouseExBehavior.updatePos();//rename from onUpdate()
}
function t2dSceneWindow::\
this is a function on any sprite, or scene object?
and then below you have FollowMouseExBehaviour, does this mean, this behavior, is only called when Mouse Down is activated?
Associate Dave Calabrese
Cerulean Games
::onMouseDown
::onMouseUp
Give those a try and search through the engine code for more information about them. However, using those, you can do exactly what you want!