<?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>Shiki &#187; Flash Games</title>
	<atom:link href="http://shikii.net/blog/category/flash-games/feed/" rel="self" type="application/rss+xml" />
	<link>http://shikii.net/blog</link>
	<description>I&#039;m so bad with words that I can&#039;t even make a cool tagline</description>
	<lastBuildDate>Wed, 28 Mar 2012 23:37:19 +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>Creating custom Box2D shape components in PushButton Engine</title>
		<link>http://shikii.net/blog/creating-custom-box2d-shape-components-in-pushbutton-engine/</link>
		<comments>http://shikii.net/blog/creating-custom-box2d-shape-components-in-pushbutton-engine/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 13:40:22 +0000</pubDate>
		<dc:creator>Shiki</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[box2d]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[pushbutton engine]]></category>

		<guid isPermaLink="false">http://shikii.net/blog/?p=58</guid>
		<description><![CDATA[If you ever need to use other ways of creating Box2D shapes that are not currently available in PushButton Engine, you can simply make one yourself. You&#8217;ll just need to create a subclass of CollisionShape and use it just like the PBE shape classes. Here&#8217;s a sample class we use to create an oriented box [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever need to use other ways of creating <a href="http://www.box2d.org">Box2D</a> shapes that are not currently available 
in <a href="http://www.pushbuttonengine.com">PushButton Engine</a>, you can simply make one yourself. You&#8217;ll just need to create a subclass of <code>CollisionShape</code> 
and use it just like the PBE shape classes.</p>

<p>Here&#8217;s a sample class we use to create an oriented box shape:</p>

<pre><code>package cavalcade.box2D
{
    import Box2D.Collision.Shapes.b2PolygonDef;
    import Box2D.Collision.Shapes.b2ShapeDef;
    import Box2D.Common.Math.b2Vec2;

    import com.pblabs.box2D.CollisionShape;

    import flash.geom.Point;

    public class OrientedBoxCollisionShape extends CollisionShape
    {
        public var halfWidth:Number = 0.5;
        public var halfHeight:Number = 0.5;
        public var center:Point = new Point();
        public var angle:Number = 0;

        public function OrientedBoxCollisionShape()
        {
            super();
        }

        override protected function doCreateShape():b2ShapeDef
        {
            var scale:Number = _parent.manager.inverseScale;            

            var shape:b2PolygonDef = new b2PolygonDef();
            shape.SetAsOrientedBox(halfWidth * scale * _parent.size.x, halfHeight * scale * _parent.size.y, b2Vec2.Make(center.x, center.y), angle);            
            return shape;
        }
    }
}
</code></pre>

<p>As you can see, the class inherits from <code>CollisionShape</code> and overrides the <code>doCreateShape</code> method in order to create the actual 
Box2D oriented box shape from there. Use it in the level files like this:</p>

<pre><code>&lt;component type="com.pblabs.box2D.Box2DSpatialComponent" name="Cart"&gt;
    &lt;collidesWithTypes childType="String"&gt;
        &lt;_0&gt;Platform&lt;/_0&gt;
    &lt;/collidesWithTypes&gt;

    &lt;collisionShapes childType="com.pblabs.box2D.CollisionShape"&gt;
        &lt;_0 type="cavalcade.box2D.OrientedBoxCollisionShape"&gt;
            &lt;halfWidth&gt;0.42&lt;/halfWidth&gt;
            &lt;halfHeight&gt;0.12&lt;/halfHeight&gt;
            &lt;density&gt;50&lt;/density&gt; &lt;!-- you can still use the same properties in CollisionShape --&gt;
            &lt;friction&gt;2&lt;/friction&gt;
            &lt;restitution&gt;0&lt;/restitution&gt;
            &lt;angle&gt;3&lt;/angle&gt; &lt;!-- specify an angle of rotation --&gt;
            &lt;center&gt; &lt;!-- you can override the location of the box so it won't be positioned somewhere other than the center of the entity --&gt;
                &lt;x&gt;1&lt;/x&gt;
                &lt;y&gt;-0.5&lt;/y&gt; 
            &lt;/center&gt;
        &lt;/_0&gt;                
    &lt;/collisionShapes&gt;
    &lt;collisionType childType="String"&gt;
        &lt;_0&gt;Renderable&lt;/_0&gt;
    &lt;/collisionType&gt;
    &lt;manager componentReference="SpatialDB" /&gt;
        &lt;position type=""&gt;
            &lt;x&gt;0&lt;/x&gt;
            &lt;y&gt;110&lt;/y&gt;
        &lt;/position&gt;
        &lt;size type=""&gt;
            &lt;x&gt;60&lt;/x&gt;
            &lt;y&gt;45&lt;/y&gt;
        &lt;/size&gt;
&lt;/component&gt;
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://shikii.net/blog/creating-custom-box2d-shape-components-in-pushbutton-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Experiment: Using SWF MovieClips in PushButton Engine</title>
		<link>http://shikii.net/blog/experiment-using-swf-movieclips-in-pushbutton-engine/</link>
		<comments>http://shikii.net/blog/experiment-using-swf-movieclips-in-pushbutton-engine/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 17:11:17 +0000</pubDate>
		<dc:creator>Shiki</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[movieclip]]></category>
		<category><![CDATA[pushbutton engine]]></category>
		<category><![CDATA[swf]]></category>

		<guid isPermaLink="false">http://shikii.net/blog/?p=32</guid>
		<description><![CDATA[Important: This tutorial is now deprecated due to the new and awesome, fully rewritten, rendering components. You can see some discussions about it here. I&#8217;ve been working with PushButton Engine for a short while now but last night was actually the first time I tried integrating/using a MovieClip from a SWF into PBE. Unfortunately, there [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
  <p><strong>Important</strong>: This tutorial is now deprecated due to the new and awesome, fully rewritten, rendering components. 
  You can see some discussions about it <a href="http://pushbuttonengine.com/forum/viewtopic.php?f=5&amp;t=353">here</a>.</p>
</blockquote>

<p>I&#8217;ve been working with <a href="http://www.pushbuttonengine.com">PushButton Engine</a> for a short while now but last night was actually the 
first time I tried integrating/using a MovieClip from a SWF into PBE. Unfortunately, there are no documentations on how do this at the 
moment. The examples mostly use images (pngs). I won&#8217;t b<em>tch about it because that is totally understandable. 
The PBE team are working their a</em>*es off to get to 1.0 and the documentation is the least of their worries. I could at least 
thank them for doing such a good job &#8212; PBE&#8217;s a well thought-out framework, everything I learned from it made a lot of sense.</p>

<p>Anyway, what I wanted to do was simply to put my MovieClip in a SWF library into the stage. I am using FlexBuilder 3 + the latest PBE 
code from the SVN. I somehow got some ideas on how to do this when I was hanging out at the 
<a href="http://pushbuttonengine.com/chat/">#pbengine IRC channel</a>. And I did a lot of digging on the PBE code and did lots of experiments to 
try to understand how to use them. There are 2 different components we can use:</p>

<ul>
<li><code>com.pblabs.rendering2D.SWFSpriteSheetComponent</code>. This one actually had a class description: <em>&#8220;A class that is similar to the 
SpriteSheetComponent except the frames are loaded by rasterizing a MovieClip rather than a single image&#8221;.</em> 
Assuming you&#8217;ve gone through the PBE examples, I think this means that instead of creating the frames from a sprite sheet image 
and have it used by SpriteRenderComponent, you&#8217;ll be getting your frames from a loaded MovieClip instead. 
Nevertheless, this isn&#8217;t what I was aiming for.</li>
<li><code>com.pblabs.rendering2D.SWFRenderComponent</code>. This component somehow works like SpriteRenderComponent, only it uses MovieClips. 
A little code investigation later, looks like this component will need to be subclassed in order for it to work. From that subclass, 
you can do what you want to do with your movieclip  using a protected _clip variable.</li>
</ul>

<p>Since I still wanted control over a movieclip, I used SWFRenderComponent. I&#8217;ll explain below how I did it. Please do note that I am 
still a novice with PBE (and Flex for that matter) so the method I used might not be good practice.</p>

<h3>Requirements</h3>

<ul>
<li><strong>Latest PBE code</strong>. You can get it from the SVN <a href="http://code.google.com/p/pushbuttonengine/source/checkout">here</a> or 
download <a href="http://code.google.com/p/pushbuttonengine/downloads/list">here</a>. As of this writing, I&#8217;m at revision 470 (Sep 10, 2009)</li>
<li><strong>SWF</strong>. I have a compiled SWF containing exported assets (movieclip classes) that I want to use in PBE.</li>
<li><strong>Flex compiler / IDE</strong> / or whatever your preferred tool is. I&#8217;m using FlexBuilder + Flex Compiler.</li>
<li><strong>A PBE Project</strong>. Our team used the <a href="http://code.google.com/p/pushbuttonengine/source/browse/trunk#trunk/examples/PBEngineDemo">PBEngineDemo</a> 
project as the base project.</li>
</ul>

<h3>Embed the SWF</h3>

<p>In <code>Resources.as</code>, embed the SWF using the same method as the sample embedded resources in the PBEngineDemo project.</p>

<pre><code>[Embed(source='../assets/gamemovieclips.swf', mimeType='application/octet-stream')]
public var _embeddedSWF:Class;
</code></pre>

<p>I thought that the above would be enough just like embedding images. However, I was getting this warning from the log:</p>

<pre><code>WARN: Resources - ResourceBundle - No resource type specified for extension '.swf'.  In the ExtensionTypes parameter, expected to see something like: ResourceBundle.ExtensionTypes.mycustomext = "com.mydomain.customresource" where mycustomext is the (lower-case) extension, and "com.mydomain.customresource" is a string of the fully qualified resource class name.  Defaulting to generic DataResource.
</code></pre>

<p>This resulted to the gamemovieclips.swf not getting embedded in the final swf. I tried to look at the problem in ResourceBundle.as, 
and somehow, adding this to Resources.as fixed it:</p>

<pre><code>public class Resources extends ResourceBundle
{
  ResourceBundle.ExtensionTypes.swf = "com.pblabs.engine.resource.SWFResource"; // added this line

  [Embed(source='../assets/gamemovieclips.swf', mimeType='application/octet-stream')]
  public var _embeddedSWF:Class; // our embedded swf

  // other embed files below
  ...
</code></pre>

<h3>Subclass SWFRenderComponent</h3>

<p>Next, I created a new class which inherits <code>SWFRenderComponent</code> and overrides the <code>getClipInstance()</code> method. For this example, 
let&#8217;s say in my embedded swf (gamemovieclips.swf in the example above) I have a car animation movieclip exported as CarMC. 
I will then need to have <code>getClipInstance()</code> return an instance of that movieclip. My class would then look like this:</p>

<pre><code>package com.game.renders
{
    import com.pblabs.engine.resource.Resource;
    import com.pblabs.engine.resource.ResourceManager;
    import com.pblabs.engine.resource.SWFResource;
    import com.pblabs.rendering2D.SWFRenderComponent;

    import flash.display.MovieClip;

    public class CarRenderComponent extends SWFRenderComponent
    {
        public function CarRenderComponent()
        {
            super();
        }

        // override getClipInstance() and return a CarMC movieclip instance
        protected override function getClipInstance():MovieClip
        {
            // check if we are currently loading the movieclip or a CarMC instance was already loaded
            if (_clipInstance == null &amp;amp;&amp;amp; !_loading) {

                _loading = true;
                // load CarMC from gamemovieclips.swf using PBE's ResourceManager
                //I'm also passing an anonymous function which gets called if gamemovieclips.swf is loaded
                ResourceManager.instance.load("../assets/gamemovieclips.swf", SWFResource, function(resource:Resource):void {
                    // reaching this part of the code means that gamemovieclips.swf was loaded
                    // we will then get an instance of CarMC by calling getExportedAsset below
                    _clipInstance = (resource as SWFResource).getExportedAsset("CarMC") as MovieClip;

                    if (_clipInstance) { // check if the class was actually loaded
                        // if it was loaded, you can now do whatever you want with it

                        // for example, I have a child movieclip in CarMC with instance name "wheel" and I want it stopped initially
                        _wheel = _clipInstance.getChildByName("wheel") as MovieClip;
                        _wheel.stop();
                    }

                    _loading = false;
                });

            }

            // return our CarMC instance that will be rendered to the scene/stage
            return _clipInstance;
        }

        private var _loading:Boolean = false; // set to true if we are currently loading a movieclip
        private var _clipInstance:MovieClip = null; // will temporarily hold the loaded movieclip
        private var _wheel:MovieClip = null;
    }
}
</code></pre>

<p>A problem with this method is that I&#8217;m using <code>ResourceManager</code> to load the swf and then the movieclip. This means that loading the 
movieclip is not instantaneous. I did try a different method for this but somehow couldn&#8217;t get the movieclip loaded into PBE.</p>

<h3>Add the component to the level file</h3>

<p>Now that I have a render class (CarRenderComponent). I can add it to any entity with a spatial component. (Please see PBEngineDemo for 
more info on this)</p>

<pre><code>&lt;entity name="car"&gt;
    &lt;component type="com.pblabs.box2D.Box2DSpatialComponent" name="CarSpatial"&gt;
        ....
    &lt;/component&gt;
    &lt;!-- our render component here --&gt;
    &lt;component type="com.game.renders.CarRenderComponent" name="SWFRender"&gt;
        &lt;loop&gt;true&lt;/loop&gt; &lt;!-- set to TRUE. The SWFRenderComponent will destroy the CarMC once it's done playing if this is FALSE --&gt;
        &lt;positionReference&gt;@CarSpatial.position&lt;/positionReference&gt; &lt;!-- set the spatial's position as the movieclip's position when rendering --&gt;
        &lt;scaleFactor&gt;0.3&lt;/scaleFactor&gt; &lt;!-- you can resize/scale your MC using this --&gt;
        &lt;screenOffset&gt; &lt;!-- or you can offset it's render position from the @CarSpatial.position --&gt;
            &lt;x&gt;0&lt;/x&gt;
            &lt;y&gt;20&lt;/y&gt;
        &lt;/screenOffset&gt;
    &lt;/component&gt;
&lt;/entity&gt;
</code></pre>

<p>One thing I noticed was the properties <code>&lt;sizeReference&gt;</code> and <code>&lt;rotationReference&gt;</code> didn&#8217;t seem to have any effect on the render. 
I&#8217;ll have look at that another time. Maybe I&#8217;m just missing something.</p>

<p>That&#8217;s it. I was happy enough to see the movieclip embedded, rendered and animated on the scene. Hopefully, this will be of help to someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://shikii.net/blog/experiment-using-swf-movieclips-in-pushbutton-engine/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

