2 Questions
by Navarre Dickson · in General Discussion · 12/07/2011 (4:42 pm) · 2 replies
I'm new to Torque and have 2 questions, 1 about Torque in general and 1 about my specific project...
1. Whenever I change the size of my camera view and preview the game, everything is stretched. This makes me think that the camera and the final output are 2 different things. If so, where do I adjust the final output and is there a way to link the 2 to avoid skewing?
2. I'm making a character walk around Legend of Zelda style. I have animations playing when the direction buttons are pressed via behaviors. I want to add an attack animation when SPACE is pressed. I have it so that the animation played when I press SPACE, but it will play as long as I have SPACE held down and wont play through the whole thing if I just tap it. How can I make the animation play once in its entirety when the button is pressed once rather than having it play only while the button is held? See code below
Note: I also noticed that the animation only plays if I'm moving while I hold SPACE.
1. Whenever I change the size of my camera view and preview the game, everything is stretched. This makes me think that the camera and the final output are 2 different things. If so, where do I adjust the final output and is there a way to link the 2 to avoid skewing?
2. I'm making a character walk around Legend of Zelda style. I have animations playing when the direction buttons are pressed via behaviors. I want to add an attack animation when SPACE is pressed. I have it so that the animation played when I press SPACE, but it will play as long as I have SPACE held down and wont play through the whole thing if I just tap it. How can I make the animation play once in its entirety when the button is pressed once rather than having it play only while the button is held? See code below
Note: I also noticed that the animation only plays if I'm moving while I hold SPACE.
// Make sure the PlayerBehavior object doesn't already exist
if(!isObject(PlayerBehavior))
{
// Create & Define the template
%template = new BehaviorTemplate(PlayerBehavior);
%template.friendlyName = "Player Controls"; // This is the name that will appear in the Behaviors property
%template.behaviorType = "Player"; // This will be the category under which the behavior appears
%template.description = "Player input for the hero's movement"; // This will provide the user with a description of the property
// Behavior fields will expose values to the editor interface
// Note that, unlike the Torque Game Builder documentation's used of simply
// "Up" and "Down", we must define "keyboard" as the source for the binding.
%template.addBehaviorField(runNorthKey, "Run- North", keybind, "keyboard w");
%template.addBehaviorField(runSouthKey, "Run- South", keybind, "keyboard s");
%template.addBehaviorField(runWestKey, "Run- West", keybind, "keyboard a");
%template.addBehaviorField(runEastKey, "Run- East", keybind, "keyboard d");
// Input Speed
%template.addBehaviorField(runSpeed, "Movement Speed", float, 15.0);
%template.addBehaviorField(attackKey, "Attack", keybind, "keyboard space");
// Movement Flags -> Auto assigned to object
%template.direction = "South";
%template.FLAG_RUN_SOUTH = 0;
%template.FLAG_RUN_NORTH = 0;
%template.FLAG_RUN_EAST = 0;
%template.FLAG_RUN_WEST = 0;
%template.FLAG_ATTACK = 0;
}
// Called when the behavior is added to an object by the editor
function PlayerBehavior::onBehaviorAdd(%this)
{
// Enable the Update callback
%this.owner.enableUpdateCallback();
// Make sure that the moveMap object exists before attempting
// to bind keys to it
if(!isObject(moveMap))
return;
// Bind our movement keys
moveMap.bindObj(getWord(%this.runNorthKey, 0), getWord(%this.runNorthKey, 1), "runNorth", %this);
moveMap.bindObj(getWord(%this.runSouthKey, 0), getWord(%this.runSouthKey, 1), "runSouth", %this);
moveMap.bindObj(getWord(%this.runEastKey, 0), getWord(%this.runEastKey, 1), "runEast", %this);
moveMap.bindObj(getWord(%this.runWestKey, 0), getWord(%this.runWestKey, 1), "runWest", %this);
moveMap.bindObj(getWord(%this.attackKey, 0), getWord(%this.attackKey, 1), "attack", %this);
}
// Utility Function - Get what our current animation name is supposed to be
function PlayerBehavior::whichAnimation(%this)
{
if(%this.FLAG_ATTACK)
return "heroAttack" @ %this.direction @ "Animation";
else
return "heroRun" @ %this.direction @ "Animation";
}
// Movement Keys
function PlayerBehavior::runNorth(%this, %val)
{
%this.FLAG_RUN_NORTH = %val;
}
function PlayerBehavior::runSouth(%this, %val)
{
%this.FLAG_RUN_SOUTH = %val;
}
function PlayerBehavior::runWest(%this, %val)
{
%this.FLAG_RUN_WEST = %val;
}
function PlayerBehavior::runEast(%this, %val)
{
%this.FLAG_RUN_EAST = %val;
}
function PlayerBehavior::attack(%this, %val)
{
%this.FLAG_ATTACK = %val;
}
// Player Update
function PlayerBehavior::onUpdate(%this)
{
%standing = false;
// Directional Animation Priority
if(%this.FLAG_RUN_SOUTH)
%this.direction = "South";
else if(%this.FLAG_RUN_NORTH)
%this.direction = "North";
else if(%this.FLAG_RUN_EAST)
%this.direction = "East";
else if(%this.FLAG_RUN_WEST)
%this.direction = "West";
else
%standing = true;
// Movement
%this.owner.setLinearVelocityY((%this.FLAG_RUN_SOUTH - %this.FLAG_RUN_NORTH) * %this.runSpeed);
%this.owner.setLinearVelocityX((%this.FLAG_RUN_EAST - %this.FLAG_RUN_WEST) * %this.runSpeed);
// Check the animation, and set it if it must be updated
if(%this.owner.getAnimationName() !$= %this.whichAnimation() )
%this.owner.setAnimation(%this.whichAnimation());
// If we're standing still, use the 'idle' frame
if(%standing)
%this.owner.setAnimationFrame(1);
}
Employee Michael Perry
GarageGames
2. Your attack function is set up to act that way:
function PlayerBehavior::attack(%this, %val) { %this.FLAG_ATTACK = %val; }The %val that is passed in will be 1 if the key was pressed and 0 if it was released. Currently, you have a flag for determining if the character is attacking or not. This is immediately checked and an animation is played appropriately. To accomplish what you are asking for, you need to add some logic to check to see if the attack animation has completed before you move to the next state.