1
votes

I'm new to AS3 and my code may look a bit off.

Basically, how my program works is that I have an instance of "Level" on the stage, and it is a MovieClip containing several other objects which are also MovieClips with their own document class. In the "Level" class, I can access the X and Y position from the instance of "Player", but in my "Arrow" class, which is also a child of Level, I'm unable to access the X and Y of "Player". What I tried to do is set a public static variable called playerX and playerY in the Level class and assign that to the player's x and y position every frame, and I try accessing the variable in the Arrow class by doing "var x:Number = Object(parent).playerX, I've also tried MovieClip(parent).playerX and parent.playerX and just player X, neither of them work.

So to sum it up, I need to access a variable from a parent class, but every way I have tried it just returns an error.

Sorry if this was a bit unclear, any help will be much appreciated!

2
If your Arrow and Player instances are siblings, then you would do the following from your Arrow class (after it's been added to stage): var player:Player = MovieClip(parent).playerInstance; - where playerInstance is the instance name (or var name) of the player.BadFeelingAboutThis
did you get it figured out?BadFeelingAboutThis
Yes, I literally just got it working. It worked! Thank you for your help!Dud

2 Answers

1
votes

You need to understand what a static variable is and what it is not. A static variable IS a Class variable, A static variable IS NOT an instance variable. So if I create a static variable called "playerX" in a class called "Level" I can now do this:

Level.playerX = 0;

Because Level is a class and playerX is a variable of that class.

I cannot do this:

Object(parent).playerX

Because 'parent' is not a class and does not have a variable called 'playerX', parent might be an instance of the class 'Level' but instances of the class 'Level' do not have a variable called 'playerX', the class itself "Level" is the one that has that variable.

1
votes

Sounds like you are using FlashPro Timelines, and have two siblings objects (they have the same parent MovieClip/timeline) and you want to be able to reference one from the other (let me know if this doesn't properly summarize your question).

Since it sounds like you have class files attached to your Arrow and Player classes, you'll want to make sure any code referencing the parent or stage or root vars is run AFTER the item has been added to the display list as those vars are only populated after the item has been put on the stage. (this isn't an issue for timeline code).

So, something like this:

public class Arrow extends MovieClip {
    public var player:Player; //a var to hold a local reference to the player

    public function Arrow():void {
        //right now parent/stage/root are null
        //listen for the added to stage event, so you know when it's safe to use the parent and stage and root keywords
        this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
    }

    private function addedToStageHandler(e:Event):void {
        player = MovieClip(parent).playerInstanceName;
        //where player instance name is the instance name of your player on the parent timeline
    }

    private function doSomethingWithPlayer():void {
        if(player){
            player.x = 5;
        }
    }
}

Now, if you wanted to use a Static var instead (might be less cumbersome than the parent keyword), and there is only ever 1 player instance, you could do the following:

public class Player extends MovieClip {
    //create a static var to hold a reference to the player instance
    public static var me:Player;

    public function Player():void {
        me = this; //now assign the instance to the static var.
    }
}

Now, you can access the player instance from anywhere in your app by doing Player.me (regardless of context)

Player.me.x = 5;