Game Development Community

Regain a Constant Speed

by rennie moffat · in Torque Game Builder · 09/23/2009 (11:30 am) · 10 replies

Edit:Thurs Sept24.9.30p
Hi, I am trying to make a game where my player moves at 10 xVelocity, when triggered right he moves at 40, when triggered left -20, when nothing, he goes back to 10.

I have a code now, see below (this thread) which moves him at 10, but does not accept/react to input.




Why?






function playerClass::onLevelLoaded(%this, %scenegraph)
{
      $PlayerShip = %this;
	  
      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
      moveMap.bindCmd(keyboard, "space", "playerUp();", "playerUpStop();");
	  moveMap.bindCmd(keyboard, "down", "playerDown();", "playerDown();");
	 
	 
	 %this.enableUpdateCallback();
}

function playerClass::onUpdate(%this)
{
   %this.updateHorizontal();

}


function PlatformerSceneGraph::onUpdateScene()
{
     if (isObject($PlayerShip))
          $PlayerShip.updateMovement();
}


function playerClass::playerLeft()
{
    $PlayerShip.moveLeft = true;
}

function playerClass::playerLeftStop()
{
    $PlayerShip.moveLeft = false;
}

function playerClass::playerRight()
{
     $PlayerShip.moveRight = true;
}

function playerClass::playerRightStop()
{
     $PlayerShip.moveRight = false;
}

function playerClass::playerUp()
{
	$PlayerShip.moveUp = true;
}

function playerClass::playerUpStop()
{
	$PlayerShip.moveUp = false;
} 

function playerClass::playerDown()
{
	$PlayerShip.moveDown = true;
}

function playerClass::playerDownStop()
{
	$PlayerShip.moveDown = false;
}


function playerClass::updateMovement(%this)
{
     %this.updateHorizontal();
}

function playerClass::updateHorizontal(%this)
{
   if (%this.moveLeft == %this.moveRight)
   {
      %this.setLinearVelocityX(10);
      return;
   }
   
   else if (%this.moveLeft)
      {
         %this.setLinearVelocityX(-20);
		 return;
      }
   

   else if (%this.moveRight)
   	{   
         %this.setLinearVelocityX(40);
		 return;
     }
   
}

About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
09/24/2009 (2:29 am)
I don't know ,What you did was apply constant force on the X axis in the physics right? Im into Platformers so I wouldn't know much about that,.
#2
09/24/2009 (1:30 pm)
Currently, I have a global variable set up as the initial speed in a player.cs (thats really the only thing there.. attached code) I then have a completely separate behavior to control movement. I know I should combine them, it would probably make life easier. But still if anyone can give me a quick run down on how I might do this.


I was thinking perhaps instead of adding a -xVelocity on left, I add a dampen OR a -impulseForce, not sure. But the real issue is for my player tho, when no buttons are pressed, maintains a set X speed.


///Default player speed, currently in onLevelLoaded
/// Trick is I must somehow make sure this speed is regained after the keybinds are used  
  $playerSpeed= 10;    
  %this.setLinearVelocityX($playerSpeed);
#4
09/24/2009 (10:30 pm)
...Please see first post. it is edited and contains most recent info.

Ren
thurs setp24 9.30p
#5
09/25/2009 (11:43 am)
If anyone has insight to this, please holler at cherr boy.


Thanks,
:]
see top post.
#6
09/25/2009 (11:57 am)
Hmm, this is an odd piece of code..

Just wondering, why would you call so much updates? I have never heard of this way but this must be your way :D I like finding out new ways to do things thanks...

Anyways... Back to the code, I would do something like this:

function playerClass::onLevelLoaded(%this, %scenegraph)
{
      $PlayerShip = %this;
	  
      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");

      $PlayerShip.UpdateHorizontal();
	 
}

function playerLeft()
{
     $PlayerShip.moveLeft = true;
     $PlayerShip.UpdateHorizontal();
}

function playerLeftStop()
{
     $PlayerShip.moveLeft = false;
     $PlayerShip.UpdateHorizontal();
}

