I need a simple sprite with a small trail for a game i'm working on so i had to create some way of tracking sprites and giving them a trail. Heres the simple demo i've created - i wonder if someone could turn it into a game idea? It's just some round blobs "swimming" from left to right. It's pretty rough, but atleast you can see what i mean by sprite "trails".



      Berlow is the code from the demo above. You'll need the AIA component library (which now contains the TrailSprite class) which you can get here. You can also download the demo with flash develop project in a zip here.

 

package  
{
	import flash.display.Sprite;
	import com.adventuresInActionscript.graphics.TrailSprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import com.robertPenner.transitions.easing.Quad;

	public class Main extends Sprite
	{
		
		public function Main() 
		{
			var timer:Timer = new Timer(100);
			timer.addEventListener(TimerEvent.TIMER, addBall, false, 0, true);
			timer.start();
		}
		
		public function addBall(event:TimerEvent):void
		{
			//create new ball sprite off to the left on a random Y line
			var mainSprite:TrailSprite = new TrailSprite();
			var colour:uint = Math.random() * 0xFFFFFF;
			drawBall(mainSprite, colour, 0.9, 15);
			addChild(mainSprite);
			mainSprite.x = -10;
			mainSprite.y = Math.round(Math.random() * 400) + 40;
			
			//set this sprite off in a random direction
			moveSprite(mainSprite);

			//add two "trail" balls
			var sprite:TrailSprite = new TrailSprite();
			drawBall(sprite, colour, 0.7, 10);
			mainSprite.addTrail(sprite, 2);
			
			sprite = new TrailSprite();
			drawBall(sprite, colour, 0.5, 5);
			mainSprite.addTrail(sprite, 4);			
		}
		
		public function moveSprite(sprite:TrailSprite):void
		{
			//"bounce" ball to new x position unless it's off the screen to the right
			if (sprite.x < 655)
			{
				var destX:int = sprite.x + Math.round(Math.random() * 50) + 50;
				var destY:int = sprite.y;
				var curveX:int = (destX + sprite.x) / 2;
				var curveY:int = (destY + sprite.y) / 2;
				if (Math.random() > 0.5)
					curveY -= ((70 * ((655 - destX) / 655)) + 20);
				else
					curveY += ((70 * ((655 - destX) / 655)) + 20);
				sprite.tweenTo(0.7, {x:destX, y:destY, bezier:[{x:curveX, y:curveY}],ease:Quad.easeInOut, onComplete:this.moveSprite, onCompleteParams:[sprite]} );
			}
			else
			{
				sprite.dispose();
				removeChild(sprite);
			}
		}
		
		public function drawBall(sprite:Sprite, colour:uint, alpha:Number, radius:int):void
		{
			//draw a ball shape in the sprite
			sprite.graphics.beginFill(colour, alpha);
			sprite.graphics.drawCircle(0, 0, radius);
			sprite.graphics.endFill();
		}
		
	}
	
}