Movement behavior-dynamic animation setting depending on face TGB
by michael bailey · 03/18/2009 (5:06 pm) · 2 comments
the comment in code basically explains it but i will also here.
to use this behavior attach it to a isometric sprite (one that has 8 directions) set the prefix for the animation. lets say your character is mike so i would enter mike in the field. all animations i use for this would be mike_move_n that means its going to load the animation of mike moving northward. you need to use these naming conventions. or rewrite to achieve the desired effect. mike_idle_n would be an idle of me facing north. n, e, w, s, ne, nw, se, sw are the last parts to name your animation. hope this is useful.. if it is leave a comment :)
to use this behavior attach it to a isometric sprite (one that has 8 directions) set the prefix for the animation. lets say your character is mike so i would enter mike in the field. all animations i use for this would be mike_move_n that means its going to load the animation of mike moving northward. you need to use these naming conventions. or rewrite to achieve the desired effect. mike_idle_n would be an idle of me facing north. n, e, w, s, ne, nw, se, sw are the last parts to name your animation. hope this is useful.. if it is leave a comment :)
$lastDirection="n";
if(!isObject(pControlsBehavior))
{
///------------------------------------
/// to use the animation auto activate please use
/// prefix field (your spritename) in these formats
/// orc_move_ne
// orc_move_se etc for n , w , e, s, nw, ne, sw, se
// substitute move for idle ( when you stand still ))
//////////////////////////////////////////////////////////
%template = new BehaviorTemplate(pControlsBehavior);
%template.friendlyName = "Player Movement";
%template.behaviorType = "Input";
%template.description = "Movement control Rpg";
%template.addBehaviorField(upKey, "Key to bind to upward movement", keybind, "w");
%template.addBehaviorField(downKey, "Key to bind to downward movement", keybind, "s");
%template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "a");
%template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "d");
%template.addBehaviorField(AnimationPrefix, "Animation PreFix ex- orc", string, "animation prefix");
%template.addBehaviorField(verticalSpeed, "Speed when moving vertically", float, 15.0);
%template.addBehaviorField(horizontalSpeed, "Speed when moving horizontally", float, 30.0);
}
function pControlsBehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;
// bind our keys on the keyboard
moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %this);
moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
%this.up = 0; %this.down = 0; %this.left = 0; %this.right = 0; %this.dir="n";
}
function pControlsBehavior::moveUp(%this, %val)
{
%this.up = %val;
%this.updateMovement();
}
function pControlsBehavior::moveDown(%this, %val)
{
%this.down = %val;
%this.updateMovement();
}
function pControlsBehavior::moveLeft(%this, %val)
{
%this.left = %val;
%this.updateMovement();
}
function pControlsBehavior::moveRight(%this, %val)
{
%this.right = %val;
%this.updateMovement();
}
function pControlsBehavior::updateMovement(%this)
{
if(%this.right==1 && %this.up==1)
{
$lastDirection="ne";
}
else if(%this.right==1 && %this.down==1)
{
$lastDirection="se";
}
else if(%this.left==1 && %this.up==1)
{
$lastDirection="nw";
}
else if(%this.left==1 && %this.down==1)
{
$lastDirection="sw";
}
else if(%this.left==1 && %this.up==0 && %this.down==0)
{
$lastDirection="w";
}
else if(%this.left==0 && %this.right==0 && %this.down==1)
{
$lastDirection="s";
}
else if(%this.left==0 && %this.right==0 && %this.up==1)
{
$lastDirection="n";
}
else if(%this.left==0 && %this.right==1 && %this.up==0)
{
$lastDirection="e";
}
if(%this.left==0 && %this.right==0 && %this.up==0 && %this.down==0)
{
%idle="idle";
}else{
%idle="move";
}
echo(%this.AnimationPrefix @ "_" @ %idle @ "_" @ $lastDirection);
%this.owner.setAnimation(%this.AnimationPrefix @ "_" @ %idle @ "_" @ $lastDirection);
// figure out what speed in what direction we are moving based on key input
%this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
%this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}
#2
To get this together, just replace "animation prefix" with "mike"
%template.addBehaviorField(AnimationPrefix, "Animation PreFix ex- orc", string, "animation prefix");
and name all your animations
mike_idle_n
mike_idle_s
mike_move_n
mike_move_s
The line "%template.addBehaviorField(AnimationPrefix" is what tells the script what your animation's name is, the second is "idle" or "move" based on if they're moving or staying still, and the last variable is the direction you're facing. n, s, e, w, ne, nw, se, sw
And there's two underscores in it.
%animation prefix _ move _ direction
mike _ move _ direction
mike _ move _ n
mike_move_n = animation's name
THANK YOU!!!!!!!!!!!
08/06/2010 (5:44 pm)
Thank you!! :)To get this together, just replace "animation prefix" with "mike"
%template.addBehaviorField(AnimationPrefix, "Animation PreFix ex- orc", string, "animation prefix");
and name all your animations
mike_idle_n
mike_idle_s
mike_move_n
mike_move_s
The line "%template.addBehaviorField(AnimationPrefix" is what tells the script what your animation's name is, the second is "idle" or "move" based on if they're moving or staying still, and the last variable is the direction you're facing. n, s, e, w, ne, nw, se, sw
And there's two underscores in it.
%animation prefix _ move _ direction
mike _ move _ direction
mike _ move _ n
mike_move_n = animation's name
THANK YOU!!!!!!!!!!!
Torque Owner Robert Carroll