Parallax: A simple solution

Posted by aBethke on Friday Jun 18, 2010 Under Bag o' Tricks, Game Development

Recently when I was working on Star Fall I needed to develop a basic Parallax implementation as part of the game design. Ive done this type of implementation before but never built anything reusable. In fact, I’d love to tell you now that I did just that when developing Star Fall but that, sadly my friends, would be a lie. I have however taken the code I wrote for Star Fall and developed it into a very basic Parallax system which I offer up now to the gods of the interwebs and anyone else that might be interested.

The basic breakdown is pretty simple. You instance the SimpleParallax class and then begin adding layers to it and defining their properties via the methods I have created. The layer handling system uses a name to keep track of the layers. When you create a layer you assign its name and then reference it layer via the same name. There is no active depth sorting, simply create the layers in the order you wish them to appear.

[cc lang=”actionscript3″ line_numbers=”true” no_links=”true” width=”100%”]
package com.goldengear.core
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;

public class SimpleParallax extends Sprite
{
public function SimpleParallax()
{
mouseEnabled = false;
mouseChildren = false;
addEventListener(Event.ENTER_FRAME, handleEnterFrame, false, 0, true);
}
protected function handleEnterFrame(in_event:Event):void
{
var i:int;
var num:int;
var layer:DisplayObject;
var layerName:String;
var scrollSpeed:Number;
var loopX:Number;

if (!isPaused)
{
num = m_layerNames.length;
for (i=0; iSimpleParallax – Test Application

As I poke at this class more I will probably release the updated versions. Enjoy!

Tags : , , | Comments Off on Parallax: A simple solution

This is a trick I picked up recently when I was preparing some games (Mashin’ Meeces and Star Fall) for release on portal sites and saw that it would be a bit easier (and required by some) to figure out how to change my multi-SWF application into a single SWF app.

Mashin’ Meeces was originally broken into three SWFs: game code, art assets and sounds. I did this so that I could work on the game independently from the sound or art since those were being taken care of by other people. Through my previous experience developing games I had never run into a problem with having games broken into multiple SWFs before but those projects have all been for private sites and game portals like Newgrounds and Kongregate are a new experience for me. After talking with my colleagues, doing some research and poking at the code for a bit I finally got the solution of embedding the SWFs into my main application and then load them using loadBytes.

Here is the code I used to embed the two SWFs (art and sounds) into the main class of my ActionScript project.

[cc lang=”actionscript3″ line_numbers=”true” no_links=”true” width=”100%”]
[SWF(width=’800′, height=’600′, backgroundColor=’0xffffff’, frameRate=’30’)]
public class MashinMeeces extends Sprite
{
[Embed(source=”MashinMeecesAssets.swf”, mimeType=”application/octet-stream”)]
private var swfAssets:Class;
[Embed(source=”MashinMeecesSounds.swf”, mimeType=”application/octet-stream”)]
private var swfSounds:Class;

public function MashinMeeces()
{
// Rawr! I am the constructor…
}
}
[/cc]

For the loading I had just been using a standard Loader implementation and to convert the pre-existing code all I had to do was drop the URLRequest() and change load() to loadBytes().

[cc lang=”actionscript3″ line_numbers=”true” no_links=”true” width=”100%”]
protected function addedToStage(in_event:Event):void
{
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);

var assetLoader:Loader = new Loader();
addChild(assetLoader);
assetLoader.visible = false;
assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, assetsLoaded, false, 0, true);
assetLoader.loadBytes(new swfAssets(), loaderContext);
}
[/cc]

Tags : , , , | Comments Off on Actionscript Project Refactors: Multiple Swf to Single Swf Loading