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

Scrolling Background...FINALLY

Sign in to follow this  
Tesseract

231 views

Well, summer is winding down and I am slowly moving my brain back inside, out of the sun and toward the computer.

I figured out a couple of things today at work, and the applied the results to the Parallax Sprite project: Now I have a scrolling background.

The code for it was, after all is said and done, pretty simple. Once I figured out how some of the Actionscript worked, everything else fell into place.

package {
import flash.display.*;
import flash.events.*
import flash.net.URLRequest;
import flash.geom.*;
[SWF(backgroundColor="0x000000",width="640",height="480",frameRate="50")]
public class World extends Sprite {
private var sprites:Array = [];
private var totalSprites:Number = 5;
private var maxHeight:Number = 0;
public var centerX:Number;
public var centerY:Number;

private var dX:Number;
private var dY:Number;
private var dOff:Number;
private var velocity:Number;
private var theta:Number;
private var mat:Matrix;
private var bmp:BitmapData;
private var _loader:Loader = new Loader();
public var spriteBitmap:BitmapData;

[Embed("ground.jpg")]
private var Ground:Class;

public function World() {
addEventListener(Event.ENTER_FRAME,grabBitmap);
}

private function grabBitmap(e:Event):void {
if(!stage) return;
removeEventListener(Event.ENTER_FRAME,grabBitmap);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,init);
_loader.load(new URLRequest("brick_pattern_1.gif"));

}

private function init(e:Event):void {
_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,init);
var _i:Bitmap = Bitmap(_loader.content);
spriteBitmap = _i.bitmapData;
mat = new Matrix();
centerX = stage.stageWidth/2;
centerY = stage.stageHeight/2;

var g:Bitmap = new Ground();
bmp = new BitmapData(g.width,g.height);

bmp.draw(g);
graphics.beginBitmapFill(bmp,mat,true,false);
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
graphics.endFill();

graphics.beginFill(0xff0000,100);
graphics.drawCircle(centerX,centerY,2.5);
graphics.endFill();

for(var i:Number = 0;i sprites = new ParallaxSprite(
this,
Math.round(Math.random()*stage.stageWidth*2/120)*120,
Math.round(Math.random()*stage.stageHeight*2/120)*120
);
sprites.cacheAsBitmap = true;
maxHeight = Math.max(sprites.layers,maxHeight);
}

for(i = 0;i for(var j:Number = 0;j if(sprites[j].spriteLayers) {
addChild(sprites[j].spriteLayers);
sprites[j].spriteLayers.cacheAsBitmap = true;
}
}
}
addEventListener(Event.ENTER_FRAME,moveSprites);
}

private function moveSprites(e:Event):void {
dX = centerX - mouseX;
dY = centerY - mouseY;
theta = Math.atan2(dY,dX);
dOff = Math.sqrt(dX*dX+dY*dY);
velocity = dOff/50;
/* begin work on ground layer */
mat.translate(Math.cos(theta)*velocity,Math.sin(theta)*velocity);
graphics.clear();
graphics.beginBitmapFill(bmp,mat,true,false);
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
graphics.endFill();
/* end work on ground layer */
for(var i:Number = 0;i sprites.x += Math.cos(theta)*velocity;
sprites.y += Math.sin(theta)*velocity;
if(sprites.x < -360) sprites.x = stage.stageWidth + 360;
else if(sprites.x > stage.stageWidth+360) sprites.x = -360;
if(sprites.y < -360) sprites.y = stage.stageHeight + 360;
else if(sprites.y > stage.stageHeight+360) sprites.y = -360;
sprites.detectCenter();
}

}
}
}


Everything I needed to know happens in the section inside of moveSprites() which is bracketed by comments.

I suppose the next project is to get an avatar of some kind in there walking around in the world. Then collision detection. Then varied terrain. Then monsters. Then combat. Then a story layer. Then...thenthenthen...

Argh.
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