1
votes

I might be missing something. I thought this would be a common question but extensive search on the web over a few days didn't turn up the answer I need. I am hoping to make a self-contained movie clip that would function wherever it is placed. However, it seems difficult to access instances defined in the movie clip from within a class that extends the movie clip.

Although I am coding in Flash Actionscript 2 (AS2), I wonder if the same issue exists for AS3.

In the following scenario, how do you access the instance "circle"?

A movie clip on the main timeline has an instance name "square". A class Square defined in Square.as extends this movie clip. Inside the movie clip, another movie clip is placed on the first frame, with instance name "circle". There is also a varialbe declared on the first frame var myName:String = "my name". The following code doesn't compile:

class Square extends MovieClip {
      function Square() {
          trace("Square.constructor, circle: " + circle);
          trace(" --- myName: " + myName);
      }
}

Compiler errors: There is no property with name 'circle'. (same for myName).

Adding this. in the reference doesn't help. Apparently, the compiler is looking for the varialbles in the class definition.

Using _level0.square.circle works, but that requires the class to know its own instance path. While _level0.square.myName doesn't produce compiler errors, it gives a value of undefined.

Referencing these variables in methods other than the constructor causes the same compiler errors.

Thanks much for your help.

1
Hey user1789547, maybe if the instance resides on the stage you have to pass the stage reference into the class? I know your question is about AS2, but this might be some help : actionscript.org/forums/showthread.php3?t=250599Mike
Thanks, Mike. I saw that or similar discussions during my search. But they were trying to reference something outside of the class, while I was trying to reference something inside, a child movie clip within the parent movie clip that the class is extending. Logically, as least I thought, the extending class would have access to everything that the parent movie clip had access to. -KeithBraeburn

1 Answers

0
votes

I found a way to solve the problem, using eval(this).

class Square extends MovieClip {
    var base:MovieClip;

    function Square() {
        base = eval(this);
        trace("Square.constructor, circle: " + base.circle);
        trace(" --- myName: " + base.myName);
        onPress = procPress;
    }

    function procPress() {
        trace("procPress: " + base.circle);
        trace("procPress: " + base.myName);
    }
}

No compiler errors. In the constructor, base.myName is still undefined (understandable). In the function procPress invoked by a click, base.myName has the correct value.