How get current animation of another class? [SOLVED]


(John Andersson) #1

I’m having trouble with static vars.

I have a spritemap in a hero class, and I have a separate gloves class.

I want to make it so that the gloves class plays an animation equal to the hero class. A.k.a one with the same name.

Hero:

	[Embed(source = 'assets/Hero_Full.png')] private const HERO:Class;
	
	public var sprHero:Spritemap = new Spritemap(HERO, 120, 136);

Gloves:

		var heroAnimation:String = Hero.sprHeroIsntHere-HowDoIgetIt?

                    sprGloves.play(heroAnimation);

Of course, I can make sprHero a public static var, but if I do that, then I get this error:

“Access of undefined property HERO”.

I assume I can do something like this:

			var heroCollide:Hero = Hero(collide("hero", x, y));

			if (heroCollide != null)
			{
				_equipped = true;
				
				spritemap.play(heroCollide.sprHero.currentAnim);
			}

But I want the spritemap to play the same animation as sprHero even if it isn’t colliding!

This is very confusing. Thanks


(billy2000) #2

1st give your hero class a name,in constructor:

name="hero";

Now in your gloves class do something like this:

var hero_:Hero= world.getInstance("hero");
spritemap.play(hero_.sprHero.currentAnim);

(azrafe7) #3

Yes, I see it may be confusing at first. But there are multiple ways to solve your problem.

As a first thing I think you’re trying to reference HERO - which is private const HERO:Class -, while you probably want Hero, that I assume you have stored somewhere as public static (probably in your World class).

One solution would be to somehow pass the owner of the Gloves instance to get a handle on it (either in the constructor or in some other way). For example:

  gloves.owner = hero;

  // then in gloves update()
  if (owner != null) {
    sprGloves.play(owner.heroAnimation);
  }

You could also activate the proper animation right from the Hero, since it should know it is equipped with gloves.

In any case I’d recommend to read more about OOP, static members/methods, dependency injection and look through other programmers game code to try and figure out how they’re addressing problems like this (the tutorials section of the forums should also have plenty of useful insights). Don’t get me wrong about this little rant, I’ve built my own little knowledge with these rules (expecially reading others code - google+github is a goldmine! :smirk:).


(John Andersson) #4

Thank you both so much.

I’ll buy a book on OOP ^^


(azrafe7) #5

The internets are full of tutorials and solutions to common problems (stackoverflow anyone?), so no need to buy a book (unless you feel more comfortable reading off the screen).

By the way keep posting your issues, we’ll be glad to help!


(John Andersson) #6

I love you guys…

(L)

If I ever get rich off of making games, you’ll most definitely get some in your PayPal :stuck_out_tongue: I’m serious