<?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>Game Geek Speak &#187; PushButton Engine</title>
	<atom:link href="http://geekspeak.creatrixgames.com/category/programming/pushbutton-engine/feed" rel="self" type="application/rss+xml" />
	<link>http://geekspeak.creatrixgames.com</link>
	<description>Flash, Java, and Unity game development blog with a focus on business and marketing.</description>
	<lastBuildDate>Mon, 25 Oct 2010 00:48:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Angry Explanations #2: Spinning a freaking image with PBE!</title>
		<link>http://geekspeak.creatrixgames.com/angry-explanations-2-spinning-a-freaking-image-with-pbe.html</link>
		<comments>http://geekspeak.creatrixgames.com/angry-explanations-2-spinning-a-freaking-image-with-pbe.html#comments</comments>
		<pubDate>Thu, 19 Nov 2009 17:26:52 +0000</pubDate>
		<dc:creator>jason</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[PushButton Engine]]></category>

		<guid isPermaLink="false">http://geekspeak.creatrixgames.com/?p=506</guid>
		<description><![CDATA[Following on from Angry Explanations #1, we will now add some motion to the scene. For kicks and giggles, we&#8217;ll cause the image to rotate each frame. (Note that this information is in reference to PushButton Engine build r625.) The key takeaway here is that for a component to be tickable, it must implement the [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from <a href="http://geekspeak.creatrixgames.com/angry-explanations-1-how-to-draw-a-freaking-image-with-pbe.html">Angry Explanations #1</a>, we will now add some motion to the scene. For kicks and giggles, we&#8217;ll cause the image to rotate each frame.</p>
<p>(Note that this information is in reference to <a href="http://pushbuttonengine.com">PushButton Engine</a> build r625.)</p>
<p>The key takeaway here is that for a component to be tickable, it must implement the <code>ITickedObject</code> interface and then tell the <code>ProcessManager</code> to start and stop calling <code>onTick()</code> every tick.</p>
<p>Oh, there&#8217;s one other key takeaway here: how to use <code>PropertyReference</code>s. You get a property of a component by way of the owning entity. You create a <code>PropertyReference</code> instance for each data member that you need to access. In this example, we&#8217;re accessing the <code>rotation</code> field on the <code>SpriteRenderer</code> component we added in <a href="http://geekspeak.creatrixgames.com/angry-explanations-1-how-to-draw-a-freaking-image-with-pbe.html">Angry Explanations #1</a>.</p>
<p>In a bit more detail, the <code>PropertyReference</code> is constructed with a special string: <code>"@Sprite.rotation"</code> (see line 11 in the code below). That means, this is a reference to the component of this entity with the name &#8220;Sprite&#8221; (recall that we gave it that name when we created the <code>SpriteRenderer</code> component instance), and the field &#8220;rotation&#8221; of the <code>SpriteRenderer</code> class. It&#8217;s quite simple once you realize what it&#8217;s doing (<em>but I&#8217;m no less angry, mind you!</em>).</p>
<p>I thought that a SpatialComponent might be necessary to alter the sprite&#8217;s position, but apparently that component has been removed (since r470, which is the current packaged release). That data seems to now be handled by the renderer component itself.</p>
<p>Add this one line to the end of <code>showLogo()</code> from the previous example:</p>
<pre class="brush: as3">spriteEntity.addComponent( new SpinController(), &quot;spinner&quot; );</pre>
<p>Then add the following new class in a sub-folder (components/controllers/).</p>
<pre class="brush: as3">
package com.yourcompany.yourgame.components.controllers
{
	import com.pblabs.engine.core.ITickedObject;
	import com.pblabs.engine.core.ProcessManager;
	import com.pblabs.engine.entity.EntityComponent;
	import com.pblabs.engine.entity.PropertyReference;

	public class SpinController extends EntityComponent implements ITickedObject
	{
		// Cache the PropertyReference instance here to avoid a bunch of temporary allocations.
		private var _rotProp:PropertyReference = new PropertyReference( &quot;@Sprite.rotation&quot; );

		// Nothing to do in the constructor because this is such a simple example.
		public function SpinController()
		{
			super();
		}

		// When the component is added to the sprite entity, we want to register to
		// get tick updates by the ProcessManager.
		protected override function onAdd():void
		{
			super.onAdd();

			ProcessManager.instance.addTickedObject( this );
		}

		// When the component is removed from the sprite entity, we want to unregister
		// from the ProcessManager so tick updates will stop.
		protected override function onRemove():void
		{
			super.onRemove();

			ProcessManager.instance.removeTickedObject( this );
		}

		// Each tick, we just change the rotation of the sprite renderer a little bit.
		public function onTick(tickRate:Number):void
		{
			// Get the current rotation.
			var rotation:Number = owner.getProperty( _rotProp ) as Number;

			// Add 1 degree to it.
			rotation += 1;

			// Store the new rotation back in the sprite.
			owner.setProperty( _rotProp, rotation );
		}
	}
}</pre>
<p>As an addendum, I should point out that the rotation will probably not be smooth. To fix that, you should have the <code>SpinController</code> implement <code>IAnimatedObject</code> instead of <code>ITickedObject</code>. The registration with the <code>ProcessManager</code> is the same, except you call <code>addAnimatedObject()</code> instead of <code>addTickedObject()</code>.</p>
<p>The difference is that <code>IAnimatedObject</code> will update when Flash draws a frame instead of when the <code>ProcessManager</code> ticks, and the motion will appear a lot smoother.</p>
<p>And you&#8217;ll want to normalize the rotation based on delta time since the last frame. Er, like this:</p>
<pre class="brush: as3">public function onFrame( elapsed:Number ):void
{
	var rotation:Number = owner.getProperty( _rotProp ) as Number;

	// Rotate 15 degrees per second, nice and slow.
	rotation += 15 * elapsed;

	owner.setProperty( _rotProp, rotation );
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://geekspeak.creatrixgames.com/angry-explanations-2-spinning-a-freaking-image-with-pbe.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Angry Explanations #1: How to draw a freaking image with PBE!</title>
		<link>http://geekspeak.creatrixgames.com/angry-explanations-1-how-to-draw-a-freaking-image-with-pbe.html</link>
		<comments>http://geekspeak.creatrixgames.com/angry-explanations-1-how-to-draw-a-freaking-image-with-pbe.html#comments</comments>
		<pubDate>Wed, 18 Nov 2009 00:24:57 +0000</pubDate>
		<dc:creator>jason</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[PushButton Engine]]></category>

		<guid isPermaLink="false">http://geekspeak.creatrixgames.com/?p=466</guid>
		<description><![CDATA[(Thanks to Ben G. for that humorous title! And just to be super clear&#8211;I&#8217;m not angry at all about PBE. The anger is entirely tongue-in-cheek.) So, yeah, I got started with PBE at a bad time, I think. It&#8217;s in transition between some major changes, and so all the docs and tutorials to this point [...]]]></description>
			<content:encoded><![CDATA[<p>(Thanks to Ben G. for that humorous title! <img src='http://geekspeak.creatrixgames.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  And just to be super clear&#8211;I&#8217;m not angry at all about PBE. The anger is entirely tongue-in-cheek.)</p>
<p>So, yeah, I got started with <a href="http://pushbuttonengine.com/">PBE</a> at a bad time, I think.</p>
<p>It&#8217;s in transition between some major changes, and so all the docs and tutorials to this point are out of sync and <del datetime="2009-11-18T00:30:38+00:00">kind of</del> very&#8230; confusing. Because of that, I really struggled to figure this out, and in fact couldn&#8217;t get an image on the screen without professional help (I&#8217;ll let you interpret that however you&#8217;d like).</p>
<p>This information is relevant for PBE r625 and up.</p>
<p>(This isn&#8217;t to say the engine is poorly designed&#8211;it&#8217;s simply not accurately documented in places because it has been changing so quickly. And to be fair, it&#8217;s still in beta. I wouldn&#8217;t use the engine if I didn&#8217;t think it was great.)</p>
<h2>A frustrated noob&#8217;s guide to getting a freakin&#8217; image on the dang screen using Flex 3</h2>
<p>Here&#8217;s the MXML which will reference the code.</p>
<pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;game:Game
	xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
	xmlns:game=&quot;com.yourcompany.yourgame.*&quot;
	width=&quot;750&quot;
	height=&quot;600&quot;
	layout=&quot;absolute&quot;&gt;

&lt;/game:Game&gt;</pre>
<p>Now define a resource in a ResourceBundle subclass, which is the image you want to display, and let PBE automagically make it accessible through the resource manager.</p>
<pre class="brush: as3">package com.yourcompany.yourgame
{
	import com.pblabs.engine.resource.ResourceBundle;

	public class EmbeddedResources extends ResourceBundle
	{
		// The path is magically also the name of the resource.
		[Embed(source=&quot;../assets/images/logo.png&quot;, mimeType=&quot;application/octet-stream&quot;)]
		public var logoImage:Class;
	}
}</pre>
<p>Put that image into action! It&#8217;s a simple notion, but somewhat complicated if you&#8217;re new to the engine and its gooey innards.</p>
<pre class="brush: as3">package com.yourcompany.yourgame
{
	import com.pblabs.engine.PBE;
	import com.pblabs.engine.entity.IEntity;
	import com.pblabs.engine.resource.ImageResource;
	import com.pblabs.engine.resource.ResourceManager;
	import com.pblabs.rendering2D.DisplayObjectScene;
	import com.pblabs.rendering2D.SpriteRenderer;
	import com.pblabs.rendering2D.ui.FlexSceneView;

	import mx.containers.Canvas;
	import mx.core.Application;
	import mx.events.FlexEvent;

	public class Game extends Application
	{
		public var resources:EmbeddedResources;

		// We need access to this in a couple of functions.
		private var _scene:DisplayObjectScene;

		public function Game()
		{
			super();

			// Let the Flex Application initialize fully before we muck with it.
			addEventListener( FlexEvent.APPLICATION_COMPLETE, onStartup );
		}

		// This is called once the Flex app (stage, in particular) is initialized.
		private function onStartup( e:FlexEvent ) : void
		{
			// Load our embedded resources (the image to show).
			resources = new EmbeddedResources();

			// Tell PBE to initialize and pass it our instance so it can get the stage.
			// (Remember, Application is a subclass of Sprite, which PBE needs.)
			PBE.startup( this );

			createPbeScene();

			showLogo();
		}

		private function createPbeScene():void
		{
			// Create a new IEntity for the scene view.
			var sceneEntity:IEntity = PBE.allocateEntity();
			sceneEntity.initialize( &quot;sceneEntity&quot; );

			_scene = new DisplayObjectScene();

			// Since this is Flex, we need the appropriate scene view class.
			var sceneView:FlexSceneView = new FlexSceneView();

			// Set up the view size.
			sceneView.width = 750;
			sceneView.height = 600;

			// FREAKIN&#039; IMPORTANT: add the view as a child to our display list!
			this.addChild( sceneView );

			// Connect the view to the display object scene (a component).
			_scene.sceneView = sceneView;

			// Add the display object scene to the scene entity.
			sceneEntity.addComponent( _scene, &quot;renderer&quot; );
		}

		private function showLogo():void
		{
			// Create a new IEntity instance for the sprite to be displayed.
			var spriteEntity:IEntity = PBE.allocateEntity();
			spriteEntity.initialize( &quot;logoEntity&quot; );

			// Add a sprite rendering component.
			var sprite:SpriteRenderer = new SpriteRenderer();

			// Tell the renderer to draw (and load) this image resource.
			sprite.fileName = &quot;../assets/images/logo.png&quot;;

			// Tell the renderer to draw to the display object scene.
			sprite.scene = _scene;

			// Add the component to the sprite entity.
			spriteEntity.addComponent( sprite, &quot;sprite&quot; );
		}
	}
}
</pre>
<p>So, what the heck just happened? Pfft! Don&#8217;t ask me! <em>I&#8217;m still a freakin&#8217; noob!</em> <img src='http://geekspeak.creatrixgames.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I know a couple of things, though. The SceneView is like a &#8220;canvas&#8221; where all DisplayObjectRenderers are drawn. So you create entities and fill them with components (like a SpriteRenderer) and tell the renderer component about the scene view. Magically, the sprite appears! It&#8217;s not too much to understand if you just want to know the API and how to use it.</p>
<p>To really understand it, you&#8217;ll need to look under the hood and figure out the connections. A good bit of hidden work is being done to simplify the API, but possibly at the expense of understanding. It&#8217;s neither good nor bad. It&#8217;s just The Way of PBE (for now).</p>
<p>Feel free to post here to help fill my gaps in comprehension. I haven&#8217;t used the engine enough to write a thorough explanation, and I&#8217;m not too proud to accept help. <img src='http://geekspeak.creatrixgames.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://geekspeak.creatrixgames.com/angry-explanations-1-how-to-draw-a-freaking-image-with-pbe.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.188 seconds -->

