Game Development Community

a rabbit

by rennie moffat · in Torque Game Builder · 09/18/2009 (3:31 pm) · 14 replies

Hi, I want to make a game where the camera flows along at a constant speed of X. So I decided the best way to do this is, create a "rabbit". This rabbit will start on levelLoaded at X speed and will run at Xspeed throughout the level. I will then attach a cameraMount to it. Simple right?


So, the only thing is the actual rabbit. I have made this code, I would think would work. I just need the rabbit to move at Xvelocity throughout it's limitless life. But for some reason, when I class my image as rabbit, it does not move. Any help would be appreciated.

Thanks
Ren

function rabbit::onLevelLoaded(%this)
	{
		$rabbit = %this;
	}

function rabbit::rabbitMovement(%this)
	{
		$rabbit.setLinearVelocityX(%this.speed);
	}

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/18/2009 (4:44 pm)
Hi Rennie,
you messed up some code. ^_^

In the rabbitMovement method you are setting the x linear velocity to 0 because you never initialized the speed variable of the object.

Use this simplier code:

//Default rabbit speed
$RabbitSpeed = 5;

//When level is loaded, this method will be called upon all objects with Rabbit class attached, i.e. your t2dStaticSprite
function Rabbit::onLevelLoaded(%this)
{
  %this.setLinearVelocityX($RabbitSpeed);
}
#2
09/18/2009 (4:48 pm)
that works great, thanks!!!!


#3
09/18/2009 (9:13 pm)
Dude, I did actually have a question for you regarding this. From all that I have learnt so far, I never would have thought to place a global variable into my setLinearVelocity($RabbitSpeed). So is this more common than I would think placing globals in what would normally be local?

However, I may end that as I would like to have flexibility level to level. So if that were the case, would I not just make what is now $ a %?






;?..im
#4
09/19/2009 (2:31 am)
You can easily make it different per level. After you add your "tracking" object and give it the class of "rabbit", open up the "Dynamic Fields" at the bottom. Set the field name to "speed" and the value to "5" or whatever you want it to be. Finally, click the "+" to add it.

Now, in Giuseppe Berton's code, make the following change
function Rabbit::onLevelLoaded( %this )
{
  %this.setLinearVelocityX( %this.speed );
}
#5
09/19/2009 (3:29 am)
hey thanks,
of course i can make it each level yah, i use dynamic fields for sure I understand that, the only thing that surprised me about this code was that Giuseppe, put in a dynamic field (Xspeed) as a global. This, if it is in global would be global. I don't want that as I may want an ice level.
#6
09/19/2009 (3:31 am)
So variety is what I am going for here. Flexibility. Therefore a local variable.
#7
09/19/2009 (3:32 am)
Or does that matter with behaviors? A global variable?
#8
09/20/2009 (2:39 am)
In an ice level, you'll have another rabbit, right? Just set a dynamic variable on it.

Globals are nice because you can have them in one area, easy for making minor tweaks to the game. But if you want it to be different in each level, you'll need to design for that.
#9
09/20/2009 (4:36 pm)
yah plus wind speed and stuff. Many possibilities. A simple % from $ would do I would think.
#10
09/21/2009 (7:27 pm)
You can't use a local variable, because the function has no clue what level you're in.

function Rabbit::onLevelLoaded( %this )
{
  %speed = 5;
  if( isIceLevelLoaded() ) %speed = %speed + 3;
  if( isWindDirectionLeft() ) %speed = %speed - 2;
  if( isWindDirectionRight() ) %speed = %speed + 2;
  %this.setLinearVelocityX( %speed );
}

Where do these functions come from? Because I'm an experienced coder, I could make this work, but it's more effort than its worth.

Your two options are to used a fixed variable, like
function Rabbit::onLevelLoaded( %this )
{
  %this.setLinearVelocityX( 5 );
}
or to use my example, where each level has its own rabbit with its own dynamic variable.

Because magic constants are Just Plain Wrong(tm) (the "5" in the above example), the proper thing is to give it a name.
function Rabbit::onLevelLoaded( %this )
{
  %rabbitSpeed = 5;
  %this.setLinearVelocityX( %rabbitSpeed );
}

Because finding all of these constants to tweak can be a pain, a lot of people will create a "configuration" file of sorts with global variables that tweak the game. (See Giuseppe Bertone's code.)

Because you want each level to have its own scroll speed, you'll need an object with a dynamic variable. That object is loaded when the level is loaded and will be used when the "onLevelLoaded" function is called.

Voila!
#11
09/21/2009 (8:19 pm)
Sweeeeeeet.
#12
10/04/2009 (11:41 pm)
Hi William, I have a question for you regarding your last post here.

I know this may sound noobish, and that I know nothing, which is not true, but I was wondering, you declared a could of if statements in the onLevelLoaded. I was wondering how, or if, you could please further elaborate how one of these "methods"(?) would be called, how would the computer know? Would you simply make a function titled "isWindDirectionLeft" with in the same .cs, or would you do something else all together?



#13
10/05/2009 (12:20 am)
To call one of these methods, you would simply make a function called "isWindDirectionLeft" that returned a boolean. You could put it in the same .cs, or you could encapsulate these and other similar functions into another file.

I still recommend making a scene object with the class "Rabbit" and using a dynamic variable.
#14
10/05/2009 (1:24 am)
Yes it was not so much this rabbit a particular problem, but more with some coding I am doing to better understand how it all works. Yes a dynamic, I am finding is much more flexible.