Game Development Community

Movement behavior-dynamic animation setting depending on face TGB

by michael bailey · 03/19/2009 (12:06 am) · 1 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 :)

$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);     

}

#1
10/23/2009 (8:37 am)
Thank you sooooo much... but how or what do I do to get my anamations in there? I saw the prefix and stuff but I don't exactlly get it... forgive me Im a n00b :)