Attempting to write a strafe function...
by Marc Zdon · in Technical Issues · 02/25/2009 (6:53 pm) · 2 replies
function mClass::onLevelLoaded(%this, %scenegraph)
{
$Player1 = %this;
moveMap.bindCmd(keyboard, "up", "mPlayerUp();", "mPlayerUpStop();");
moveMap.bindCmd(keyboard, "down", "mPlayerDown();", "mPlayerDownStop();");
moveMap.bindCmd(keyboard, "left", "mPlayerLeft();", "mPlayerLeftStop();");
moveMap.bindCmd(keyboard, "right", "mPlayerRight();", "mPlayerRightStop();");
moveMap.bindCmd(keyboard, "shift", "mPlayerStrafe();", "mPlayerStrafeStop();");
}
function mClass::updateFrame(%this, %frame)
{
$Player1.setFrame(%frame);
}
function mClass::updateMovement(%this)
{
if(%this.moveLeft)
{
if(!%this.strafe)
{
$Player1.updateFrame(2);
}
$Player1.setLinearVelocityX( -$Player1.hSpeed );
}
if(%this.moveRight)
{
if(!%this.strafe)
{
$Player1.updateFrame(3);
}
$Player1.setLinearVelocityX( $Player1.hSpeed );
}
if(%this.moveUp)
{
if(!%this.strafe)
{
$Player1.updateFrame(1);
}
$Player1.setLinearVelocityY( -$Player1.vSpeed );
}
if(%this.moveDown)
{
if(!%this.strafe)
{
$Player1.updateFrame(0);
}
$Player1.setLinearVelocityY( $Player1.vSpeed );
}
if(%this.moveLeft && %this.moveRight)
{
$Player1.setLinearVelocityX( 0 );
}
if(%this.moveUp && %this.moveDown)
{
$Player1.setLinearVelocityY( 0 );
}
if(!%this.moveLeft && !%this.moveRight)
{
$Player1.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
$Player1.setLinearVelocityY( 0 );
}
}
function mPlayerStrafe()
{
$Player1.strafe = true;
$Player1.updateMovement();
}
function mPlayerUp()
{
$Player1.moveUp = true;
$Player1.updateMovement();
}
function mPlayerDown()
{
$Player1.moveDown = true;
$Player1.updateMovement();
}
function mPlayerLeft()
{
$Player1.moveLeft = true;
$Player1.updateMovement();
}
function mPlayerRight()
{
$Player1.moveRight = true;
$Player1.updateMovement();
}
function mPlayerStrafeStop()
{
$Player1.strafe = false;
$Player1.updateMovement();
}
function mPlayerUpStop()
{
$Player1.moveUp = false;
$Player1.updateMovement();
}
function mPlayerDownStop()
{
$Player1.moveDown = false;
$Player1.updateMovement();
}
function mPlayerLeftStop()
{
$Player1.moveLeft = false;
$Player1.updateMovement();
}
function mPlayerRightStop()
{
$Player1.moveRight = false;
$Player1.updateMovement();
}
This is the code I've got going so far to make my sprite move up down left and right and face the proper direction and it works until I try and use the strafe key. It sets the sprite moving in a certain direction and it will continue to move after I release the directional key. But the frames continue to change no matter what.
Clearly I'm very new to TGB. I'm supposed to be helping program a game for my game II class but that in itself is a joke. I shouldn't be on the tech team to being with...
Any ideas and possibly an explanation with the correct code so that I might understand torquescript better and continue writing other functions?
{
$Player1 = %this;
moveMap.bindCmd(keyboard, "up", "mPlayerUp();", "mPlayerUpStop();");
moveMap.bindCmd(keyboard, "down", "mPlayerDown();", "mPlayerDownStop();");
moveMap.bindCmd(keyboard, "left", "mPlayerLeft();", "mPlayerLeftStop();");
moveMap.bindCmd(keyboard, "right", "mPlayerRight();", "mPlayerRightStop();");
moveMap.bindCmd(keyboard, "shift", "mPlayerStrafe();", "mPlayerStrafeStop();");
}
function mClass::updateFrame(%this, %frame)
{
$Player1.setFrame(%frame);
}
function mClass::updateMovement(%this)
{
if(%this.moveLeft)
{
if(!%this.strafe)
{
$Player1.updateFrame(2);
}
$Player1.setLinearVelocityX( -$Player1.hSpeed );
}
if(%this.moveRight)
{
if(!%this.strafe)
{
$Player1.updateFrame(3);
}
$Player1.setLinearVelocityX( $Player1.hSpeed );
}
if(%this.moveUp)
{
if(!%this.strafe)
{
$Player1.updateFrame(1);
}
$Player1.setLinearVelocityY( -$Player1.vSpeed );
}
if(%this.moveDown)
{
if(!%this.strafe)
{
$Player1.updateFrame(0);
}
$Player1.setLinearVelocityY( $Player1.vSpeed );
}
if(%this.moveLeft && %this.moveRight)
{
$Player1.setLinearVelocityX( 0 );
}
if(%this.moveUp && %this.moveDown)
{
$Player1.setLinearVelocityY( 0 );
}
if(!%this.moveLeft && !%this.moveRight)
{
$Player1.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
$Player1.setLinearVelocityY( 0 );
}
}
function mPlayerStrafe()
{
$Player1.strafe = true;
$Player1.updateMovement();
}
function mPlayerUp()
{
$Player1.moveUp = true;
$Player1.updateMovement();
}
function mPlayerDown()
{
$Player1.moveDown = true;
$Player1.updateMovement();
}
function mPlayerLeft()
{
$Player1.moveLeft = true;
$Player1.updateMovement();
}
function mPlayerRight()
{
$Player1.moveRight = true;
$Player1.updateMovement();
}
function mPlayerStrafeStop()
{
$Player1.strafe = false;
$Player1.updateMovement();
}
function mPlayerUpStop()
{
$Player1.moveUp = false;
$Player1.updateMovement();
}
function mPlayerDownStop()
{
$Player1.moveDown = false;
$Player1.updateMovement();
}
function mPlayerLeftStop()
{
$Player1.moveLeft = false;
$Player1.updateMovement();
}
function mPlayerRightStop()
{
$Player1.moveRight = false;
$Player1.updateMovement();
}
This is the code I've got going so far to make my sprite move up down left and right and face the proper direction and it works until I try and use the strafe key. It sets the sprite moving in a certain direction and it will continue to move after I release the directional key. But the frames continue to change no matter what.
Clearly I'm very new to TGB. I'm supposed to be helping program a game for my game II class but that in itself is a joke. I shouldn't be on the tech team to being with...
Any ideas and possibly an explanation with the correct code so that I might understand torquescript better and continue writing other functions?
About the author
#2
You can also get away with simplifying the code significantly:
function mPlayerDown()
{
$Player1.yDirection = 1;
$Player1.updateMovement();
}
function mPlayerUp()
{
$Player1.yDirection = -1;
$Player1.updateMovement();
}
...so that the update function can be simpler:
$Player1.setLinearVelocityX($Player1.hSpeed * $Player1.yDirection);
You can eliminate "%this.moveUp" and "%this.moveDown" because you can't have your character move up and down at the same time anyway.
06/02/2010 (3:52 pm)
I think it's an issue with binding the "shift" key. Typically you would bind "shift up" since shift is a modifier key like ctrl, alt and option. If you want to bind the shift keys, you can bind "lshift" and "rshift" directly.You can also get away with simplifying the code significantly:
function mPlayerDown()
{
$Player1.yDirection = 1;
$Player1.updateMovement();
}
function mPlayerUp()
{
$Player1.yDirection = -1;
$Player1.updateMovement();
}
...so that the update function can be simpler:
$Player1.setLinearVelocityX($Player1.hSpeed * $Player1.yDirection);
You can eliminate "%this.moveUp" and "%this.moveDown" because you can't have your character move up and down at the same time anyway.
Torque Owner Kevin James