Change spritemap to another file?


(John Andersson) #1
	[Embed(source="../../assets/graphics/blabla.png")]
	private const HERO:Class;		

	[Embed(source="../../assets/graphics/blabla_powerup.png")]
	private const HERO_2:Class;		

        public var spritemap:Spritemap = new Spritemap(HERO, 20, 20);

later on

        graphic = spritemap

standard code. however, I want a powerup to change the spritemap into another one, as I find it easier to simply change spritemaps instead of adding animations to one big-ass spritemap, so I tried (in the update function)

		if (changeSpritemap)
		{
			if (HeroStats.powerup_active) spritemap = new Spritemap(HERO_2, 20, 20);
			else spritemap = new Spritemap(HERO, 20, 20);
			changeSpritemap = false;
			graphic = spritemap;
		}

changeSpritemap is set to true whenever you pick up a powerup, and the spritemap seems to change just fine, but the animation is frozen, it doesn’t change or update in any way after it has been set to the powerup file…

I assume it’s because I haven’t set

		spritemap.add("stand", [0], 0, false);
		spritemap.add("run", [10, 11, 12, 13, 14, 15], 15, true);
		spritemap.add("jump", [40, 41, 42, 43], 10, true);
		spritemap.add("glide", [20, 21], 15, true); etc

for the “new” spritemap, but why would hey get removed when the spritemap simply changes the file? is there any way to keep these animations?

to explain why I would want to use different pngs for different sets of animations, is because I already have a ton of animations set (stand, run, jump, glide and maybe 40 more), if I expand the spritemap with the powerup ones, then I will have a total of 80 animations to keep track of… instead of only having 40 as before with different files


(Martí Angelats i Ribera) #2

I’m not sure if this is the best option becouse having all in a single spritemap is easier.

But if i would want to have two of them i would probably do something like this:

//set the non-powered up
spritemap1 = new Spritemap(HERO, 20, 20);
spritemap1.add("stand", [0], 0, false);
spritemap1.add("run", [10, 11, 12, 13, 14, 15], 15, true);
spritemap1.add("jump", [40, 41, 42, 43], 10, true);
spritemap1.add("glide", [20, 21], 15, true);
//etc.

//set the powered up
spritemap2 = new Spritemap(HERO_2, 20, 20);
spritemap2.add("stand", [0], 0, false);
spritemap2.add("run", [10, 11, 12, 13, 14, 15], 15, true);
spritemap2.add("jump", [40, 41, 42, 43], 10, true);
spritemap2.add("glide", [20, 21], 15, true);
//etc.

Then change between one and the other:

if (changeSpritemap)
{
	changeSpritemap = false;
	
	//save the values of the animation
	var animation:String (graphic as Spritemap).currentAnimation;
	var frame:int = (graphic as Spritemap).frame;
	
	//set the correct graphic
	if (HeroStats.powerup_active) graphic = spritemap2;
	else graphic = spritemap1;
	
	//plays the same animation at the same frame
	(graphic as Spritemap).play(animation, true, frame);
}

Be sure that the animations have the same names (i recomend to just copy-paste and change the number) and to set a default animation or it might throw an error.

PD: To evoid errors you only have to add in the constructor:

//after spritemap1 and spritemap2 have been setted
graphic = spritemap1;
spritemap1.play("stand");

PD2: I hope it helps :smile:


(John Andersson) #3

Thank you, I will try your solution as soon as I get home. One question though, you say it’s easier having everything in a single spritemap, but what if I want to have some big-ass sprites combined with smaller ones?

 spritemap1 = new Spritemap(HERO, 20, 20);

20, 20 already sets the size, how can I make exceptions for bigger portions of the image?

thanks :stuck_out_tongue:


(Martí Angelats i Ribera) #4

It’s easy. You only get the two images and put them together one behind the other (you can use different rows). Then set all the animations with aname that starts with something that determines if its poweredUp or not. Then the name of the animation.

// in this example i use "normal_" and "powerup_" to define if it is powered up or not

spritemap = new Spritemap(HERO, 20, 20);
spritemap.add("normal_stand", [0], 0, flase);
spritemap.add("powerup_stand", [44], 0, false); //the number is invented
//and continue with the animations


//Then the animation name can be defined as (this is not a complete line of code thought)
(HeroStats.powerup_active?"powerup_":"normal_") + animationName

PD: Something is wrong with the comentaries. I don’t know why they are shown this way.

PD2: If you want to end with an specific string instead of starting with another, you can do it. It’s the same.

PD3: You could actually end with 0 or 1 (1 for power up) and then do:

animationName = animation + HeroStats.powerup_active;

Maybe it is an easier code.


(John Andersson) #5

This is annoying the heck out of me now, why can’t just

	[Embed(source="../../assets/graphics/enemies/enemy/enemy.png")]
	private const ENEMY:Class;		
	[Embed(source="../../assets/graphics/enemies/enemy/enemy_ice.png")]
	private const ENEMY_ICE:Class;	

	public var spritemap:Spritemap;


	public function Enemy(p:Point)
	{
                if (GameWorld.isNormalLevel) spritemap = new Spritemap(ENEMY, 16, 16);
		if (GameWorld.isIceLevel) spritemap = new Spritemap(ENEMY_ICE, 16, 16);

                spritemap.add("stand", [0, 1, 2, 3], 0, false);
		spritemap.add("run", [5, 6, 7, 8, 9], 25, true);
        }

work? I get null reference errors on the spritemap.add lines, why can’t changing the file be simple? Why would I have to define several different spritemaps, when one variable should be able to hold different files when needed?

If I define spritemap1 and spritemap2 instead of spritemap, then I have to change A LOT of code. All of the spritemap.play(“animationHere”) would need both a spritemap1 and spritemap2 version.

And if I just make one huge spritemap which has all of the different skins in one file, then I need to add A TON of

spritemap.add("skin1_run")
spritemap.add("skin2_run")
spritemap.add("skin1_stand")
spritemap.add("skin2_stand");

etc etc and ALSO check for when to use what animation, it just feels really unnecessary. Is there any sort of EASY way to just change the spritemap the way I want?


(Martí Angelats i Ribera) #6
//i'll say that if is not ice / other special type i must be normal so
if (GameWorld.isIceLevel)
{
spritemap = new Spritemap(ENEMY_ICE, 16, 16);
}
else
{
spritemap = new Spritemap(ENEMY, 16, 16);
}

You always have to have a default value to avoid errors like that. Use if .. else if ... else ... if one condition excludes the other. The final else is the default you want.