SimpleButton(getChildByName(button_name)) returns null - actionscript-3

I just started learning AS3 and I'm stuck.
I'm trying to get the instance names of some buttons from an XML file and then make those buttons disapear, but there is a problem. When I try to locate the buttons, the getChildByName
method I use returns null.
Here is my code:
for (var i:int = 1; i < countries.item.length(); i++) {
var button_name:String = countries.item[i].Name.toString();
trace(button_name);
var button1:SimpleButton = SimpleButton(getChildByName(button_name));
trace(button1);
button1.visible=false;
}
As you see, I check if I get the instance names and trace(button_name) works fine.
Now, trace(button1) on the other hand returns null and I don't understand why.
Any suggestions please?

Related

CreateJs MovieClip Instance name

Hi Guys (sorry for my bad english), I've stuck with this in a week, is it possible to get the movieclip instance name with createJS (I've put the movieclip manually on the stage and I give it an instance name), in AS3 I can use movieclip.name to get the instance name, how can I do that in createJS, when I use this.movieclipName.name it always return null in console log, do You have any suggestion? Thanks for Your help?
#Ferry thank You very much for Your solution. finally I figured it out, We can't get the instance name of the object by myObject.name because the name of the object is not signed previously (it looks like the instance name not same as name in html5). that's why it always giving a null. This is my script to get all the instances name of my objects.
for (var a = 0; a <= maxObject - 1; a++) {
var myObjectName = 'object' + (a + 1);
var myObject = this[arrObject[a]];
myObject.name = myObjectName;
}
Thank you guys for all of your respon.
i solve this problem using:
for (i = 1; i < 6; i++) {
this["m" + i].addEventListener("click", clickme.bind(this));
this["m" + i].hamdy=i;}
so you can use var hamdy to pass any change in clickme funaction.

How to push instantiated MC into Array dynamically?

Im really stuck. I have 5 MC´s that are being spliced from one array at a certain time. In that same function I want to push another movieclips into another array. The two arrays holds mc's that represent right or wrong answers. So when one question is being given a correct answer that questions visualisation alters.
This function holds a incrementing variable as I do want the mc's to be pushed by the user and one at the time. The thing is I cant seem to refer them properly.
I´ve tried
pQuestSum = this[pQuest + pQuestNumber];
and
pQuestSum = this[pQuest] + pQuestNumber;
and pretty much everything I´ve imagined would work...but the problem is I havent tried
the right thing.
when I trace pQuestSum (which would be the reference) I get an error saying thats its not a number.
this is one of 5 mc's named from 1-5:
var passedquest1:PassedQuest = new PassedQuest();
this is the vars that i try to to build a reference of
var pQuest = "passedquest";
var pQuestNumber = 1;
var pQuestSum;
var questCorrArray:Array = [];
if(event.target.hitTestObject(questArray[ix])){
removeChild(questArray[ix]);
questArray.splice(ix,1);
pQuestNumber ++;
pQuestSum = this[pQuest] + pQuestNumber;
trace("pQuestSum"); // NaN
questCorrArray.push(pQuestSum);
//trace(questArray.length);
pointsIncreased = false;
questPoints = 0;
}
How do I refer an existing movieclip when the reference consists of both a string and a number? Hope I made myself somewhat clear:)
If you had an instance of an object on your timeline called "passedquest1" (as an example), then you could access it this way:
var myObj = this["passedquest" + 1];
Or,
var pQuest = "passedquest";
var pQuestNumber = 1;
var myObj = this[pQuest+ pQuestNumber.toString()];
When you do this: pQuestSum = this[pQuest] + pQuestNumber;, you are trying add the number to an object (this[pQuest]), unless you have number/int var called "passedquest", this will result in NaN.

Trying to remove all children, receiving error #1009

The idea with my setup is that I have an input text field and three separate buttons on stage. When you type something in the text field and press the input button, the text inside the field is added to an array.
When you press the display button the contents of the array are displayed on screen (each value of the array is displayed underneath the last value).
The final button is supposed to remove all the current values on the array, and clear all displayed values on screen. But I cannot get my code to work as intended, since I receive,
TypeError: Error #1009: Cannot access a property or method of a null object reference.
With this block of code:
import flash.text.TextField;
import flash.events.MouseEvent;
var myArray:Array = new Array ("");
var tf:TF;
btnInput.addEventListener(MouseEvent.CLICK, txtInput);
function txtInput(event:MouseEvent):void
{myArray.push(txtInput.text);}
btnDisplay.addEventListener(MouseEvent.CLICK, txtDisplay);
function txtDisplay(event:MouseEvent):void
{for (var i:int = 0; i < myArray.length; i++)
{var tf:TF = new TF();
tf.txt.text = myArray[i];
tf.y = 280 + (i * 25);
tf.x = 265;
addChild(tf);
tf.name="test";}
}
btnClear.addEventListener (MouseEvent.CLICK, txtClear);
function txtClear(event:Event){
myArray.splice(1);
if (tf.numChildren != 0){
removeChild(getChildByName("test"));}
}
Alternatively, when I add
var tf:TF = new TF;
It removes only one displayed value on screen. Also I might add, that "TF" is a movie clip in the library that contains a dynamic text field that's instance name is txt. Is the problem only with the last button, or should I change something else as well? I don't know how to make this work as I want it to. I'm pretty new to coding so any tips or help would be much appreciated. Thanks in advance!
In your condition if(tf.numChildren != 0) the tf object is null since you never created it. That's why you're getting the #1009 error.
On the other hand, in order to delete all items you should replace the if (tf.numChildren != 0) with while (tf.numChildren != 0).

