PDA

View Full Version : possibly undefined method, nested getChildByName



bigarth
June 19th, 2007, 01:22 PM
allow me to set up the situation. i have created three movieclips that are in the library. the first movieclip is called Beat and has been linked to an actionscript class Beat. i have added a second movieclip inside the Beat movieclip that is called light (the instance name is light). The third movieclip is called BeatBar and is linked to class BeatBar. both the Beat and BeatBar classes extend MovieClip.

after adding a BeatBar to the stage via AS and adding children Beats to that BeatBar (addChild), i am attempting to access the light movieclip that is inside the Beat movieclip. I have placed this code inside a function in the BeatBar class:

getChildAt (current_beat).getChildByName ("light").visible = true;

current_beat is a number referencing a particular Beat inside the BeatBar. However, when i try to run the code, i get the error:

1061: Call to a possibly undefined method getChildByName through a reference with static type flash.display: DisplayObject. (i added a space before DisplayObject to disable the smiley)

i have tried importing flash.display.DisplayObject. i have tried assigning variables to each of the getChild calls instead of doing it on one line, such as:

var target: DisplayObject = getChildAt (current_beat); (added space to get rid of smiley)
target.getChildByName ("light").visible = true;

but that still throws the same error. this is my first time coding in AS3 and i'm at a loss as to why this isn't working the way i assume it should. any help would be greatly appreciated.

bigarth
June 19th, 2007, 04:44 PM
somebody has to have run into this problem before...please help me out. its left my project progress at a standstill.

bigarth
June 19th, 2007, 05:59 PM
i decided to try type casting and it made the errors go away

(getChildAt (current_beat) as Beat).getChildByName ("light").visible = true;

however now when i run the application i get an error stating that the object being referenced is null. i guarantee i have used the addChild method, and i even see the items on the stage. i'm horribly confused and really in awe at the difficulty this is presenting.

lambros
June 27th, 2007, 07:45 AM
hey i get the first error you mentioned too...how did you fix it if you did ?

Krilnon
June 27th, 2007, 08:45 AM
By casting the instance to a subclass of DisplayObjectContainer. (Like MovieClip)

lambros
June 27th, 2007, 09:23 AM
well i don't work with classes at all so i don't know what you are talking about mate!!!

Krilnon
June 27th, 2007, 09:51 AM
Well, if you are working with something that is returned to you as a DisplayObject (Like content from a Loader), but could really be more accurately described as something like a MovieClip (as in, it really is a MovieClip), then you can cast:
var displayObject:DisplayObject = aMovieClipReferenceTypedAsADO;
var movieClip:MovieClip = displayObject as MovieClip;

After casting, you can call MovieClip methods without the compiler complaining.

IanCremona
November 10th, 2008, 12:53 PM
I'm not sure if this might help you, but it works:

import flash.events.MouseEvent

remove_btn.addEventListener(MouseEvent.CLICK, removeBoarder);

function removeBoarder(event:MouseEvent):void
{
mc1.removeChild(mc1.mc2);
}