Basic object tutorial
by Shelby Flagg · in Torque Game Builder · 05/10/2008 (9:12 am) · 1 replies
I'm in a Torque 2D class, and our first project was to build a tutorial explaining something in Torque. So I chose to explain the very basics of objects. Or what little I know of them anyways. Maybe I'll come back and edit this after I've learned a bit more, as it stands I'm pretty much completely new at this! [All of the code is from the Fish Game Demo, since it had a lot to work with at my level.] Enjoy!
Objects in Torque serve many purposes, far too many for me to cover in just one tutorial. So we'll start with something simple, moving an object.
The first thing that you need to do is to drag said object into your playing field, and to do so you need to check out your image library under the 'create' tab. (If you have no images in your library import some. Via the 'create new image map' button, or Project - Resources, then add a set images that way.)
After you've dragged your image in and resized to whatever size you'd like, you need to give it some setting before you get into the script. With the image selected go now to the edit tab, and then the scene object menu. All that needs to be done here is to set the layer of your object. If it's the only thing you have you can leave it at the default layer.
Now in the scripting menu give your object a class, something simple that relates to the object is fine. Just make sure that it's easy to remember.
The last thing you need is the dynamic fields. Give it something like vertical speed and horizontal speed. Or vSpeed and hSpeed. Give these some numerical values as well 15 and 30, respectively.
The last thing you need to do is set the world limits for your object. You wouldn't want it walking right off your playing field.
Now for the code!
function PlayerFish::onLevelLoaded(%this, %scenegraph)
{
$FishPlayer = %this;
moveMap.bindCmd(keyboard, "w", "fishPlayerUp();", "fishPlayerUpStop();");
moveMap.bindCmd(keyboard, "s", "fishPlayerDown();", "fishPlayerDownStop();");
moveMap.bindCmd(keyboard, "a", "fishPlayerLeft();", "fishPlayerLeftStop();");
moveMap.bindCmd(keyboard, "d", "fishPlayerRight();", "fishPlayerRightStop();");
}
PlayerFish is the class name that we gave the object earlier, and this code will set up what keys will move your object. We used the wasd setup here, you can choose whatever keys that you'd like.
function fishPlayerUp()
{
$FishPlayer.moveUp = true;
$FishPlayer.updateMovement();
}
function fishPlayerDown()
{
$FishPlayer.moveDown = true;
$FishPlayer.updateMovement();
}
function fishPlayerLeft()
{
$FishPlayer.moveLeft = true;
$FishPlayer.updateMovement();
}
function fishPlayerRight()
{
$FishPlayer.moveRight = true;
$FishPlayer.updateMovement();
}
All of these will update the image of your object, and give it the illusion of moving across the screen."
Objects in Torque serve many purposes, far too many for me to cover in just one tutorial. So we'll start with something simple, moving an object.
The first thing that you need to do is to drag said object into your playing field, and to do so you need to check out your image library under the 'create' tab. (If you have no images in your library import some. Via the 'create new image map' button, or Project - Resources, then add a set images that way.)
After you've dragged your image in and resized to whatever size you'd like, you need to give it some setting before you get into the script. With the image selected go now to the edit tab, and then the scene object menu. All that needs to be done here is to set the layer of your object. If it's the only thing you have you can leave it at the default layer.
Now in the scripting menu give your object a class, something simple that relates to the object is fine. Just make sure that it's easy to remember.
The last thing you need is the dynamic fields. Give it something like vertical speed and horizontal speed. Or vSpeed and hSpeed. Give these some numerical values as well 15 and 30, respectively.
The last thing you need to do is set the world limits for your object. You wouldn't want it walking right off your playing field.
Now for the code!
function PlayerFish::onLevelLoaded(%this, %scenegraph)
{
$FishPlayer = %this;
moveMap.bindCmd(keyboard, "w", "fishPlayerUp();", "fishPlayerUpStop();");
moveMap.bindCmd(keyboard, "s", "fishPlayerDown();", "fishPlayerDownStop();");
moveMap.bindCmd(keyboard, "a", "fishPlayerLeft();", "fishPlayerLeftStop();");
moveMap.bindCmd(keyboard, "d", "fishPlayerRight();", "fishPlayerRightStop();");
}
PlayerFish is the class name that we gave the object earlier, and this code will set up what keys will move your object. We used the wasd setup here, you can choose whatever keys that you'd like.
function fishPlayerUp()
{
$FishPlayer.moveUp = true;
$FishPlayer.updateMovement();
}
function fishPlayerDown()
{
$FishPlayer.moveDown = true;
$FishPlayer.updateMovement();
}
function fishPlayerLeft()
{
$FishPlayer.moveLeft = true;
$FishPlayer.updateMovement();
}
function fishPlayerRight()
{
$FishPlayer.moveRight = true;
$FishPlayer.updateMovement();
}
All of these will update the image of your object, and give it the illusion of moving across the screen."
Shelby Flagg
{
$FishPlayer.moveUp = false;
$FishPlayer.updateMovement();
}
function fishPlayerDownStop()
{
$FishPlayer.moveDown = false;
$FishPlayer.updateMovement();
}
function fishPlayerLeftStop()
{
$FishPlayer.moveLeft = false;
$FishPlayer.updateMovement();
}
function fishPlayerRightStop()
{
$FishPlayer.moveRight = false;
$FishPlayer.updateMovement();
}
And of course we need to make sure that once you stop holding the button, that your object stops as well. Setting the FishPlayer.moveRight to false tells the object to stop moving, just as the setting the previous set of code to true is what told the object to continue moving.
function PlayerFish::updateMovement(%this)
{
if(%this.dead)
return;
if(%this.moveLeft)
{
$FishPlayer.setFlipX(true);
$FishPlayer.setLinearVelocityX( -$FishPlayer.hSpeed );
}
if(%this.moveRight)
{
$FishPlayer.setFlipX(false);
$FishPlayer.setLinearVelocityX( $FishPlayer.hSpeed );
}
if(%this.moveUp)
{
%this.setLinearVelocityY( -$FishPlayer.vSpeed );
}
if(%this.moveDown)
{
%this.setLinearVelocityY( $FishPlayer.vSpeed );
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY( 0 );
}
}
Now to update the object even more. The first few bits will make your object change directions when you change the direction that the object is going. So that you don't end up with your object moving backwards. The second half sets the velocity at which your object moves.
Now that we have the basics of moving the object down be sure that you test this all out and make sure that everything is in working order. I'd also like to thank whoever made the fish demo game, as without the help from that tutorial I would have never been able to get this tutorial done."