views:

93

answers:

2

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" icon in the library panel.

alt text

A: 

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/

Tegeril
are you saying the approach i've listed above is defining these base class instances as dynamic automatically?
TheDarkInI1978
Yes, since you're not instantiating them by calling new Sprite() judging by how the superclass is listed as Sprite, Flash id dynamically creating an object, which I believe is also dynamic :)
Tegeril
humm... i just realized the above approach doesn't work (can't add properties to sprites) if they are nested in other sprites. it seems that it will only work for sprites at index 0 on the display list.
TheDarkInI1978
A: 

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.

Muhammad Irfan