Flixel Forums

development => help => Topic started by: luciebara on Tue, Jul 6, 2010

Title: Animated Coins?
Post by: luciebara on Tue, Jul 6, 2010
Hm, i'm with such a doubt...
I am trying to but a animated coin on a project i'm making and studying, but i really cannot find a way to do so.
Actually, i think i made everything ok, but i cannot tell it to "display" the animation, i tried to use play("animation-name") but FB tells me that play doesnt exists, and i'm really stuckd, probably its a simple and stupid way to do it, but i just cant imagine how hahaha

I Tried to make a separated class for coin, but i got confused doing it...

Well, the simplified code of what i have at the moment:

Code: [Select]
package
{
import org.flixel.*;

public class PlayState extends FlxState
{
[Embed(source="data/coin.png")] private var ImgCoin:Class;

public var _coins:FlxGroup;


override public function create():void
{

//Coins
_coins = new FlxGroup();
_coins.add(createCoin(9,9));
add(_coins);
}

public function createCoin(X:uint,Y:uint):FlxSprite
{
return new FlxSprite(16,16).loadGraphic(ImgCoin,true);
addAnimation("coin",[0,1,2,3],5);
}

override public function update():void
{
super.update();

play("coin");
}
}
}
Title: Re: Animated Coins?
Post by: zez on Tue, Jul 6, 2010
your trying to tell the state to play the animation, you want to tell the sprite too. You are either going to want to make a coin class that extends flxsprite, or at the very least assign a variable to the coin and use coin.addAnimation("spin or whatever" , [0,1,2,3,infinity] , framerate); coin.play("spin") after you set it up. I would honestly recommend just making a coin class so you dont have to do that every time you add a coin.
Title: Re: Animated Coins?
Post by: Billy on Tue, Jul 6, 2010
The problem is your createCoin function. Firstly, function execution always stops at a return statement, so the function returns the created object and quits right there. Your addAnimation call is never executed.

Secondly, you're going about it the wrong way. addAnimation is a method of the FlxSprite class. The best practice is to create a class for each broad kind of object you're going to have (a Coin class, Enemy class, etc). Even though it may seem like overkill for a simple coin, it's worthwhile (later you may want to define all kinds of properties on the coin, like varying values kinds of coins or whatever). You can check out some of the example code on Flixel's GitHub repo to see how this works. You create a class which extends FlxSprite. From within that class, you would create the coin's graphic, add its animation and so on

But if you wish to do it the current way, your code should look something like this:

Code: [Select]
public function createCoin(X:uint,Y:uint):FlxSprite
{
 var coin:FlxSprite = new FlxSprite(16,16).loadGraphic(ImgCoin,true);
 coin.addAnimation("coin",[0,1,2,3],5);
 return coin;
}

In your code, you're inside a FlxState class (well, a class that inherits from FlxState). Calling addAnimation from there is calling FlxState.addAnimation(), and there is no such method.

edit: You would also have to loop through each coin in your update function and call its play() method. Again, your code is trying to call FlxState.play(), which is meaningless. You would have to get a reference to each coin object and call its play method. It's much better and much cleaner to create a Coin class and have each one call its own play method
Title: Re: Animated Coins?
Post by: luciebara on Tue, Jul 6, 2010
Hm, still having troubles.

I created the class and so, but i found mysefl lost on adding it to my State, becoz i need it to be a FlxGroup, for adding, and the countDead things, but this way i cant load the class on it.

I'm adding it this way:

Code: [Select]
_coins = new FlxGroup();
_coins.add(new Coin(100,100));
add(_coins);

and the var is a FlxGroup.

Then, the coin appears on the top of the screen, like the i was setted it to 1,1, so i'm really lost ;/
Title: Re: Animated Coins?
Post by: luciebara on Wed, Jul 7, 2010
no one? :(
Title: Re: Animated Coins?
Post by: TahGamer on Wed, Jul 7, 2010
You need to use "coin.addAnimation("coin",[0,1,2,3],5);" as Billy explained. That explains to FB/Flixel that you want to display that animation.
Title: Re: Animated Coins?
Post by: luciebara on Wed, Jul 7, 2010
Actually the animation is working, as i made a Coin separeted class, but, i cant position it on my map, dont matter what cordinates i gave to it, it always keeps on top ;/
Title: Re: Animated Coins?
Post by: luciebara on Sat, Jul 10, 2010
:(
Tryed everything, but it stills not working correctly
Title: Re: Animated Coins?
Post by: lordkuragari on Sat, Jul 10, 2010
what does your coin constructor look like ... maybe its not taking the right var or your not assigning them correctly

im thinking something like this
Code: [Select]
...
import org.flixel.*;
public class Coin extends FlxSprite
{
[Embed(source="../../data/Coin.png")] private var ImgCoin:Class;
public function Coin(X:Number,Y:Number,ThePlayer:Player)
{
super(X,Y);
loadGraphic(ImgCoin, true, true, 16, 16);
                        ...
                        addAnimation("coin", [0,1,2,3,4,5],3);
                 }
Title: Re: Animated Coins?
Post by: zez on Tue, Jul 13, 2010
Im making a wild guess that the problem is in the coin function. Make sure you have
Code: [Select]
public function coin(X:number or int or uint or whatever, Y:see explanation for X, anything else you need your coin to know when its made like value or something):void
{
super(X, Y);
}
otherwise, when you call super, it will set the coins coordinates to the default coordinates (0,0) for a flxsprite when you call super, and everything will be added to the top left corner of the game world.