BindOBJ in behavior problem.
by Skebrew · in Torque Game Builder · 10/11/2011 (7:15 am) · 2 replies
Hello! I don't know whether this is the right section. But here's my problem. I followed an example of how to make a character movement behavior, and everything was going fine. My character (Whom I've named Meelo) was able to walk in any direction on the screen. I was using the %val Boolean to see if a button was pressed for the normal movement keys and a run key (space), but then whenever I try to run up left it won't do it!
I have tried everything and searched all over this website but I couldn't find anything that helped.
I think it's either a problem with it can't detect five buttons at once. Because I've tried having a special case for Up and Left running. Without running it works fine, so I tried several things that proved that it was a problem detecting the running button. Any way any help would be HUGELY appreciated! This thing has been eating me for days. Here's the code:
I have tried everything and searched all over this website but I couldn't find anything that helped.
I think it's either a problem with it can't detect five buttons at once. Because I've tried having a special case for Up and Left running. Without running it works fine, so I tried several things that proved that it was a problem detecting the running button. Any way any help would be HUGELY appreciated! This thing has been eating me for days. Here's the code:
if (!isObject(PlayerBehavior))
{
%template = new BehaviorTemplate(PlayerBehavior);
%template.friendlyName = "Player Movement";
%template.behaviorType = "Player";
%template.description = "Gives the player movement ^u^.";
%template.addBehaviorField(isPlayer, "Is this object the player?", bool,"False");
%template.addBehaviorField(upKey, "What is the key to walk up?", keybind,"keyboard up");
%template.addBehaviorField(downKey, "What is the key to walk up?", keybind,"keyboard down");
%template.addBehaviorField(leftKey, "What is the key to walk up?", keybind,"keyboard left");
%template.addBehaviorField(rightKey, "What is the key to walk up?", keybind,"keyboard right");
%template.addBehaviorField(runKey, "What is the key you press to run?", keybind,"keyboard space");
%template.addBehaviorField(runSpeed, "How much does holding the run key multiply your speed?", float, 2.0);
%template.addBehaviorField(walkSpeed, "How fast can the character walk", float, 12.0);
%this.currentY = 0.0;
%this.currentX = 0.0;
Echo("Operating start");
}
function PlayerBehavior::onBehaviorAdd(%this, %scenegraph, %playerMap)
{
%this.owner.enableUpdateCallback();
Echo(" on behaviour add started setting variables ");
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
%this.running = 0.0;
Echo(" behavior add finished ");
}
function PlayerBehavior::onAddToScene(%this, %scenegraph)
{
Echo("onAddToScene controlls starting");
Echo(" Starting bind ");
playerMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "Up", %this);
playerMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "Down", %this);
playerMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "Left", %this);
playerMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "Right", %this);
playerMap.bindObj(getWord(%this.runKey, 0), getWord(%this.runKey, 1), "Run", %this);
// playerMap.bindCmd(keyboard, getWord(%this.upKey, 0), %this.WalkUpActiv(), %this.WalkUpUnActiv());
Echo(" binding complete and pushing ");
playerMap.push();
Echo(" Pushing complete ");
}
function PlayerBehavior::onUpdate(%this, %scenegraph)
{
//%this.UpdateMovementY(); //old
//%this.updateMovementX(); //old
%this.updateMovement();
%this.PlayMovementAnimations();
}
function PlayerBehavior::Up(%this, %val)
{
%this.up = %val;
}
function PlayerBehavior::Down(%this, %val)
{
%this.down = %val;
}
function PlayerBehavior::Run(%this, %val)
{
%this.running = %val;
}
function PlayerBehavior::Left(%this, %val)
{
%this.left = %val;
}
function PlayerBehavior::Right(%this, %val)
{
%this.right = %val;
}
function PlayerBehavior::UpdateMovement(%this)//new improvisez method
{
if(%this.running == 1)
{
%speed = %this.runSpeed * %this.walkSpeed;
}
else
{
%speed = %this.walkSpeed;
}
if(%this.down == 1)
{
if(%this.up == 1)
{
%Y = 0;
}
else
{
%Y = %speed;
}
}
if(%this.up == 1)
{
if(%this.down == 1)
{
%Y = 0;
}
else
{
%Y = %speed * -1;
}
}
if(%this.right == 1)
{
if(%this.left == 1)
{
%X = 0;
}
else
{
%X = %speed;
}
}
if(%this.left == 1)
{
if(%this.right == 1)
{
%X = 0;
}
else
{
%X = %speed * -1;
}
}
if(%this.up == 1)//special case handeler (Doesn't work!)
{
if(%this.left == 1)
{
%X = %speed * -1;
%Y = %speed * -1;
}
}
%this.owner.setLinearVelocityX(%X);
%this.owner.setLinearVelocityY(%Y);
}
/*function PlayerBehavior::UpdateMovementY(%this) //old handlers
{
if(%this.running == 0)
{
%newMovementY = (%this.down - %this.up) * %this.walkSpeed;
}
else
{
%newMovementY = ((%this.down - %this.up) * %this.walkSpeed) * %this.runSpeed;
}
%this.owner.setLinearVelocityY(%newMovementY);
}
function PlayerBehavior::updateMovementX(%this)
{
if(%this.running == 0)
{
%newMovementX = (%this.right - %this.left) * %this.walkSpeed;
}
else
{
%newMovementX = ((%this.right - %this.left) * %this.walkSpeed) * %this.runSpeed;
}
%this.owner.setLinearVelocityX(%newMovementX);
}*/
function PlayerBehavior::PlayMovementAnimations(%this)//Not implemented yet
{
%movementX = %this.owner.getLinearVelocityX();
%movementY = %this.owner.getLinearVelocityY();
%angle = "None";
//figure out angles
if(%movementX == 0)
{
if(%movementY == 0)
{
%angle = "None";
}
}
if(%movementX > 0)//going right
{
if(%movementY == 0)
{
%angle = "Right";
}
if(%movementY > 0)//going down
{
%angle = "DownRight";
}
if(%movementY < 0)//going up
{
%angle = "UpRight";
}
}
else
{
if(%movementY == 0)
{
if(%movementX < 0)//going Left
{
%angle = "Left";
}
}
if(%movementY > 0)//going down
{
%angle = "DownLeft";
}
if(%movementY < 0)//going up
{
%angle = "UpLeft";
}
}
if(%movementX == 0)
{
if(%movementY < 0)//going up
{
%angle = "Up";
}
if(%movementY > 0)//going down
{
%angle = "Down";
}
}
} About the author
I like Batman.
#2
Thank you answering so fast. I'm glad it wasn't a problem with my code.
Any way thanks again I'm glad that's done.
10/11/2011 (7:17 pm)
Wow that was fast! Thanks I tried it using M as the run key and it worked.Thank you answering so fast. I'm glad it wasn't a problem with my code.
Any way thanks again I'm glad that's done.
Torque Owner Rpahut
You can probably use numpad arrow keys if you turn Num Lock off, but still it's always wise to add control options to your game, or at least support few alternative layouts.