AS3 Accessing Variables of Parent Class From Child

i'm trying to assign a parent's variable from the parent's child

//Parent public class Main extends Sprite { public var selectedSquare:Sprite; public function Main() { //inits and adds new Square child class to display list } ... ------- //Child public function dragSquare(evt:MouseEvent):void { Sprite(parent).selectedSquare = this; //evil doesn't work! parent.addChild(this); this.startDrag(); }

i'm receiving this error, but i'm casting parent from displayObjectContainer to a Sprite so i have no idea why it's not working.

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


You should cast parent as a Main rather than a Sprite, since a Sprite won't have any references to a "selectedSquare". If Main were to extend MovieClip, this wouldn't be a problem, since MovieClips can have dynamically created references.

Proposed modification to child function:

public function dragSquare(evt:MouseEvent):void
(parent as Main).selectedSquare = this;

Another reason that this may not work is that you're attempting to use the parent property right before adding the child to the parent's display list.

Sprite(parent).selectedSquare = this;

That second line is what worries me. In this code, the current object (this) must already be added as a child to the parent object (Main) for the first line to work properly. So, either the current object is not yet a child of the parent object, in which case you need to revise your code.

Or, the second line is unnecessary (because this is already a child of Main, that's why this.parent, or just parent, works as expected).

I believe, though, that your code is probably set up well. You just don't need that second line, as it's completely redundant.

I hope that helps! Let me know if you want me to clarify anything.

(This is, of course, assuming you didn't already know all of this and aren't doing some sort of insane, arcane, weird magic with the redundant addChild call. You never can tell with magicians!)

Category:actionscript 3 Time:2010-04-11 Views:1

Related post

  • as3 access VARIABLE in parent MovieClip or Loader? 2012-04-30

    Here is my code below. Its simple. I just need to get the var defined on the Main Stage and have every external swf get access to that var. Here is the code that doesn't work: IN MAIN SWF: var ThePath:String; ThePath = "happy/go/lucky/"; /// Below th

  • How to access variable of parent movie clip in Action script 3.0 2011-05-24

    How to access variable of parent movie clip in child movie clip in Action script 3.0 Thanks. --------------Solutions------------- Same way you would access a variable of any object, using parent as the target object. Because the parent property is of

  • issues accessing variable in parent from child - flash AS3 2011-10-30

    Im trying to access some variables from the parent in a child mc. Parent code: var date_1:String; var date_2:String; var date_3:String; date_1 = "test1"; date_2 = "test2"; date_3 = "test3"; Child code: date_1_txt.text = MovieClip(parent).date_1; date

  • Mustache: read variables from parent section in child section 2010-11-01

    Is it possible in Mustache to read variable from parent section while in child section? for instance my example below, I want the {{}} to read variable from it's parent $order_store[(array index of current child loop)]['id'] the templat

  • accessing variable from parent class 2012-01-01 import java.util.ArrayList; public class gameServer{ public static server server; public static gameRunner gameRunner; public static ArrayList<packet> packets = new ArrayList<packet>(); public static void main(String[] ar

  • How to access variable of parent process in C? 2011-04-25

    There is one var named "users" in one process and the process fork some child processes, I want know how could these child processes access the "users" variable ? BTW: The variable will be modified by parent process in runtime --------------Solutions

  • Accessing Variable in Parent Form from OnTimer Event - Getting Exception 2009-01-31

    I'm getting an exception in an OnTimer event handler (TTimer) that when executed increments an integer variable in the parent form. The timers need to be able to access an incremented integer used as an id. My first question is: How can I tell in Del

  • Flash AS3 - Dispatching Events from Parent Class to Child Class 2010-04-30

    I think this is a pretty simply problem but I do not seem to be able to pull it off. Basically I have a parent class A, and a child class B. Class A instantiates class B with addChild. There is a shared object which is being updated from a java serve

  • accessing variable in parent page from iframe 2010-10-07

    I have a page with an iframe that contains a html page. I want to access a javascript variable in the parent page from within the iframe. The name of the variable in the main page is "observer". I try and do the following in the iframe page: parent.o

  • Java: variable from parent class in child one isn't initialized and holds null 2011-01-19

    Parent class(*ch.qos.logback.core.FileAppender*): ... protected String fileName = null; public FileAppender() { } public void setFile(String file) { if (file == null) { fileName = file; } else { // Trim spaces from both ends. The users probably does

  • Accessing variables of parent function in Javascript event handlers 2011-06-27

    I have an event handler which is bound to a 'change' event. The problem is, functions within that function cannot access any elements of the parent function Process.prototype.handleCheckboxChange = function(event) { var rgbs = [] $(':checked').each(f

  • accessing control in parent window from child window in c# 2010-11-11

    I have a main "parent" window contains a button and a textbox. I have another window "child" window which fires when I enter some text in the textbox and click the button on the main window. now the child window contains another textbox and a button.

  • How to get variable from parent url or parent iframe? 2012-02-28

    How to get the variable from parent window url to child iframe or get the variable from parent iframe to child iframe? I have index.html file which have iframe who have the form the form send the data to checker.php than this files forward the user t

  • issues passing variables from parent to child. AS3 2011-10-14

    I'm trying to access some variables from my parent in my child MC. Parent code: var data_history:String; function finish_checkUp(event:Event):void{ var checkUp_stat:String; checkUp_stat = data.check_UP_STAT; if (checkUp_stat == "PASSED"){ data_histor

  • Accessing a static property of a child in a parent method 2010-03-10

    Say I have the following code: class Parent { static string MyField = "ParentField"; public virtual string DoSomething() { return MyField; } } class Child : Parent { static new string MyField = "ChildField"; } Now I want to be able to do both of the

  • Can I change the access modifiers of any parent function in Child class? 2011-02-07

    Can I change the access modifiers of any parent function in Child class? --------------Solutions------------- Child classes may only "loosen" the visibility, e.g. class ParentClass { protected function foo() {} } can be class ChildClass extends Paren

  • access overrided protected variable from parent class in PHP 2011-04-17

    Hi How can I access an overrided protected variable from parent class by calling its getter method in child class? for example: class A{ protected x='test'; protected function printx(){ echo $this->x; } } class B extends A{ protected x='test2'; pu

  • taking variable from parent to child and inserting it into dynamic text field 2011-08-04

    I'm trying to take a variable from the parent movieclip and use it in the child movieclip inside of a dynamic text field. The variable has a value that is taken from a php file. I'm completely lost at the moment and know that my code is probably comp

  • read child class variables in parent class 2012-04-09

    I am trying to access child class variables in parent class..can u suggest me how to proceed based on below code snippet? public abstract class Base{ //some abstract methods //one more method to parse the xml public final void parseXml(){ String clNa

Copyright (C), All Rights Reserved.

processed in 0.652 (s). 13 q(s)