FishGame tutorial movement question... Why...
by Matt Troup · in Torque Game Builder · 05/22/2006 (7:57 pm) · 3 replies
I keep forgetting to ask this, so I'll do it when I have half a mind to.
In the FishGame tutorial, you control the fish with functions that look similar to:
Why was movment handled in this way as compared to the old Shooter tutorials where the code was something like:
In the fishGame tutorial, I get a few bugs/undesired affects from the code. Sometimes when I release a button the player character will continue to move in that direction without my control. Also, if I hold the right button then press left while holding the right button, the fish will not turn left. However, if I hold left then press right while still holding left the fish will turn right. The same happens with the Up/Down buttons respectively.
So, my question is why was the code to handle movment changed? Is there a great benefit to this new way that I have not witnessed yet?
In the FishGame tutorial, you control the fish with functions that look similar to:
function PlayerFish::updateMovement(%this)
{
if(%this.dead)
return;
if(%this.moveLeft)
{
$FishPlayer.setFlipX(false);
$FishPlayer.setLinearVelocityX( -$FishPlayer.hSpeed );
}
.
.
.
function fishPlayerUp()
{
$FishPlayer.moveUp = true;
$FishPlayer.updateMovement();
}
etc
etcWhy was movment handled in this way as compared to the old Shooter tutorials where the code was something like:
function ControlRight(%val)
{
if (%val)
{
$Player.setLinearVelocityX( $movementSpeed );
}
else
{
if ( $Player.getLinearVelocityX() > 0 )
$Player.setLinearVelocityX( 0 );
}
}In the fishGame tutorial, I get a few bugs/undesired affects from the code. Sometimes when I release a button the player character will continue to move in that direction without my control. Also, if I hold the right button then press left while holding the right button, the fish will not turn left. However, if I hold left then press right while still holding left the fish will turn right. The same happens with the Up/Down buttons respectively.
So, my question is why was the code to handle movment changed? Is there a great benefit to this new way that I have not witnessed yet?
About the author
#2
That shooter movement code is from the actual demo, not a tutorial. The tutorial code is much more basic. The major difference between the two movement codes is naturally the shooter demo using four seperate functions to handle movement while the fish game only has one. Is one method better than the other? Probably not, it all comes down to how you script it.
As to why the fish game is giving you "undesired" effects, it's due to the order in which the if statements are evaluated.
If you wish to have movement like in the shooter demo, you can do that. It would mean changing how the if statements are evaluated or using switch/case statements instead.
Why the player keeps moving sometimes after key release, not sure why you are getting that. Recheck your player..Stop() functions to make sure the code is correct. I haven't noticed that behavior.
05/23/2006 (1:13 am)
Couple things:That shooter movement code is from the actual demo, not a tutorial. The tutorial code is much more basic. The major difference between the two movement codes is naturally the shooter demo using four seperate functions to handle movement while the fish game only has one. Is one method better than the other? Probably not, it all comes down to how you script it.
As to why the fish game is giving you "undesired" effects, it's due to the order in which the if statements are evaluated.
function playerShip::updateMovement(%this)
{
if(%this.moveLeft)
{
%this.setLinearVelocityX( -$pShip.hSpeed );
}
if(%this.moveRight)
{
%this.setLinearVelocityX( $pShip.hSpeed );
}
if(%this.moveUp)
{
%this.setLinearVelocityY( -$pShip.vSpeed );
}
if(%this.moveDown)
{
%this.setLinearVelocityY( $pShip.vSpeed );
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY( 0 );
}
}If I hold down the right button: moveLeft is checked first and is false (no negative velocity applied), then moveRight is checked and found true (positive movement applied). If you now press the left button while still holding down the right: moveLeft is checked and found true (negative movement applied), then moveRight checked and is also still true (positive movement applied). There is no addition or subtraction going on, the positive setLinearVelocity of moveRight is simply overwriting the negative velocity from moveLeft.If you wish to have movement like in the shooter demo, you can do that. It would mean changing how the if statements are evaluated or using switch/case statements instead.
Why the player keeps moving sometimes after key release, not sure why you are getting that. Recheck your player..Stop() functions to make sure the code is correct. I haven't noticed that behavior.
#3
05/23/2006 (10:34 am)
Thanks for the input. My characters in TGE have had a similar problem not stopping when told with the stock movement code. Seems like it's just my machine - that's actually a relief!
Torque Owner Philip Mansfield
Default Studio Name