In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Another Random Curving Road

Randomly generates a curving road and then displays the current road segment 
plus the full road map.
This is just a test for a little driving game I'm making.
Get Adobe Flash player
by PESakaTFM 20 Nov 2009
    Embed
/**
Randomly generates a curving road and then displays the current road segment 
plus the full road map.
This is just a test for a little driving game I'm making.
**/
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	import net.hires.debug.Stats;
	
	[SWF(width=500, height=500, frameRate=30)]
	public class CurvingRoad extends Sprite
	{
	        //used to display road
		public var bmp:Bitmap;
		public var bmd:BitmapData;
		public var mc:Sprite = new Sprite();
		
		//used to store road
		public var road:ByteArray = new ByteArray();
		public var visibleRoad:Array = new Array();
		
		//used to calculate current road segment direction and curvature
		public var dir:Point = new Point();
		public var deltaDir:Point = new Point();
		public var altDir:Point = new Point();
		public var altDeltaDir:Point = new Point();
		public var matrix:Matrix = new Matrix();
		
		public var roadPiece:Sprite;
		
		//How long the road segment going to be
		public const SEGMENT_LENGTH:int = 25;
		
		public function CurvingRoad()
		{
			addChild( new Stats() );
			
			this.roadPiece = drawRoadPiece();
			
			bmd = new BitmapData(500,500,true,0x00000000);
			bmp = new Bitmap(bmd);
			this.addChild(bmp);
			
			var degrees:int = 0;
			var i:int
			var delta:int
			road.writeInt(degrees);
			
			//start road off going down for 50 pixels
			i = 50;
			delta = 0;
			for( ; i>0; i--)
			{
				degrees += delta;
				road.writeInt(degrees);
			}
			//randomly curve around for a while
			for(var j:int=0; j<20; j++)
			{
				i = 60;
				delta = Math.random()*6-3;
				for( ; i>0; i--)
				{
					degrees += delta;
					road.writeInt(degrees);
				}
				degrees = degrees%360;
				if(degrees < 0) degrees = 360 + degrees;
			}
			//come back to a downward direction for smooth looping
			i = degrees;
			delta = -1;
			for( ; i>0; i--)
			{
				degrees += delta;
				road.writeInt(degrees);
			}
			
			setupFullTrack();
			road.position=0;
			
			//initial fill of array
			for(i=0; i<SEGMENT_LENGTH; i++)
			{
				visibleRoad[i] = road.readInt();
			}
			
			this.addEventListener(Event.ENTER_FRAME, onEnter);
		}
		
		protected function drawRoadPiece():Sprite
		{
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill(0x303030,1);
			sprite.graphics.drawRect(-25,-10,50,20);
			sprite.graphics.endFill();
			sprite.graphics.lineStyle(3,0xFFFF00);
			sprite.graphics.moveTo(0,5);
			sprite.graphics.lineTo(0,-5);
			
			return sprite;
		}
		
		protected function setupFullTrack():void
		{
			mc.graphics.lineStyle(8);
			mc.x = 150;
			mc.y = 150;
			this.addChild(mc);
		}
		
		protected function onEnter(e:Event):void
		{
			if(road.position == road.length)
			{
				road.position = 0;
				mc.graphics.clear();
				mc.graphics.lineStyle(8);
				altDir.x = altDir.y = 0;
			}
			
			step();
			draw();
		}
		
		protected function step():void
		{
			var i:int = road.readInt();
			//draws our full roadmap one step at a time
			altDeltaDir.x = Math.sin(Math.PI*(i)/180);
			altDeltaDir.y = Math.cos(Math.PI*(i)/180);
			altDir.x += altDeltaDir.x;
			altDir.y += altDeltaDir.y;
			mc.graphics.lineTo(altDir.x, altDir.y);
			
			visibleRoad.shift();
			visibleRoad[SEGMENT_LENGTH-1] = i;
		}
		
		protected function draw():void
		{
			dir.x = dir.y = 0;
			var first:int = visibleRoad[0];
			var next:int = 0;
			var i:int=1;
			
			//clears bitmap
			bmd.fillRect(bmd.rect, 0x00000000);
			for( ; i<SEGMENT_LENGTH; i++)
			{
				next = visibleRoad[i];
				deltaDir.x = Math.sin(Math.PI*(first-next)/180);
				deltaDir.y = Math.cos(Math.PI*(first-next)/180);
				dir.x += deltaDir.x*10;
				dir.y += deltaDir.y*10;
				
				matrix.a = deltaDir.y;
				matrix.b = deltaDir.x;
				matrix.c = -deltaDir.x;
				matrix.d = deltaDir.y;
				matrix.tx = 250+dir.x;
				matrix.ty = 500-dir.y;
				
				bmd.draw(this.roadPiece, matrix);
			}
		}
	}
}