function playerRight()
{
     $PlayerShip.moveRight = true;
     $PlayerShip.UpdateHorizontal();
}

function playerRightStop()
{
     $PlayerShip.moveRight = false;
     $PlayerShip.UpdateHorizontal();
}

function playerClass::updateHorizontal(%this)
{
   if (%this.moveLeft == %this.moveRight)
   {
      %this.setLinearVelocityX(10);
      return;
   }
   
   else if (%this.moveLeft)
      {
         %this.setLinearVelocityX(-20);
		 return;
      }
   

   else if (%this.moveRight)
   	{   
         %this.setLinearVelocityX(40);
		 return;
     }
   
}

I have shortened the code just for the left and right buttons.

If this doesn't work or you see a problem or can even improve it please let me know.
#7
09/25/2009 (12:06 pm)
Thanks, Just checking it out.






#8
09/25/2009 (12:14 pm)
Nothing.

Just checked it he does not even move.
Haven't really had gone over the code yet, but all I have that is worrying me, is that I have a Class and Name, playerClass and Name. The Name, PlayerShip; the class: playerClass. Other than that I would expect the code, as I see it, to work. Regaurding all the updates those were just from the tut. but my most basic code would not work so I figured this, following the tut to a tea.

I don't know, may be Im dyslexic. Harhar.

I don't know But Im going to keep seeing if I can crack it.




#9
09/25/2009 (12:20 pm)
Hmm, I have tested the code on a quick test game and it works perfectly.

I have this:

My object in the game, it has a class as "playerClass", without quotes.

Then I just created a new cs file and added it to scripts and put this code in:

function playerClass::onLevelLoaded(%this, %scenegraph)  
{  
      $PlayerShip = %this;  
        
      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");  
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");  
  
      $PlayerShip.UpdateHorizontal();  
       
}  
  
function playerLeft()  
{  
     $PlayerShip.moveLeft = true;  
     $PlayerShip.UpdateHorizontal();  
}  
  
function playerLeftStop()  
{  
     $PlayerShip.moveLeft = false;  
     $PlayerShip.UpdateHorizontal();  
}  
  
function playerRight()  
{  
     $PlayerShip.moveRight = true;  
     $PlayerShip.UpdateHorizontal();  
}  
  
function playerRightStop()  
{  
     $PlayerShip.moveRight = false;  
     $PlayerShip.UpdateHorizontal();  
}  
  
function playerClass::updateHorizontal(%this)  
{  
   if (%this.moveLeft == %this.moveRight)  
   {  
      %this.setLinearVelocityX(10);  
      return;  
   }  
     
   else if (%this.moveLeft)  
      {  
         %this.setLinearVelocityX(-20);  
         return;  
      }  
     
  
   else if (%this.moveRight)  
    {     
         %this.setLinearVelocityX(40);  
         return;  
     }  
     
}

It is important that you have this code:

function playerLeft()  
{  
     $PlayerShip.moveLeft = true;  
     $PlayerShip.UpdateHorizontal();  
}  
  
function playerLeftStop()  
{  
     $PlayerShip.moveLeft = false;  
     $PlayerShip.UpdateHorizontal();  
}  
  
function playerRight()  
{  
     $PlayerShip.moveRight = true;  
     $PlayerShip.UpdateHorizontal();  
}  
  
function playerRightStop()  
{  
     $PlayerShip.moveRight = false;  
     $PlayerShip.UpdateHorizontal();  
}

without the playerClass:: before the function name, this means it is only for the object but when calling the function from the button all it calls is Left, Right, LeftStop, RightStop... No specific object is being called with it..

- Houssen
#10
09/25/2009 (12:31 pm)
Ok Houssen,
Thank you so much. It works!!!!


The only thing I noticed was that you added in a $playership.UpdateHorizontal() in the onLevelLoad. That is something I have never seen before and is not prevelant in the online materials. If you saw, before, my code, it had a %this.enableUpdateCallback()> Something I thought later linked to other bits of code, the updatesHorizontals etc.

But you! You! you cheeky bugger popped it right into the onlevel, made it more direct. Excellent sir. Thank You.



-Rennie