user780756
user780756

Reputation: 1484

Actionscript 3, access Symbol properties (AS Linkage)

I have created a new Symbol in the Flash IDE, I set it to Export for Actionscript and it has a class name of itemCoin

My stage has now 3 movieclips of that class, how can I:

Upvotes: 0

Views: 1185

Answers (1)

Jason Sturges
Jason Sturges

Reputation: 15955

Instance name is used for referencing instances of objects.

For example, if you had a symbol of type ItemCoin (note that naming convention for a type usually starts with a capital letter):

symbols

When you place instances on the stage, you give them an instance name to reference them by (note that naming convention for an instance is usually camel case):

instance-name

Now, properties may be accessed by referencing the instance name from code:

itemCoin1.x = 50;
itemCoin1.y = 25;

Remove it form stage:

removeChild(itemCoin1);

Add an event listener to the itemCoin1 instance for when it is removed:

import flash.events.Event;

itemCoin1.addEventListener(Event.REMOVED, removedHandler);

function removedHandler(event:Event):void {
    trace("itemCoin1 was removed");
}

removeChild(itemCoin1);

Although generally a poor practice, you can iterate all children to identify instances. For example, to count the number of ItemCoins:

import flash.display.DisplayObject;

var count:uint = 0;

for (var i:uint = 0; i < numChildren; i++) {
    var obj:DisplayObject = getChildAt(i);

    if (obj is ItemCoin) {
        trace("Found " + ++count + " item coins so far.");
    }
}

To comprehensively search the display list, you'd have to traverse children of all display objects.

If knowing the total number of instances on the stage was that important, it might be a better idea to define some ActionScript inside the component or within a Factory class to reference count when added to stage and removed from stage.

Upvotes: 2

Related Questions