开发者

Editing A Library Symbol From ActionScript

开发者 https://www.devze.com 2023-01-01 15:11 出处:网络
In the Flash authoring environment I can edit a library symbol and all on-stage instances based upon it reflect the changes. How can I do the same thing in ActionScript? There seems to be no way to ad

In the Flash authoring environment I can edit a library symbol and all on-stage instances based upon it reflect the changes. How can I do the same thing in ActionScript? There seems to be no way to address a library symbol.

For example:

Inside Flash CS3, I have created a Square.swf file that has 100 instances of the library symbol Square.

Now, Square.swf is loaded into another file BlueSquare.swf and I want to change the Square symbol into a blue square so that all instances of Square become blue.

How do I do this using Actionscript?

Thanks for the he开发者_运维百科lp.


What's in a clip's library symbol is the author-time definition of that object - you can't change it at runtime. Instead the normal approach would be to dynamically change the contents (not definitions) of the clips you want to change, which can be done in various ways, but all the good ways of doing that involve making the dynamically-changing clip understand how to update its appearance. So you need to be able to re-author the changing clips to suit your needs.

If you're loading in an animation that somebody else made, and trying to go through and replace all instances of object A with object B, the only way to achieve that is to traverse through the content's display list looking for A, and when you find one, remove its children and replace them with the the contents of a B. Mind you, for animations that may not really solve your problem, since animations normally add and remove clips frequently, so at any given point you could replace all the "hand" clips with "hand2", but then a frame later new "hand" clips might come into existence. But short of opening up the SWF and changing the binary data inside, there's no other way to dynamically change all of a given object to something else unless the object knows how to change its contents.


If it is only about making sure that the square you are attaching is blue you could use the colorTransform to change its appearance:

var someSquare:Square = new Square();
someSquare.transform.colorTransform = new ColorTransform(0,0,0,1,0x00,0x00,0xff,0x00 );
addChild( someSquare );

Of course this does not change the color of all instances that you have already attached.

If you really wanted to change the actual SWF symbol in Actionscript the only way I see is to parse the swf with as3swf ( https://github.com/claus/as3swf/wiki ), find the shape tag of the symbol, change it and then load the ByteArray that contains the swf via loader.loadBytes() - but that's admittedly quite a complicated way and you can achieve the same result by simply putting some colorizing code into the shape symbol itself and then trigger the color change via an Event that is broadcasted by your main app.


Of course, if you make custom component, when you change it changes will appear on all instances of that component/class. Here's the example: http://livedocs.adobe.com/flex/3/html/intro_3.html

On the other hand, if you use modules whey pretty much do the same as swf-s you used in Flash, when you rebuild-recompile them changes will reflect on your main application which uses them. Here's th eexample for modules: http://blog.flexexamples.com/2007/08/06/building-a-simple-flex-module/

So MXML/AS component/class are your "symbols" which you can create or drop on stage on fly.
Modules are "movies" you can load and they run on their own with possibility to communicate to main movie.


The closest way of achieving this is to use Bitmaps. If you update the bitmapData they display, they will all update automatically.

However this approach is not good at all. You should maintain application state separately in an object model, and have the visualisation update, if the state changes.

What you want to do, is to misuse a feature for changing graphic appearence at design time, to change application state at runtime. In generally, ideas like these can be thought off as bad.

For example if you take the time to separate the state model and the visualisation layer, it will become fairly easy to save the game state on a server or to synchronize it with other clients to achieve multiuser features.

greetz
back2dos


If you are trying to build an Avatar and user can customize your Avatar parts e.g. hands, legs, face etc. and you want all these assets to be kept in separate swf file, that is pretty straightforward. You keep all the assets, in separate swf or one large swf file and load them at runtime. Now, maintain your Avatar object instance and place the child objects, which are chosen by the user.


You can create inside your class a static List with references all the created instances and then apply a change with static methods. For example:

package
{
  import flash.display.MovieClip;
  import flash.geom.ColorTransform;

  public class Square extends MovieClip
  {

     public static var instances:Array = new Array();


     public function Square():void
     {
        Square.instances.push(this); // This is the trick. Every time a square is created, it's inserted in the static list.
     }

     // This property gets the color of the current object (that will be the same of all others because the setter defined below).
     public function get color():ColorTransform
     {
        return this.transform.colorTransform;
     }

     public function set color(arg:ColorTransform):void
     {
        // Sets the color transform of all Square instances created.
        for each(var sqr:Square in Square.instances)
        {
           sqr.transform.colorTransform = arg;
        }
     }

  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消