Sign in to follow this  
  • entries
    90
  • comments
    66
  • views
    72301

Parallax Sprite

Sign in to follow this  
Tesseract

126 views

I just posted another experiment. This one is the beginnings of a parallax sprite engine, inspired by the Dixie Chicks Underground website created by WeFail. Seems easy enough so far, although I have done nothing but throw up some layered graphics.

You will need the Flash 9 plugin to see this one.

Here is the rough draft of the ParallaxSprite class:
package {
import flash.display.*

public class ParallaxSprite extends Sprite {
private var layers:Number;
private var offsetRatio:Number = 1.01;
private var sizeOffset:Number = 1.1;
private var maxCenterDistance:Number;
private var world:World;
private var dX:Number = 0;
private var dY:Number = 0;
private var d:Number = 0;
private var r:Number = 0;
private var spriteLayers:Array = new Array();
public function ParallaxSprite($world:World,$x:Number,$y:Number) {
world = $world;
x = $x;
y = $y;
maxCenterDistance = Math.min(world.centerX,world.centerY)/2
init();
}

private function init():void {
layers = Math.round(Math.random()*30)+30;
var rW:Number = Math.round(Math.random()*25)+25;
var rH:Number = Math.round(Math.random()*30)+20;
var rC:Number = Math.round(Math.random()*10)*0x111111;
for(var i:Number = 0;i spriteLayers = new Shape();
if(i%10==0 || i==layers-1) spriteLayers.graphics.lineStyle(1,0xededed,100);
spriteLayers.graphics.beginFill(rC,100);
spriteLayers.graphics.drawRect(-rW/2 * sizeOffset,-rH/2 * sizeOffset,rW * sizeOffset,rH * sizeOffset);
spriteLayers.graphics.endFill();
spriteLayers.x = i;
spriteLayers.y = i;
spriteLayers.cacheAsBitmap=true;
addChild(spriteLayers);
sizeOffset *= offsetRatio;
}
}

public function detectCenter():void {
dX = x - world.centerX;
dY = y - world.centerY;
d = Math.sqrt(dX*dX+dY*dY);
var o:Number = d/maxCenterDistance*offsetRatio;
r = Math.atan2(dY,dX);
for(var i:Number = 0;i spriteLayers.x = i*o*Math.cos(r);
spriteLayers.y = i*o*Math.sin(r);
}
}

}
}


And here is the World class which creates and powers the ParallaxSprites:

package {
import flash.display.Sprite;
import flash.events.*
[SWF(backgroundColor="0x000000",width="640",height="640",frameRate="50")]
public class World extends Sprite {
private var sprites:Array = [];
private var totalSprites:Number = 20;
public var centerX:Number;
public var centerY:Number;
public function World() {
addEventListener(Event.ENTER_FRAME,init);
}

private function init(e:Event):void {
if(!stage) return;
removeEventListener(Event.ENTER_FRAME,init);
centerX = stage.stageWidth/2;
centerY = stage.stageHeight/2;
graphics.beginFill(0xff0000,100);
graphics.drawCircle(centerX,centerY,5);
graphics.endFill();
for(var i:Number = 0;i sprites = new ParallaxSprite(
this,
Math.round(Math.random()*stage.stageWidth/50)*50,
Math.round(Math.random()*stage.stageHeight/20)*20
);
sprites.cacheAsBitmap = true;
addChild(sprites);
}
addEventListener(Event.ENTER_FRAME,moveSprites);
}

private function moveSprites(e:Event):void {
for(var i:Number = 0;i sprites.x--;
if(sprites.x < -30) {
sprites.x = stage.stageWidth + 30;
sprites.y = Math.round(Math.random()*stage.stageHeight-40)+20;
}
sprites.detectCenter();
}
}
}
}
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now