one eventListener for different targets - actionscript-3

I want to have only one event listener that work with 3 different buttons: btn1, btn2, btn3.
I know that "btn+i" doesn't exist and doesn't work. Theres any way to do this? Sorry, I'm a beginner...
for(var i:uint=1;i<4;i++){
btn+i.addEventListener(MouseEvent.CLICK, btnClicked);
}

You should be able to do this["btn" +i].
But you'd probably be better off stuffing them all into an array and accessing them that way. Then your code isn't so dependent on everything being named a certain way.
Something like this:
var buttons:Array = [btn1, btn2, btn3];
for(var i:int = 0; i < buttons.length; i++){
buttons[i].addEventListener(MouseEvent.CLICK, btnClicked);
}

Related

Equal height layout of 3 module content, best method?

In looking at this image you can see in an ideal world each box would have the same height of content in each box. However in the real world we can't control how many characters the client uses for a heading. Wondering thoughts on how to deal with a situation like this? Is it ok to just let it be as is?
This will create an array of heights of an element by class, then find the tallest, and then make them all that height.
<script>
var headerHeights = [];
var mclength = document.getElementsByClassName("myClass").length;
for (i = 0; i < mclength; i++) {
headerHeights[i] = document.getElementsByClassName("myClass")[i].getBoundingClientRect().height;
}
var headerMaxHeight = Math.max(...headerHeights);
for (i = 0; i < mclength; i++) {
document.getElementsByClassName("myClass")[i].style.height = headerMaxHeight+"px";
}
</script>
You will likely want to make this a function which replaces "myClass" with a function parameter so that you can call it for each class you add. You will also want to add a listener for when a person resizes their window to rerun the function.

SimpleButton(getChildByName(button_name)) returns null

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?

referring to textfields in for loop

I've looked up how I can do this and nothing seems to work.
Trying to use the i in the loop to refer to all my textfields and set them.
Suppose my textfields are named textField0, textField1, textfield2, etc.
Something like:
for(var i:int = 0; i < numberOfFields; i++)
{
parent.child.getChildByName("textField" + i).text = stringArray[i];
}
Any help would be much appreciated
Thanks
Let's assume your hierarchy looks like this
0: root:MainTimeline ¬
0: Background:Shape
1: textField1:TextField
2: textField2:TextField
3: myClip:MovieClip
4: textField3:TextField
We've got some "noise" in the list, so a straight-up iteration over all those might not be the best way. We could, and place an if statement that tests the name of the object, or its object type, or we could create a manual list of pointers to each of these TextFields. In that scenario, each textfield could hypothetically exist in any nested container. It depends on what you have set up.
In the example you gave above, you're referencing an object literally called "child", which would mean your hierarchy might look like this...
0: root:MainTimeline ¬
0: child:MovieClip ¬
0: textField1:TextField
1: textField2:TextField
2: textField3:TextField
1: myClip:MovieClip // <- assuming that this is the class your code is coming from
So, the logic you have is that we go up a level with an implicit this reference (this.parent == parent), and then down to our container named child, and iterate over its children (note: I suspect this is not the case, and part of the reason its failing).
Assuming the same hierarchy, I might rewrite it this way:
for (var i:int = 0; i < parent.child.numChildren; i++) {
var txt:TextField = parent.child.getChildAt(i);
txt.text = stringArray[i];
}

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');
}

Problems with accessing DOM tree

I'm developing a simple Chrome extension, which should be able to retrieve a DOM nodes and modify them in the specific way. And this is what I'm stuck with.
There are two DOM queries (lets call the A and B) in a javascript file, that i inject in html-page. The first one (A) goes well, but the second one (B) always crushes. Even If I interchange the first (A) query and second one (B), everything will still work in the same way. Query (B) now works as it should in the first place, and A now does nothing.
spans = document.getElementsByTagName('span');
for(j =0 ;j<=divin.length; j++)
{
//Manipulations //Works just fine
}
paragraphs = document.getElementsByTagName('p');
for(j =0 ;j<=paragraphs.length; j++)
{
//Manipulations //Does nothing
}
And what we see here. The same code, but different position.
paragraphs = document.getElementsByTagName('p');
for(j =0 ;j<=paragraphs.length; j++)
{
//Manipulations //Works just fine
}
spans = document.getElementsByTagName('span');
for(j =0 ;j<=divin.length; j++)
{
//Manipulations //Does nothing
}
I tried every way of injecting this code, but result was always the same.
Seems like you're using divin.length instead of spans.length.