0

Just found it is possible to give a linkage in Flash Professional IDE for symbols that has Sprite class as parent. Previously I was sure the only option is MovieClip.

The question is:

How to make these symbols to play animation at runtime? When I put it on a stage in IDE I can extend parent's timeline and use Loop or Play First propery to control the animation inside of a sprite. But if I instantiate that Sprite derived class at runtime I have no idea hot to animate it :( There are no play() and I don't know how to link it's timeline to parent's one Please help.

Updated: Probably I missunderstand the difference of Shape and Sprite classes. So I am in need for Shape or Sprite instances :)

Oleksiyka
  • 1
  • 1

2 Answers2

2

Suppose you have a symbol with linkage in your IDE called bear_animation that is a MovieClip.

You can then create an instance of that in your code like such :

// create an instance of your bear_animation symbol in library
var bearAnimation:MovieClip = new bear_animation;

Then you could add it to the stage and play it like so :

addChild(bearAnimation);
bearAnimation.gotoAndPlay(1);

Also, I believe you are confused as to what a Sprite is. A Sprite is a static graphic image, and doesn't have a timeline, so it doesn't animate. Therefore you don't have the option to play an animation.

A MovieClip has a timeline, and therefore can have a collection of frames that you can play. So you would just use :

yourMovieClip.gotoAndPlay(<frame_number_to_start_on>);

That line of code will play the animation of yourMovieClip starting at whatever frame you specify.

You cannot animate a Sprite, so your initial premise of this question in combination with the second part is undoable. If you want to animate, you need a MovieClip. My suggestion is that if you have a symbol with animation, you need to change it to a MovieClip in the IDE.

prototypical
  • 6,731
  • 3
  • 24
  • 34
  • // create an instance of your bear_animation symbol in library var bearAnimation:MovieClip = new bear_animation; Not movieclip. Sprite – Oleksiyka Dec 18 '12 at 09:01
  • Your question asks for a sprite based symbol from the library. Next it says it wants animate it. You CANNOT animate it. I am telling you that if you want to play an animation, you need it to be a MovieClip. I explained that in the answer. This is a two part question and I answered both. – prototypical Dec 18 '12 at 18:47
  • ok. ok. I got it. But, we can create graphics symbol on main timeline in FlashProfessioan IDE, we can put 10 frames in it and make any animation in it. executing: `code` trace( numChildren ); var c:DisplayObject = this.getChildAt(0); trace( describeType( c ) ); `code` we have: As Sprite, Shape don't have gotoAndPlay method too. But it Can have animation in it. So maybe there are a trick to have dynamicaly created Shape or Sprite. – Oleksiyka Dec 19 '12 at 23:44
  • My belief as to why that is so in the IDE is for flexibility. I don't believe you can even set linkage on a graphic symbol as they are meant to be building blocks for sprites/movieclips. – prototypical Dec 20 '12 at 00:02
  • I am confused as to what you are trying to solve. Why not just use a MovieClip ? You want an animated symbol, use what is designed for that purpose, right ? – prototypical Dec 20 '12 at 00:04
  • Purpose is simple. MovieClip is rendered very-very slow on android devices (ppl say iOs devices are affected too). We achieved huge fps boost when removed or converted to Sprites not needed MCs. But we have TONS of UI made 100% in flash using MovieClips, and I don't like the idea of rebuilding whole UI using sprites and by-hand gathering of window. – Oleksiyka Dec 22 '12 at 01:18
  • Ah, now you are describing your issue better. It's a whole different question, right? What you just said here needs it's own question with a proper description of what you are trying to solve. How to set linkage as a Sprite and have a Sprite play an animation is not the question to ask. – prototypical Dec 22 '12 at 05:40
  • You can cast a MovieClip as a Sprite ie : var sprite:Sprite = new yourLinkageClassName as Sprite; But I think what you are looking to do if you have animation and performance is the issue, is blitting. – prototypical Dec 22 '12 at 05:51
1

As others have mentioned, a Sprite object has no timeline/frames in it. From the Sprite class api reference:

A Sprite object is similar to a movie clip, but does not have a timeline. Sprite is an appropriate base class for objects that do not require timelines. For example, Sprite would be a logical base class for user interface (UI) components that typically do not use the timeline.

The class does not have any method to play frames or any that even refer to frames. If you need frames or a timeline, use a Movieclip:

From Movieclip api reference:

Unlike the Sprite object, a MovieClip object has a timeline.

A sprite basically only has one frame in it. To animate objects inside a sprite, you would have to animate it by code. Though I've never actually tried it, I think if you cast a Movieclip (with frames) in to a Sprite, it'll be stuck on the first frame.

mitim
  • 3,169
  • 5
  • 21
  • 25