i'm pretty confused over the following issue and would be grateful for some clarity.
generally, how i work involves designing all of my graphics in Flash Authoring, converting them to Sprite symbols by changing the base class to flash.display.Sprite, give my instances names and finally export them to ActionScript.
the approach actually permits me to dynamically create properties in code on my Sprite instances that i've exported to ActionScript, just as if they were instances of MovieClips. i'm not entirely sure why i'm able to do this, but i can. in polling the objects to make sure of their superclass, they are indeed Sprites and not MovieClips.
however, as expected, if i program a new sprite from scratch in code and try to dynamically add a property to the new programmed sprite a compile time error will result.
package
{
import flash.display.Sprite;
import flash.utils.getQualifiedSuperclassName;
public class Document extends Sprite
{
public function Document()
{
trace(getQualifiedSuperclassName(blueOvalInstance));
//flash.display::Sprite (it's not a MovieClip)
trace(blueOvalInstance.hasOwnProperty("currentFrame"));
//false (ok, ok, it's definately not a MovieClip)
blueOvalInstance.myNewProperty = true;
//dynamically added boolean property on a Sprite instance
trace(blueOvalInstance.hasOwnProperty("myNewProperty"));
//true. fancy that! my Flash Authoring exported Sprite has a dynamically added property
codeSprite();
}
private function codeSprite():void
{
var myCodedSprite:Sprite = new Sprite();
myCodedSprite.graphics.beginFill(0xFF0000);
myCodedSprite.graphics.drawRect(0, 0, 100, 100);
myCodedSprite.graphics.endFill();
addChild(myCodedSprite);
myCodedSprite.anotherNewProperty = true;
//dynamically added boolean property on a Sprite instance, just like before!
//Compile Time Error!!!
//1119: Access of possibly undefined property anotherNewProperty through a reference with static type flash.display:Sprite.
}
}
}
so why is it that can i dynamically add properties to exported sprites in my document class if they are not MovieClips, while i can not if i create them myself in code?
the following image shows a new BlueOval symbol being exported to ActionScript from Flash Authoring with the base class of Sprite (not MovieClip). notice the new green (instead of blue) colored "Movie Clip" 开发者_C百科icon in the library panel.
You class needs to be defined as a dynamic class in order to have properties added during runtime.
Check out this page: http://flexmusings.wordpress.com/2008/07/23/actionscript-3-dynamic-classes-part-2/
var myCodedSprite:Sprite = new Sprite(); myCodedSprite is an object of sealed class so it can not add property at runtime.. however when you extend this class , you can add custom properties in child class.
精彩评论