Author Topic: FlxTrail.as  (Read 5503 times)

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
FlxTrail.as
« 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 showing off how awesome it works in conjunction with Tweening.

GitHub repo of the demo-swf

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" heavily depends on this class.

<a href="https://dl.dropbox.com/u/18627942/Demo.swf" target="_blank" class="new_win">https://dl.dropbox.com/u/18627942/Demo.swf</a>

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

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);
« Last Edit: Sat, Dec 22, 2012 by Gama11 »

paala

  • Contributor
  • ****
  • Posts: 250
  • Karma: +0/-1
    • View Profile
Re: FlxTrail
« Reply #1 on: Tue, Aug 28, 2012 »
hi, this is great.
Can you post the demoSuite source code to show who this is used.
Thanks

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: FlxTrail
« Reply #2 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.
blog, twitter, Check out my award winning game, Rot Gut:

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: FlxTrail
« Reply #3 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)

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: FlxTrail
« Reply #4 on: Wed, Aug 29, 2012 »
Great, I'll put it to use in about 3 weeks and report back! lovely.
blog, twitter, Check out my award winning game, Rot Gut:

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: FlxTrail
« Reply #5 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

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: FlxTrail
« Reply #6 on: Fri, Aug 31, 2012 »
Sweet!
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

Esti

  • Active Member
  • ***
  • Posts: 104
  • Karma: +0/-0
    • View Profile
Re: FlxTrail
« Reply #7 on: Wed, Sep 5, 2012 »
Very nice effect! thanks.

Check it out on Mochi!

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: FlxTrail.as
« Reply #8 on: Fri, Dec 21, 2012 »
Pushed the whole demo-swf to a GitHub repo, as I thought that would be a convenient thing to do. Also haven't really used Git before, so this was a good opportunity.

snakebee

  • New Member
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: FlxTrail.as
« Reply #9 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!

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: FlxTrail.as
« Reply #10 on: Thu, Jun 6, 2013 »
@snakebee: Thanks for spotting that! Seems like I forgot that in the last update, just fixed it.