-
can't target children
I have a simple class to create some buttons:
Code:
package {
import flash.display.*;
import flash.events.*;
public class ControlButton extends Sprite {
:
public var _gryBtn:GreyButton = new GreyButton();
public function createButton(what:String, iIndex:Number, m:Number):void {
// add name - its the url of the iamge without the swf eg introduction, 1999, mar2001
_gryBtn.name=ref;
in my main file I have a function that creates the buttons:
Code:
var gryBtn:ControlButton = new ControlButton();
gryBtn.addEventListener(MouseEvent.CLICK, sceneBtnClicked);
WHen I press the button I can get the name out easily by going
Code:
function sceneBtnClicked(e:Event):void{
trace( e.target.ref)
}
all works fine
Later on the code I want to loop through all these buttons but I can't seem to get name to work
Code:
for (j:Number:0; j<this.container.numChildren, j++){
trace (this.container.getChildAt(j).ref);
}
brings up "access of possibly undefined property"
I do have other mcs etc within that container which don't have a ref
but also trace (this.container.getChildAt(5).ref) doesn't work and I know number 5 is one of the ones that does have a ref
How do I use the ref to target the buttons
thanks
E
-
getChildAt returns a DisplayObject. Since DisplayObject is a sealed class which does not define a ref property, to access that property you need to cast it to something that does declare it or is dynamic.
Code:
MovieClip(this.container.getChildAt(5)).ref
It's a very good idea to put stuff that you will want to treat as a collection in an actual collection. That is, put the things with refs in their own array so that you don't have to iterate over everything and test whether it's what you want.
-
Thanks for the reply. Sorry this is a longish post - still not able to reference the buttons.
ANy more help greatfully received!
Thanks in advance
Edward
I have tried a few things, but its still not working.
Heres the class (I have left it complete)
Code:
package {
import flash.display.*;
import flash.events.*;
public class ControlButton extends MovieClip {
public var _gryBtn:GreyButton = new GreyButton();
private var btnText:Array = ["Introduction", "1999", "Mar 2001", "Mar 2002", "Apr 2004", "Nov 2007", "Dec 2007", "Feb 2008", "Mar 2009", "Dec 2009", "Credits"];
public function createButton(what:String, iIndex:Number, m:Number):void {
// add name - its the url of the iamge without the swf eg introduction, 1999, mar2001
_gryBtn.name=what;
_gryBtn.ref=iIndex-m;
_gryBtn.btnTxt=btnText[iIndex-m];
// x coordinate
_gryBtn.xCo = 50+(79*(iIndex-m));
// add text to textfield
_gryBtn.btnLabel.text=_gryBtn.btnTxt;
_gryBtn.buttonMode=true;
_gryBtn.mouseChildren = false;
_gryBtn.addEventListener(MouseEvent.ROLL_OVER, btnOver);
_gryBtn.addEventListener(MouseEvent.ROLL_OUT, btnOut);
this.addChild(_gryBtn);
}
private function btnOver(e:MouseEvent):void{
_gryBtn.gotoAndStop("mOver");
}
private function btnOut(e:MouseEvent):void{
_gryBtn.gotoAndStop("mOut");
}
private function traceOut():void{
trace (_gryBtn.ref);
}
}
}
On my maintime line is:
Code:
function addButton():void{
var gryBtn:ControlButton = new ControlButton();
gryBtn.createButton(randArray[imageIndex], imageIndex, n);
gryBtn.x=50+(79*(imageIndex-n)); gryBtn.y=7;
btnHold.menuBar.addChild(gryBtn);
// put button in array for access
btnArray.push(gryBtn);
}
Later, the user presses a button and I go to trace function where I have out all sorts of things! (none of which work)
Code:
trace((btnHold.menuBar.getChildAt(5) as MovieClip).ref) ;
which gives me "ReferenceError: Error #1069: Property ref not found on ControlButton and there is no default value."
and...
Code:
MovieClip(this.container.getChildAt(5)).ref
which gives "TypeError: Error #1010: A term is undefined and has no properties."
and...
Code:
trace(btnArray[5].ref) ;
which gives "ReferenceError: Error #1069: Property ref not found on ControlButton and there is no default value."
-
The things in menuBar are ControlButtons, but the ref property is on the GreyButtons inside the ControlButtons. Container appears to be undefined in this code.
So the first and third ways can be made to work with a tweak.
Code:
trace((btnHold.menuBar.getChildAt(5) as ControlButton)._gryButton.ref);
Code:
trace(btnArray[5]._gryButton.ref);
Or you could add a ref getter function to your ControlButton class
Code:
public function get ref():Number{
return _gryButton.ref;
}
-
thanks for your help.
At last its working.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|