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.
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);
}
}
}
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.
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.
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.
[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.