Converting String values to MovieClip in AS 3.0

I am trying to convert an array of string to MovieClip names which already exist on stage.
THis is how I am doing it but doe snot seem to work.
I have 11 movieclips on stage. THeir instance names are "bmc1", "bmc2" and so on.
All these BMC movie clips are inside a clip called "bars_mc". So this is how I am coding it right now.
var myBtnArray = ['bmc1', 'bmc2', 'bmc3', 'bmc4', 'bmc5', 'bmc6', 'bmc7', 'bmc8', 'bmc9', 'bmc10', 'bmc11'];
for each (var btn in myBtnArray){
bars_mc.MovieClip(getChildByName(btn)).gotoAndPlay('open');
}
This does not work.
I have tried doing:
this[btn]
That also didnt work.
This is being coded in AS 3.0.
Need someone t0 help me figure out the right way to convert strings to MOvieclips.
Apprecuiate your help.
I think the problem is likely that you've added the MovieClip cast in the wrong place, as though there were a property MovieClip on the object bars_mc.
This example involves an extra line but should be a bit clearer:
var myBtnArray = ['bmc1', 'bmc2', 'bmc3', 'bmc4', 'bmc5', 'bmc6', 'bmc7', 'bmc8', 'bmc9', 'bmc10', 'bmc11'];
for each (var btn:String in myBtnArray) {
var btnClip:MovieClip = bars_mc[btn] as MovieClip;
btnClip.gotoAndPlay('open');
}
And a version closer to your original would be:
var myBtnArray = ['bmc1', 'bmc2', 'bmc3', 'bmc4', 'bmc5', 'bmc6', 'bmc7', 'bmc8', 'bmc9', 'bmc10', 'bmc11'];
for each (var btn:String in myBtnArray) {
MovieClip(bars_mc[btn]).gotoAndPlay('open');
}
You should just store the MovieClips in the Array like so :
var myBtnArray = [bars_mc.bmc1, bars_mc.bmc2, bars_mc.bmc3, bars_mc.bmc4, bars_mc.bmc5, bars_mc.bmc6, bars_mc.bmc7, bars_mc.bmc8, bars_mc.bmc9, bars_mc.bmc10, bars_mc.bmc11];
for each (var btn in myBtnArray){
btn.gotoAndPlay('open');
}
Even better, if the buttons you have in that array are the ONLY children of bars_mc, why not just use the display lis of bars_mc instead of an Array like this :
for (var index:int = 0;index < bars_mc.numChildren;index++)
{
var btn:MovieClip = bars_mc.getChildAt(index) as MovieClip;
btn.gotoAndPlay('open');
}
However, if you want to go the route you currently have chosen, you can just do this :
var myBtnArray = ['bmc1', 'bmc2', 'bmc3', 'bmc4', 'bmc5', 'bmc6', 'bmc7', 'bmc8', 'bmc9', 'bmc10', 'bmc11'];
for each (var btn:String in myBtnArray) {
MovieClip(bars_mc[btn]).gotoAndPlay('open');
}

Data from TLF TextLine

i have some problem with utilizing TLF, i need to parse through the text and get x and y for each character inside the textfield. This is what i have so far...
Getting every TextLine from the TextFlow:
if (textflow.flowComposer) {
for (var i:int = 0; i < textflow.flowComposer.numLines; i++) {
var flowLine:TextFlowLine = textflow.flowComposer.findLineAtPosition(i);
var textLine:TextLine = flowLine.getTextLine(true);
}
}
Getting every "atom" for the TextLine:
var charPosition:int = textLine.textBlockBeginIndex;
while (charPosition < textLine.textBlockBeginIndex + textLine.rawTextLength) {
var atomIndex:int = textLine.getAtomIndexAtCharIndex(charPosition);
textLine.getAtomBounds(atomIndex);
charPosition = textLine.getAtomTextBlockEndIndex(atomIndex);
}
This works for getting the bounding for each character but i still need some more data like what character is it and what font-size, font does it have? When doing a textLine.dump(); i think im getting this data but not the character, i get something called gid witch seems to point to the character in use but i don't know how to get exactly what character that is. Anny ideas?
Solved my problem with the help of Jin-Huang over at the adobe forum for TLF. I haven't tried it to full extent yet, but seems to work for now.