<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Minalien.com &#187; TGB</title>
	<atom:link href="http://minalien.com/tag/tgb/feed/" rel="self" type="application/rss+xml" />
	<link>http://minalien.com</link>
	<description>Game Programming Weblog &#38; Portfolio</description>
	<lastBuildDate>Sat, 21 Jan 2012 23:05:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>TGB 1.7.5 &#8211; Multiple Animations for Sprite</title>
		<link>http://minalien.com/2011/07/tgb175-multiple-animations/</link>
		<comments>http://minalien.com/2011/07/tgb175-multiple-animations/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 06:34:01 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[TGB]]></category>
		<category><![CDATA[Torque]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Zelda]]></category>

		<guid isPermaLink="false">http://minalien.com/?p=258</guid>
		<description><![CDATA[It seems that once again I&#8217;ve heard the call to write a one-shot tutorial (and strangely enough, this one is also one related to animations). This time, the call for help comes from a user on the GarageGames forums, from a user jumping into the Torque engine line with Torque Game Builder 1.7.5. I  [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that once again I&#8217;ve heard the call to write a one-shot tutorial (and strangely enough, this one is also one related to animations). This time, the call for help comes from a user on the <a href="http://garagegames.com">GarageGames</a> forums, from a user jumping into the Torque engine line with Torque Game Builder 1.7.5. I have a great amount of respect and love for the Torque engines, and the GarageGames team and community have a lot of people that are really fun to be around (in contrast to my experience with the Unity boards, which seems to have no shortage of elitists).</p>
<p><a href='http://minalien.com/wp-content/uploads/2011/07/ZeldaGame-game.zip'>ZeldaGame- game assets</a></p>
<p>Once again, we will be writing up a Zelda-like animation, because the file&#8217;s on-hand and already formatted and somewhat optimized for our use. And so, if you don&#8217;t have it from following previous tutorials, here it is again:</p>
<p><div id="attachment_193" class="wp-caption aligncenter" style="width: 522px"><a href="http://minalien.com/wp-content/uploads/2011/06/link.png" rel="lightbox[258]" title="ALTTP Link"><img src="http://minalien.com/wp-content/uploads/2011/06/link.png" alt="" title="ALTTP Link" width="512" height="256" class="size-full wp-image-193" /></a><p class="wp-caption-text">Sample Animation Sheet</p></div><br />
<span id="more-258"></span></p>
<h3>Setup</h3>
<p>Start out by creating a new project in Torque Game Builder. I am using version 1.7.5 on my Mac, but there should be no differences between platforms or versions for this project. I&#8217;m calling mine &#8220;ZeldaGame&#8221; and including the executable, but that&#8217;s personal preference more than necessity. Once you&#8217;ve got the project created, make sure you create a new image map from the Link animation sheet above by either dragging the file into the assets pane on the right (under Static Sprites &#8211; only works on Windows), or by using the Create ImageMap button.</p>
<p>After creating the image map, double-click on it so that we can edit its properties as follows:<br />
<div id="attachment_260" class="wp-caption aligncenter" style="width: 729px"><a href="http://minalien.com/wp-content/uploads/2011/07/Screen-shot-2011-07-29-at-10.25.26-PM.png" rel="lightbox[258]" title="Link Image Map"><img src="http://minalien.com/wp-content/uploads/2011/07/Screen-shot-2011-07-29-at-10.25.26-PM.png" alt="" title="Link Image Map" width="719" height="418" class="size-full wp-image-260" /></a><p class="wp-caption-text">Link Image Map</p></div></p>
<p><strong>Image Mode:</strong> Cell<br />
<strong>Filter Mode:</strong> Smooth<br />
<strong>Cell Height:</strong> 64<br />
<strong>Cell Width:</strong> 64</p>
<p>This will break our sprite sheet into its individual frames, ready to be used in animations. It&#8217;s best to keep as many frames of an animation set within the same file as possible, because it will help the Torque engine to limit the number of times it is changing the texture context during the game&#8217;s execution.</p>
<h3>Animation Naming Scheme</h3>
<p>It is important to come up with a naming scheme for your assets and your animations. In this sample, it was fine to leave Link&#8217;s image name as linkImageMap, because it&#8217;s the only image we&#8217;re going to have. Your animation names, however, should have a structured and defined naming scheme, because it will make things much easier when you are writing the behavior (or class, if that is your preference) that will control which animation we are displaying. For this project, we will use &#8220;linkRunDirectionAnimation&#8221; for our names; <em>link</em> being the name of the entity, <em>Run</em> being the name of the object&#8217;s state (running, as opposed to swimming or dying), <em>Direction</em> being the direction he is running (North, South, East, or West), and Animation to denote that this is an animation resource.</p>
<p>We will now create four animations: North, South, East, and West. Click the Create New Animation button, and select the <em>linkImageMap</em> image as the base for our animation. Double-click each frame, in sequence, to add it to the animation. Change Frames Per Second for the animation to 15 (so that it looks good), and name the animation based on the direction he is going (such as linkRunSouthAnimation). Create the remaining animations, and we can begin programming our behavior.</p>
<h3>Behave, Link!</h3>
<p>Now we are going to write a behavior script to define the Link object&#8217;s interaction. In this project, our behavior is going to control everything about our player- it will store the object&#8217;s state, ensure that the proper animation is playing, and we will be able to edit the player&#8217;s movement speed within the editor interface. Open your project&#8217;s folder and create a new file in <em>ZeldaGame/game/behaviors/</em> folder called <em>playerBehavior.cs</em>. You can use Notepad, Text Edit, Torsion, TextMate, or whatever your TorqueScript editor preference to write these scripts, so long as it is saved without special formatting data that may occur from applications such as word processing applications.</p>
<pre class="brush: cpp; title: ZeldaGame/game/behaviors/playerBehavior.cs; notranslate">// Make sure the PlayerBehavior object doesn't already exist
if(!isObject(PlayerBehavior))
{
	// Create &amp; Define the template
	%template = new BehaviorTemplate(PlayerBehavior);

	%template.friendlyName = &quot;Player Controls&quot;;	// This is the name that will appear in the Behaviors property
	%template.behaviorType = &quot;Player&quot;;	// This will be the category under which the behavior appears
	%template.description = &quot;Player input for the Zelda sample&quot;;	// This will provide the user with a description of the property

	// Behavior fields will expose values to the editor interface
	// Note that, unlike the Torque Game Builder documentation's used of simply
	// &quot;Up&quot; and &quot;Down&quot;, we must define &quot;keyboard&quot; as the source for the binding.
	%template.addBehaviorField(runNorthKey, &quot;Run- North&quot;, keybind, &quot;keyboard up&quot;);
	%template.addBehaviorField(runSouthKey, &quot;Run- South&quot;, keybind, &quot;keyboard down&quot;);
	%template.addBehaviorField(runWestKey, &quot;Run- West&quot;, keybind, &quot;keyboard left&quot;);
	%template.addBehaviorField(runEastKey, &quot;Run- East&quot;, keybind, &quot;keyboard right&quot;);

	// Bind the Movement Speed for Link
	%template.addBehaviorField(runSpeed, &quot;Movement Speed&quot;, float, 15.0);

	// Movement Flags
	%template.direction = &quot;South&quot;;
	%template.FLAG_RUN_SOUTH = 0;
	%template.FLAG_RUN_NORTH = 0;
	%template.FLAG_RUN_EAST = 0;
	%template.FLAG_RUN_WEST = 0;
}</pre>
<p>In this first portion, we are actually defining the behavior template. As this is covered in the TGB documentation, I&#8217;m not going to go into too much detail- I recommend <a href="http://docs.garagegames.com/tgb/official/index.html?content/documentation/Behavior%20Tutorials/Fish%20Game%20Tutorial.html">reading up</a> if you&#8217;re unfamiliar with behaviors in TGB. Down at the bottom, though, we set some flags that we will use for checking our movement. The first, &#8220;direction,&#8221; will allow us to determine which directional animation we wish to display (North, South, East, or West). The following four are used to determine whether the associated key is being held, as we want to use prioritized animations with movement, but don&#8217;t want to lose the player&#8217;s ability to run in multiple directions.</p>
<pre class="brush: cpp; highlight: [5]; title: ZeldaGame/game/behaviors/playerBehavior.cs; notranslate">// Called when the behavior is added to an object by the editor
function PlayerBehavior::onBehaviorAdd(%this)
{
	// Enable the Update callback
	%this.owner.enableUpdateCallback();

	// Make sure that the moveMap object exists before attempting
	// to bind keys to it
	if(!isObject(moveMap))
		return;

	// Bind our movement keys
	moveMap.bindObj(getWord(%this.runNorthKey, 0), getWord(%this.runNorthKey, 1), &quot;runNorth&quot;, %this);
	moveMap.bindObj(getWord(%this.runSouthKey, 0), getWord(%this.runSouthKey, 1), &quot;runSouth&quot;, %this);
	moveMap.bindObj(getWord(%this.runEastKey, 0), getWord(%this.runEastKey, 1), &quot;runEast&quot;, %this);
	moveMap.bindObj(getWord(%this.runWestKey, 0), getWord(%this.runWestKey, 1), &quot;runWest&quot;, %this);
}</pre>
<p>Again, all of this is covered in the Torque Game Builder documentation. Note, however, the highlighted line. Here, we are enabling the onUpdate() callback function, which will be called every frame to allow us to update the object where needed. We will use this to check the animation against the current object state, and to update movement information.</p>
<pre class="brush: cpp; title: ZeldaGame/game/behaviors/playerBehavior.cs; notranslate">// Utility Function - Get what our current animation name is supposed to be
function PlayerBehavior::whichAnimation(%this)
{
	return &quot;linkRun&quot; @ %this.direction @ &quot;Animation&quot;;
}

// Movement Keys
function PlayerBehavior::runNorth(%this, %val)
{
	%this.FLAG_RUN_NORTH = %val;
}

// [...]
</pre>
<p>Because of their simplicity, I&#8217;m not going to include all of the movement response functions in this online sample- they each just edit their associated flag based on whether the key was pressed or released. The utility function, whichAnimation, will allow us to quickly get the animation that we are currently using. Normally, we would use an animation state variable in place of &#8220;Run,&#8221; but I&#8217;ve eschewed it in favor of simplicity.</p>
<pre class="brush: cpp; title: ZeldaGame/game/behaviors/playerBehavior.cs; notranslate">// Player Update
function PlayerBehavior::onUpdate(%this)
{
	%standing = false;

	// Directional Animation Priority
	if(%this.FLAG_RUN_SOUTH)
		%this.direction = &quot;South&quot;;
	else if(%this.FLAG_RUN_NORTH)
		%this.direction = &quot;North&quot;;
	else if(%this.FLAG_RUN_EAST)
		%this.direction = &quot;East&quot;;
	else if(%this.FLAG_RUN_WEST)
		%this.direction = &quot;West&quot;;
	else
		%standing = true;

	// Movement
	%this.owner.setLinearVelocityY((%this.FLAG_RUN_SOUTH - %this.FLAG_RUN_NORTH) * %this.runSpeed);
	%this.owner.setLinearVelocityX((%this.FLAG_RUN_EAST - %this.FLAG_RUN_WEST) * %this.runSpeed);

	// Check the animation, and set it if it must be updated
	if(%this.owner.getAnimationName() !$= %this.whichAnimation())
		%this.owner.setAnimation(%this.whichAnimation());

	// If we're standing still, use the 'idle' frame (0)
	if(%standing)
		%this.owner.setAnimationFrame(0);
}</pre>
<p>Here is the meat of our project. First, we create a temporary variable that we can set to mark the player as simply standing there (rather than walking in place, a la <em>Final Fantasy</em>). We check each of our movement flags sequentially, allowing us to prioritize which direction our player will be considered to be facing (South > North > East > West), and if none of them are pressed, we set the player to be standing (the direction variable will persist across frames, so the player will be facing the last direction he was moving). Next, we set the linear velocity -the amount the player moves each frame- in both vertical (Y) and horizontal (X) axes by calculating the value of the directional flags multiplied by the run speed (if the south and north keys are pressed together, the player will not move in that direction, for example (1-1=0=false)).</p>
<p>Finally, we check the animation&#8217;s name and compare it to our whichAnimation() function, and if it is not already set to the appropriate animation, we change it to such with setAnimation(). Note that these are called on the behavior&#8217;s <strong>owner</strong>, not the behavior itself &#8211; and you will want to make sure that the owner of the behavior (the object to which the behavior was assigned) is a t2dAnimatedSceneObject or a derivative of such (Animated Sprites dragged from the editor window are <em>t2dAnimatedSceneObject</em>s), so that the animation functions actually work. If the player is standing (<em>%standing == true</em>), we set the animation frame to 0, which is perfect for our idle image. You might also choose to include an idle animation, set as another animation state, if you wish to have an animated idle object.</p>
<h3>Tying it all Together</h3>
<p>Now, you will want to either close out the editor and re-open it, or you will want to go to Project->Reload Project in order for the editor to automatically load in our behaviors. Drag your South animation over from the editor window, under Animated Sprites, and place Link wherever you&#8217;d like. Next, select the object we just placed and go into the Edit pane. Under the Behaviors category, select &#8220;Player Controls&#8221; from the drop-down menu and click the + button the left to add the behavior to the player object.</p>
<div id="attachment_270" class="wp-caption aligncenter" style="width: 533px"><a href="http://minalien.com/wp-content/uploads/2011/07/Screen-shot-2011-07-29-at-11.30.46-PM.png" rel="lightbox[258]" title="ZeldaGame - Behaviors"><img src="http://minalien.com/wp-content/uploads/2011/07/Screen-shot-2011-07-29-at-11.30.46-PM.png" alt="" title="ZeldaGame - Behaviors" width="523" height="297" class="size-full wp-image-270" /></a><p class="wp-caption-text">Player Controls behavior</p></div>
<p>Now simply press Play, and use the arrow keys to move Link around. Congratulations, you&#8217;ve now got Link running around in the middle of absolutely nothing!</p>
]]></content:encoded>
			<wfw:commentRss>http://minalien.com/2011/07/tgb175-multiple-animations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

