Author Topic: Good Flixel code logic for animating FlxSprites?  (Read 1406 times)

VictorGrunn

  • Member
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Here's something I've been trying to get down properly with flixel.

I'm trying to find out good ways to move the sprites in ways that get effects similar to motion tweens (for menus), or just plain good flight patterns for shooter games. Say, have a menu slide in from offscreen to the right, go off to the left too far, then rubberband back and snap in place. Or, for shooters, maybe have an enemy move in a smooth arcing motion, stop, aim at the player, fire, then fly off again.

I can see how to accomplish some of this in update. For the menu example, start off with velocity.x of (say) -200, then check to see if the menu.x is less than or equal to some point, then start adding to the velocity until x = desired position, then velocity.x = 0.

I can vaguely see how to accomplish it somewhat with sprites. Using trig for wave patterns of course, or similar tricks.

The question is, what's a good way to go about this? Am I on the right track? Is there a vastly better way? I'm still pretty new to all this, but learning as much as I can. So if anyone can give tips or help, it would be appreciated. For all I know the sort of things I'm trying to do are just things Flixel's not really meant for doing, etc.

Alextfish

  • Active Member
  • ***
  • Posts: 174
  • Karma: +0/-0
    • View Profile
Re: Good Flixel code logic for animating FlxSprites?
« Reply #1 on: Mon, Nov 28, 2011 »
Well, one thing to note is that FlxSprites have acceleration as well as velocity; so you don't need to repeatedly add something to the velocity, as Flixel will do that for you. Just set the acceleration at two or three points.

You can use trig for wave patterns, but you can accomplish a lot of similar effects just using acceleration. E.g. if you have an enemy with a constant x velocity, but each step you set its y acceleration proportional to (K minus this enemy's y position), that'll give you a sinusoidal wave across the screen.

More complex effects I think you will have to code yourself. There is a class called FlxPath, which you can create with FlxTilemap.findPath or a set of points; and you can call the followPath method on a FlxSprite to have it follow such a path automatically. I haven't used this myself, but wg/funstorm recently wrote a tutorial on it, so it could be worth checking out http://www.funstormgames.com/blog/2011/11/flixel-drawing-sprite-path-attack-radius-part-12/ .
« Last Edit: Mon, Nov 28, 2011 by Alextfish »

cai

  • Contributor
  • ****
  • Posts: 465
  • Karma: +0/-0
  • the illest of villains
    • View Profile
    • Brandon Cash
Re: Good Flixel code logic for animating FlxSprites?
« Reply #2 on: Mon, Nov 28, 2011 »
Rather than doing it manually, I'd suggest using a tweening library like actuate.  It's free to use for all projects, as it is under the MIT license (the same license as flixel).  Another popular tweening library is TweenLite, but it requires a special license for use in commercial projects.
Follow me on Twitter | Come join us at #flixel on irc.freenode.net!

ETG

  • Active Member
  • ***
  • Posts: 115
  • Karma: +0/-0
    • View Profile
Re: Good Flixel code logic for animating FlxSprites?
« Reply #3 on: Mon, Nov 28, 2011 »
Tweening is the animator's word for interpolation, so if you search the web for that, you might get more results.

The part that might give you some pause is the approach to the problem. Steering behavior answers the question "where do I go from here?" While interpolation answers "how far should I be by now?"

Of course you can easily do both: "move towards where I'm supposed to be."

VictorGrunn

  • Member
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Re: Good Flixel code logic for animating FlxSprites?
« Reply #4 on: Mon, Nov 28, 2011 »
Thanks for the tips. I actually wasn't aware that Flixel could be used with TweenMax at all - I figured it wouldn't work with anything that wasn't a straight up MovieClip, etc. (Probably a result of me trying to use things like Blur at first in combination with everything else, so when that didn't work, I figured nothing did. Also I'm still pretty much a coding novice.)

Thanks for the tips regarding the tweening programs and the general logic. Forever ago I used a tutorial which altered speed by means of something like velocity = velocity * .98 to slow down, and it got ingrained there.