JCR10
JCR10

Reputation: 79

access parent movieclip class variable from child/inside movieclip AS3

Maze Runner MovieClip has a "AS Linkage" class MazeRunner

MazeRunner class

public class MazeRunner extends MovieClip 
{

    public var _startCave       :String;

    public function MazeRunner():void
    {

    }
}

and movieclip (Maze Runner>mc_terrain) wants to access _startCave from "MazeRunner" class to be used in "mc_terrain" timeline. can it be done?

i tried using var mr:MazeRunner = new MazeRunner(); - but it's an error cause i think you can't access your own class/movieclip?

Upvotes: 0

Views: 533

Answers (1)

Andrei Nikolaenko
Andrei Nikolaenko

Reputation: 1074

if Maze_Runner is a DisplayObjectContainer and mc_terrain is a DisplayObject attached to Mazerunner via addChild:

var mr:MazeRunner = new MazeRunner(); // you have to have an instance of MazeRunner to run it, anyway
var mt:Terrain = new Terrain();
mr.addChilld(mt);

then you can use

(mc_terrain.parent as MazeRunner)._startCave

to access it.

If they are not attached this way, then you need to reference the MazeRunner inside a mc_terrain (you need a new property for that):

var mr:MazeRunner = newMazeRunner;
mr.mc_terrain.maze_runner = mr;
// ...access:
trace(mc_terrain.maze_runner._startCave);

Lastly, if you always have only one active instance of MazeRunner, you can change its _startCave property to static:

public static var _startCave       :String;

This will allow you to modify and read it from any place using static reference:

MazeRunner._startCave = "1";
trace(MazeRunner._startCave);

But it is generally not recommended as it may lead to issues if MazeRunner happens to have several instances which need different _startCave's.

Upvotes: 1

Related Questions