Jump to content
  • Advertisement
Sign in to follow this  
leogoldseed

Moving a Sprite in a 2D Game

This topic is 3459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using Torque Game builder, and the script is pretty much a light version of C. So far nobody has been able to explain this in any of the forums I have posted it. I am using a Torque behavior (Prepackaged code than can be resued as needed for any object in a game) to move an object. It works great! but there are a couple of things I don't get: On the moveMap.bindObj( ) function, the moveup, move down, etc, etc are bound with keyboard 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: Code:
  
      // 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();
  	}
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 really appreciate any explanation. [Edited by - leogoldseed on December 28, 2008 3:15:52 AM]

Share this post


Link to post
Share on other sites
Advertisement
Brian WIlson from Garage Games kindly provided some explanation for enyone interested:

1) the %val is a boolean of whether the key is pressed or not
2) that allows for one call to handle the movement regardless of direction. It's a cleaner way of handling movment rather than setting the velocity with each move function. Think about what those values mean with each possilbe keypress. Since TGB is vector-based scene placement, that bit of math determines if the object should be moving + or - on each axis.

------------------------------------------------
My reply:

If I am following this correctly, if the user presses the left key, then %val would passed as "true"and then the assignment:

%this.left = %val;

would basically set %this.left = true as well. Does this mean that since "true" is generally = 1 and "false" = 0

then:

1-0 = +1
0-1 = -1
1-1 = 0
0-0= 0

and that's how the function figures out if it shuold be left, right (positive negative)?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!