Game Development Community

Help with animation or picture also button.

by Eliezer Cardona · in Game Design and Creative Issues · 04/28/2011 (8:58 pm) · 4 replies

1. I been practicing with Itorque2d and i been able to learn simple stuff. I am using the floursack that came for practice. I can make it into many cell and covert it into a animation. When it come down to I can only have it move in the screen with the code from rain. I can get the same image to to move but not change at the same moment so it seem that he is walking. Would I need to add a timer and for everystep? Would i need a timer? Any help would be helpful since I am a newbie to this.

I put the code underneath to show what i use to make the image move

2. My second thing is that I want to use a image as a button but i see that it use keys as well to other project. How would i convert a image to be my key to make anything have an action as also move. Do I need a actionlistener as java and timers?


Also thanks for looking in my thread and sorry if some one ask before so many pages to look and areas.



if (!isObject(MovementBehavior))
{
%template = new BehaviorTemplate(MovementBehavior);

%template.friendlyName = "Movement Behavior";
%template.behaviorType = "Movement Styles";
%template.description = "Moves a sprite around using a keyboard";
%template.addBehaviorField(upKey, "Key to bind to upward movement",keybind, "keyboard up");
%template.addBehaviorField(downKey, "Key to bind to downward movement", keybind, "keyboard down");
%template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "keyboard left");
%template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "keyboard right");
%template.addBehaviorField(verticalSpeed, "Speed when moving vertically", float, 20.0);
%template.addBehaviorField(horizontalSpeed, "Speed when moving horizontally", float, 20.0);
}
{
if (!isObject(moveMap))
{
echo("***ERROR: ActionMap moveMap does not exist!***");
return;
}

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;
}
function MovementBehavior::moveUp(%this, %val)
{
%this.up = %val;
%this.updateMovement();
}

function MovementBehavior::moveDown(%this, %val)
{
%this.down = %val;
%this.updateMovement();
}

function MovementBehavior::moveLeft(%this, %val)
{
%this.left = %val;
%this.updateMovement();
}

function MovementBehavior::moveRight(%this, %val)
{
%this.right = %val;
%this.updateMovement();
}
function MovementBehavior::updateMovement(%this)
{
%this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
%this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}

#1
04/29/2011 (6:29 am)
@Eliezer - The following code seems out of place:

{
if (!isObject(moveMap))
{
echo("***ERROR: ActionMap moveMap does not exist!***");
return;
}

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;
}

From your code, it is just floating in the script. It should be in the onBehaviorAdd function:

function MovementBehavior::onBehaviorAdd(%this)
{
}

#2
04/29/2011 (8:04 am)
Hi Michael can you give me a little more detail of how it would look like. Im new to this so im a little lost.


Also if I had a Image like button call up imagemap how would I link this up? to move the image instead of a key board?


Thanks for a reply
#3
04/29/2011 (9:50 am)
Here is the full script:

//---------------------------------------------------------------------------------------------
// iTorque 2D
// Copyright (C) GarageGames.com, LLC.
//---------------------------------------------------------------------------------------------

// Create this behavior only if it does not already exist
if (!isObject(MovementBehavior))
{
   // Create this behavior from the blank BehaviorTemplate
   // Name it MovementBehavior
   %template = new BehaviorTemplate(MovementBehavior);
   
   // friendlyName will be what is displayed in the editor
   // behaviorType organize this behavior in the editor
   // description briefly explains what this behavior does
   %template.friendlyName = "Movement Behavior";
   %template.behaviorType = "Movement Styles";
   %template.description  = "Moves a sprite around using a keyboard";
   
   // upKey will bind an input to the moveUp function
   // downKey will bind an input to the moveDown function
   // leftKey will bind an input to the moveLeft function
   // rightKey will bind an input to the moveRight function
   %template.addBehaviorField(upKey, "Key to bind to upward movement", keybind, "keyboard up");
   %template.addBehaviorField(downKey, "Key to bind to downward movement", keybind, "keyboard down");
   %template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "keyboard left");
   %template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "keyboard right");
   
   // verticalSpeed determines how quickly the cloud moves up and down
   // horizontalSpeed determines how quickly teh cloud moves left and right
   %template.addBehaviorField(verticalSpeed, "Speed when moving vertically", float, 20.0);
   %template.addBehaviorField(horizontalSpeed, "Speed when moving horizontally", float, 20.0);
}

