| Moving a character in Torque GameBuilder | ||||||
| Home Page | ||||||
In your game, you need to make a new document for the code that you are going to make for your character. We are going to call it �move.cs� and make sure to save it as a .cs extension so it can work with your project. We are going to make a function to move are character. function MoveCharacter::onLevelLoaded(%this, %scenegraph) { } Now we are going to declare a $MoveCharacter, any variable that has the $ is a global variable. Any variable with the % is a local variable available only in that function. $MoveCharacter = %this Add that line of code inside the MoveCharacter Function because we need it to link up with the other functions that we are going to make. Now we are going to start add the code that will get are character to move. moveMap.bindCmd (keyboard, �w�, �moveCharacterUp();�, �moveCharacterUpStop();�); moveMap.bindCmd (keyboard, �s�, �moveCharacterDown();�, �moveCharacterDownStop();�); moveMap.bindCmd (keyboard, �a�, �moveCharacterLeft();�, �moveCharacterLeftStop();�); moveMap.bindCmd (keyboard, �d�, �moveCharacterRight();�, �moveCharacterRightStop();�); All that code goes into the function MoveCharacter. Now that we have keys to move are character, we just need the code now. There are going to be 8 new functions for each moveCharacter up in the first function. function moverCharacterUp() { $MoveCharacter.setLinearVelocityY(-15) } function moverCharacterDown() { $MoveCharacter.setLinearVelocityY(15) } function moverCharacterLeft() { $MoveCharacter.setLinearVelocityX(-30) } function moverCharacterRight() { $MoveCharacter.setLinearVelocityX(30) } The code above is for moving your character around the screen at a set speed. These are stand alone functions they don�t need to be put into the first function that we made. function moveCharacterUpStop() { $MoverCharacter.setLinearVelocityY(0); } function moveCharacterDownStop() { $MoverCharacter.setLinearVelocityY(0); } function moveCharacterLeftStop() { $MoverCharacter.setLinearVelocityX(0); } function moveCharacterRightStop() { $MoverCharacter.setLinearVelocityX(0); } The code above stops the movement of the character when you are switching keys. It helps slow it down. We are almost done with the code all we need to do now is to make sure that the game can read it. In the game.cs document add this line of code to the function startGame. exec(�./move.cs�); Now we are down with the code. This tutorial was based off of the tutorial Fish Game in Torque. |
||||||