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!

Comments are closed.