Flixel Forums

development => releases => Topic started by: Gama11 on Tue, Aug 28, 2012

Title: FlxTrail.as
Post by: Gama11 on Tue, Aug 28, 2012
Apparently, this got ported to and integrated into Haxe-Flixel, which is pretty awesome. There's also a demo of it there (http://www.haxeflixel.com/demos/flxtrail) showing off how awesome it works in conjunction with Tweening.

GitHub repo of the demo-swf (https://github.com/Gama11/FlxTrail-FlxSlider-Demo)

I have created a handy class to create trails that follow FlxSprites. (It's basically a FlxGroup which stores the positions and angles of a sprite in arrays. It then creates a number of Sprites whose positions & angles are updated after a set amount of frames) It's probably not all too fancy or even efficient performance-wise, but after I've enjoyed using the flixel framework so much, I figured it's about time to give something back to this great community. ;) I also created a little Demo swf which allows you to play around with the different paramters a bit.

In case you wanna see some more practical use of the class, my Ludum Dare #24 entry "viviDNA (http://www.kongregate.com/games/Gama11/vividna)" heavily depends on this class.

https://dl.dropbox.com/u/18627942/Demo.swf

Feel free to leave feedback and suggestions, use the class yourself and / or customize it according to your needs. :)

FlxTrail.as

Click here for the source code (https://raw.github.com/Gama11/FlxTrail-FlxSlider-Demo/master/src/FlxTrail.as)

Usage:

Code: [Select]
// Assigning a trail to a sprite called "ball". The trailimage is "ballImage", the number of sprites is "5", and the trails position will update every 2 frames.
// The first trailsprite was an alpha value of "0.6", "0.02" is sutracted from that alpha value for each subsequent sprite
var ball:FlxSprite = new FlxSprite(0, 0, ballImage);
var trail:FlxTrail = new FlxTrail(ball, ballImage, 5, 2, 0.6, 0.02);
add(trail);
add(ball);

// Sprite needs to move for you to see the trail
ball.velocity.y = 300;
ball.velocity.x = 300;

// Change the image of all trailsprites
trail.changeGraphic(newImage);

// Increase the length of the trail by 5 sprites
trail.increaseLength(5);
Title: Re: FlxTrail
Post by: paala on Tue, Aug 28, 2012
hi, this is great.
Can you post the demoSuite source code to show who this is used.
Thanks
Title: Re: FlxTrail
Post by: test84 on Tue, Aug 28, 2012
That's great, as in my current game the player moves fast in certain situations.

hi, this is great.
Can you post the demoSuite source code to show who this is used.
Thanks

I second it.
Title: Re: FlxTrail
Post by: Gama11 on Tue, Aug 28, 2012
Thx for your feedback, glad you like it. The demo suite source code is pretty ugly, so I'd rather not publish that one, I think it would confuse ppl more than anything. I could probably fix it though, if you really want me to.
For now, I've added another code-field to my first post titled "usage", which should allow you to make use of the features the class has. Plus, pretty much all the code in that class is commented, so that should help too. Usage is fairly straigthforward, you add the trail just like any other FlxGroup. (You need at least two parameters, sprite to follow and image to use, though)
Title: Re: FlxTrail
Post by: test84 on Wed, Aug 29, 2012
Great, I'll put it to use in about 3 weeks and report back! lovely.
Title: Re: FlxTrail
Post by: Gama11 on Wed, Aug 29, 2012
Great, I'll put it to use in about 3 weeks and report back! lovely.

Awesome, can't wait to see some games using this. :D
Title: Re: FlxTrail
Post by: xhunterko on Fri, Aug 31, 2012
Sweet!
Title: Re: FlxTrail
Post by: Esti on Wed, Sep 5, 2012
Very nice effect! thanks.
Title: Re: FlxTrail.as
Post by: Gama11 on Fri, Dec 21, 2012
Pushed the whole demo-swf to a GitHub repo (https://github.com/Gama11/FlxTrail-FlxSlider-Demo), as I thought that would be a convenient thing to do. Also haven't really used Git before, so this was a good opportunity.
Title: Re: FlxTrail.as
Post by: snakebee on Wed, Jun 5, 2013
Thanks for sharing this! I tried using this in my game today, but it kept giving me an error. I even tried doing it in an almost identical way to the example code. I ended up adding a single statement to make it work.

Code: [Select]
public function increaseLength(amount:int):void
{
// Can't create less than 1 sprite obviously
if (amount <= 0) return;

trailLength += amount;

// Create the trail sprites
for (var i:int = 0; i < amount; i++) {
var trailSprite:FlxSprite = new FlxSprite(0, 0);

if (image == null) trailSprite.pixels = sprite.pixels;
else trailSprite.loadGraphic(image);

trailSprite.alpha = transp;
transp -= difference;

if (trailSprite.alpha <= 0) trailSprite.kill();

add(trailSprite); // this makes it work
}
}

Hope it helps out someone else!
Title: Re: FlxTrail.as
Post by: Gama11 on Thu, Jun 6, 2013
@snakebee: Thanks for spotting that! Seems like I forgot that in the last update, just fixed it.