// This function is called when this behavior is added
// to an object (aka owner)
function MovementBehavior::onBehaviorAdd(%this)
{
   // If we do not have our stock ActionMap, do nothing
   if (!isObject(moveMap))
   {
      // Print this error to the console
      echo("***ERROR: ActionMap moveMap does not exist!***");
      return;
   }
   
   // Bind our keys to the keyboard
   // bindObj(device, action, command, object)
   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);
  
   // up, down, left and right are dynamic variables
   // They are created for the first time here, with default values set to 0
   // These are essentially on/off flags used by the updateMovement function
   %this.up = 0;
   %this.down = 0;
   %this.left = 0;
   %this.right = 0;
}

// This function is called when this behavior or its owner
// is removed from the scene
function MovementBehavior::onBehaviorRemove(%this)
{
   // If we do not have our stock ActionMap, do nothing
   if (!isObject(moveMap))
   {
      // Print this error to the console
      echo("***ERROR: ActionMap moveMap does not exist!***");
      return;
   }
   
   // Stop the onUpdate callback for this behavior's owner
   %this.owner.disableUpdateCallback();
  
   // Remove the keybinds
   moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
   moveMap.unbindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), %this);
   moveMap.unbindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), %this);
   moveMap.unbindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);
 
   // Reset the dynamic flags for tracking current movement 
   %this.up = 0;
   %this.down = 0;
   %this.left = 0;
   %this.right = 0;
}

// Keybind triggered moveUp function
// %this - The MovementBehavior being used
// %val - Boolean that will be 1 (true) if the key was pressed down, 0 (false) if the key was released
// The variable "up" will be toggled based on %val, then movement will be updated
function MovementBehavior::moveUp(%this, %val)
{
   %this.up = %val;
   %this.updateMovement();
}

// Keybind triggered moveDown function
/// %this - The MovementBehavior being used
// %val - Boolean that will be 1 (true) if the key was pressed down, 0 (false) if the key was released
// The variable "down" will be toggled based on %val, then movement will be updated
function MovementBehavior::moveDown(%this, %val)
{
   %this.down = %val;
   %this.updateMovement();
}

// Keybind triggered moveLeft function
// %this - The MovementBehavior being used
// %val - Boolean that will be 1 (true) if the key was pressed down, 0 (false) if the key was released
// The variable "left" will be toggled based on %val, then movement will be updated
function MovementBehavior::moveLeft(%this, %val)
{
   %this.left = %val;
   %this.updateMovement();
}

// Keybind triggered moveRight function
// %this - The MovementBehavior being used
// %val - Boolean that will be 1 (true) if the key was pressed down, 0 (false) if the key was released
// The variable "right" will be toggled based on %val, then movement will be updated
function MovementBehavior::moveRight(%this, %val)
{
   %this.right = %val;
   %this.updateMovement();
}

// Actual movement of the owner is performed in this function
function MovementBehavior::updateMovement(%this)
{
   // Make the owner move around horizontally
   %this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
   
   // Make the owner move around vertically
   %this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}

Also, make sure UsesPhysics is turned on. Without this, the sprite will not move around.

I'm not sure what you are asking for your second question. Can you provide more details or an example?
#4
04/29/2011 (10:42 am)
I see that this code I use move the picture sprite any direction but doesnt' change image, so it the same image. How would I use this code to change picture So it seem that he is walking? instead of a static picture non moving.
example a man walking which would use a animation to walk every step