BumbleShrimp
BumbleShrimp

Reputation: 2200

Accessing instances of symbols via Actionscript 3 in Flash

Background Information:

The Problem

The Question:

Am I doing something wrong in detecting the names? How do I actually detect the instance names that I have created on the stage through the ActionScript in my class?

Here is my code:

        var pt:Point = new Point(e.stageX, e.stageY);
        var objects:Array = getObjectsUnderPoint(pt);
        var action = 0;

        for(var i=0; i< objects.length; i++) {
            trace(objects[i].name);
        }

        if( objects.indexOf('left_box') >= 0 ){
            action = 1;

        }
        if(objects.indexOf('right_box') >= 0 ){
            action = 2;

        }

Upvotes: 1

Views: 1703

Answers (3)

pho
pho

Reputation: 25489

Taking forward Fahim Akhter's answer,

You can then loop to get the parent until the object is a movieclip

var o:DisplayObject=objects[i];
while(!(o.parent is MovieClip)) {
    o=o.parent;
}
var myMovieClip:MovieClip=o.parent;

This should give you a movie clip in myMovieClip and when you trace myMovieClip.name, you'll get what you are looking for.

This will also work for multiple level symbols (where 1 symbol contains another)

Upvotes: 1

BumbleShrimp
BumbleShrimp

Reputation: 2200

Fahim Akhter pointed out in a comment that I was tracing shapes, not objects. This led me to a more accurate google search than I had been able to craft thus far.

The search led me to this forum thread: http://www.actionscript.org/forums/showthread.php3?t=231181

And in this particular post (number 6), the author pointed out that

"It turns out that getObjectsUnderPoint returns an array of the simplest object- the child-est, if that makes any sense. I was able to solve part of the problem by simply adding a .parent to the end of my variable..."

I appended .parent to the object, and received the appropriate name:

        var pt:Point = new Point(e.stageX, e.stageY);
        var objects:Array = stage.getObjectsUnderPoint(pt);
        var action = 0;

        for(var i=0; i< objects.length; i++) {
            trace(objects[i].parent.name);
        }

        if( objects.indexOf('left_box') >= 0 ){
            action = 1;

        }
        if(objects.indexOf('right_box') >= 0 ){
            action = 2;

        }

This solves my problem, and hopefully leads us all to a better understanding of getObjectsUnderPoint.

Upvotes: 1

Fahim Akhter
Fahim Akhter

Reputation: 1625

Reminds me of a quotation "I do not have the solution to your problem, but I certainly admire the problem"

If you try to trace(objects[i]) you'll see the function getObjectsUnderPoint is returning a shape not the MovieClip, so you never get the name you are looking for.

Upvotes: 1

Related Questions