Referencing

Hi There,

I have odd problem here :frowning:

I´m not able to reference property [COLOR=YellowGreen]“prop”[/COLOR] in class Level.

FLA file:


var mainLogic:Level = new Level();

Level.as:


package {

    import flash.display.Sprite;

    public class Level extends Sprite {
        public var prop:int = 5;

        public function Level():void {
            var h:Tetris_Shape = new Tetris_Shape();
        }
    }
}

Tetris_Shape.as:


package {

    import flash.display.MovieClip;

    public class Tetris_Shape extends MovieClip {


        public function Tetris_Shape():void {
            trace(parent.prop);

        }
    }
}

I don´t know why, but it throws error 1119…

1119: Access of possibly undefined property prop through a reference with static type flash.display:DisplayObjectContainer.

Thank you for your responses. :angel:

trace(parent.prop);

swap with

trace(MovieClip(parent).prop);

and you need to addChild your Tetris_Shape

You should use Sprite(parent).prop, BUT you ultimately can’t use parent in a classes constructor because there is no possible way that it can be a child of another DOC until after it is instantiated (which includes executing the constructor).

You should run the code that requires access to the parent a function which gets called after the instance is added to the display list. It can be called by using the Event.ADDED event. Something like this:

package {
	import flash.display.Sprite;
	import flash.events.Event;
	public class Test extends Sprite {
		public function Test() {
			addEventListener(Event.ADDED, init);
		}
		private function init(evt:Event):void {
			trace(parent);
		}
	}
}

I made this change :


package {

    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;

    public class Tetris_Shape extends MovieClip {


        public function Tetris_Shape():void {

            addEventListener(Event.ADDED, init);
        }
        private function init(evt:Event):void {
            trace(Sprite(parent).prop);
        }


    }
}

and i added Tetris_Shape to Display List


package {

    import flash.display.Sprite;

    public class Level extends Sprite {
        public var prop:int = 5;

        public function Level():void {
            var h:Tetris_Shape = new Tetris_Shape();
            this.addChild(h);
        }
    }
}

but i´m still getting error :
1119: Access of possibly undefined property prop through a reference with static type flash.display:Sprite.

:expressionless:

I don’t know why Canadian said you should use Sprite() because your Tetris_Shape extends a movieclip…

replace
trace(Sprite(parent).prop);
with
trace(MovieClip(parent).prop);

I replaced [COLOR=Red]trace(Sprite(parent).prop);[/COLOR] with [COLOR=Red]trace(MovieClip(parent).prop);[/COLOR]

But, it threw an error :
TypeError: Error #1034: Type Coercion failed: cannot convert Level@31cfa61 to flash.display.MovieClip.

actually… my bad. Both me and Canadian were wrong. I misread it a little bit since the parent you are referencing is a sprite and the child is the movieclip…

What you SHOULD do, is cast it to your class, and not the generic sprite class. Sprite doesn’t have a property called prop, but your document class does.

try this;

trace(Level(parent).prop);

Thank you very much guys :rambo:
It is working :pope:

May I have a question ? :slight_smile:

In which case I could use Sprite(parent.prop) or MovieClip(parent.prop).

Thank you again :slight_smile:

Sprite(parent).prop would never really work since no Sprites have the propery ‘prop’. MovieClips are dynamic class though so that would work if your parent was a generic movieclip class.

Ahh… thank you I get it now :slight_smile:

[QUOTE=sekasi;2353627]What you SHOULD do, is cast it to your class, and not the generic sprite class. Sprite doesn’t have a property called prop[/QUOTE]
Good call.