Moving a Sprite using a behavior
by Leo Goldseed · in Technical Issues · 12/20/2008 (11:42 pm) · 0 replies
I am using a behavior to move an object in TGB. It works great! but there are a couple of things I don't get:
On the moveMap.bindObj( ) the moveup, move down, etc, etc are bound with keys. However, when I look at the actual functions that move the object (moveUp, moveDown, etc), the code passes a %val parameter that in the function body it gets assigned to %this.right, %this.left, etc.
1st Question: Where did this %val come from? What does it do???
2nd Question:
Also, on the updateMovement function, the linear velocity assignment functions pass the speed parameter like this: (%this.right - %this.left) why is that?
I know thanks to this the movement is made smooth, vs exclusively assigning the linear velocity the the object, but I don't get how they work. I would relaly appreciate any explanation.
Code:
On the moveMap.bindObj( ) the moveup, move down, etc, etc are bound with keys. However, when I look at the actual functions that move the object (moveUp, moveDown, etc), the code passes a %val parameter that in the function body it gets assigned to %this.right, %this.left, etc.
1st Question: Where did this %val come from? What does it do???
2nd Question:
Also, on the updateMovement function, the linear velocity assignment functions pass the speed parameter like this: (%this.right - %this.left) why is that?
I know thanks to this the movement is made smooth, vs exclusively assigning the linear velocity the the object, but I don't get how they work. I would relaly appreciate any explanation.
Code:
if (!isObject(MovementBehavior))
{
%template = new BehaviorTemplate(MovementBehavior);
%template.friendlyName = "Movement Behavior";
%template.behaviorType = "Movement Styles";
%template.description = "Shooter style movement control";
// this next field creation also effectively creates a variable with a string called upKey with a value of "Keyboard up"
%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);
}
function MovementBehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;
// bind our keys to the keyboard. here you obtain the value of the previously saved string, and assign a funciton to them
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);
// set the default values to 0
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
}
function MovementBehavior::onBehaviorRemove(%this)
{
if (!isObject(moveMap))
return;
%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);
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
}
function MovementBehavior::updateMovement(%this)
{
// make the player move
%this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
%this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}
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();
}