Setting Styles for an Array of Labels - Flash Builder 4.6 - arrays

Having a bit of trouble setting label colors in a for loop
This works as expected:
label1.styleName = "myStyle";
However this does not:
for (var j:int = 0; j < labels.length; j++) {
labels[j].styleName = "myStyle";
}
When I trace the style I get the style name, but nothing changes visually, as it does in the first example.
I've tried other things such as:
(labels[j] as Label).setStyle('color', 0xFFFFFF); // Null object reference
And all the variants I could think of on that....setStyle(), as LabelItemRenderer...
Any thoughts?

Ah, for anyone else who happens to have this unusual problem, I had added the elements to the array wrong....
I had added them all in a loop without referencing with the 'this' keyword.... Nothing wrong with the code above, just the array. Derp!

Related

TypeScript object shows all values as defined but then values come back as undefined

I am trying to dynamically define an object in typeScript with a for loop.
Here is the code for that
var n = 7
interface LooseObject {
[key: string]: any
}
var boardValues: LooseObject = {}
for (var i = 0; i <= n; i++){
for(var j = 0; j <= n; j++){
var boardIndex = 8*(n-i)+j
boardValues[`n${boardIndex}`] = `n${boardIndex}`
console.log("generating boardValues",boardValues[`n${boardIndex}`]) //outputs the expected result e.g. n15
}
}
Here is a gif showing the output
While running the loop the console.log gives the expected output (e.g. n7). However when I then output the boardValues object created to the console only n16 to n47 are defined. Even weirder is that chrome shows the values as defined properly until I expand the object to see all values and then shows them as undefined.
What am I doing wrong? (I am new to typeScript and not great at programming so sorry for poor practices)
I think your code works fine, it's just that the boardValues properties are not sorted (e.g n50 might be the first key shown, and n7 could be in the middle);
To see all the keys set, try:
console.log(Object.keys(boardValues).sort((a,b)=>Number(a.slice(1))-Number(b.slice(1))))

Error while using removeChild() and accessing members of array

I am stuck doing this even though I know it's very simple. Yet, I am getting errors.
What I have:
I have 3 arrays.
1st Array contains objects of UpgradeButton class.
2nd Array contains objects of BuyButtonclass.
3rd Array named newCostlyShops contains Numbers.
BuyButton class and UpgradeButton class, both have a shopCode member which is a number; the number which I'm trying to equate.
What I'm trying to do:
My goal is to first look for BuyButton and UpgradeButton objects in the respective arrays which have shopCodes same as those in newCostlyShops.
After that, I removeChild() that object and splice it out from the array.
My Code:
Array 3:
var newCostlyShops:Array = new Array();
newCostlyShops = Object(root).WorkScreen_mc.returnCostlyShops();
trace(newCostlyShops); // this is tracing out the exact shopCodes I want and is working fine.
Deletion and Splicing codes:
for (looper = 0; looper < upgradeButtonsArray.length; looper++) {
for (var secondLooper: int = 0; secondLooper < newCostlyShops.length; secondLooper++) {
if (upgradeButtonsArray[looper].shopCode == newCostlyShops[secondLooper]) {
trace(looper);
trace(upgradeButtonsArray[looper]);
removeChild(upgradeButtonsArray[looper]);
upgradeButtonsArray.splice(looper, 1);
}
}
}
for (looper = 0; looper < buyButtonsArray.length; looper++) {
for (secondLooper = 0; secondLooper < newCostlyShops.length; secondLooper++) {
if (buyButtonsArray[looper].shopCode == newCostlyShops[secondLooper]) {
trace(looper);
trace(buyButtonsArray[looper]);
removeChild(buyButtonsArray[looper]);
buyButtonsArray.splice(looper, 1);
}
}
}
What's wrong with this Code:
I keep getting error
TypeError: Error #1010: A term is undefined and has no properties.
This error comes only after the 1st time this code is run and not the first time it is run. When I remove the removeChild and splice , this traces out objects that are not null, ever. Even after this whole function is called 100 times, the error is not shown. Only when I removeChild and use splice this occurs.
Is there something wrong with what I'm doing? How to avoid this error? This is throwing the whole program haywire. If there is any other alternative to this method, I'm open to take those methods as well as long as I don't get errors and my goal is reached.
It might sounds funny, but.... try to decrement looper after splicing.
trace(looper);
trace(upgradeButtonsArray[looper]);
removeChild(upgradeButtonsArray[looper]);
upgradeButtonsArray.splice(looper, 1);
looper--;
I think after splicing the array all item's are being shifted and you're skipping next one.
Also, you should get some more information with this error, like which class/line is throwing it. Maybe you need to enable "permit debugging" or something?
Bonus suggestion:
For newCostlyShops use Dictionary instead of Array so you won't have to nest for inside for...

AS3 adding EventListener to array of movieClips but getting error #2007 :Parameter listener must be non-null

I am trying to add an event listener to ALL of my buttons in the buttons array. I can make them buttons within the loop but when I try and add the event listener is give me this error:
TypeError: Error #2007: Parameter listener must be non-null.
at flash.events::EventDispatcher/addEventListener()
at Main()
I can add this event to another array but just not this one. Ive placed these buttons on the stage and gave them instance names which I'm referring to in my as file. I am learning AS3 in school so this is probably a very obvious problem but Im not qualified to debug my code yet :S Thanks for all your help.
//array of buttons and making them buttons
var buttons:Array = [armButton, lobeButton, beakButton, crotchButton, earButton, hairButton, legButton, shoulderButton, spineButton, tailButton, tearButton, eyeButton];
for(var b:int = 0; b<buttons.length; b++){
buttons[b].buttonMode = true;
buttons[b].addEventListener(MouseEvent.CLICK, clickMe);
}
function clickMe(e:MouseEvent):void{
trace("hello");
}
Check the name of the "clickMe" function both in definition and parameters-section, make sure that exactly the same characters are used (a character can sometimes be mistaken for the character from another code table). The error occurs because "clickMe" expression is null at the moment when the loop is being executed.
var buttons:Array = [armButton, lobeButton, beakButton, crotchButton, earButton, hairButton, legButton, shoulderButton, spineButton, tailButton, tearButton, eyeButton];
for(var b:int = 0; b<buttons.length; b++){
buttons[b].buttonMode = true;
// what is the output of the following expression?
trace(clickMe) // should be "function Function() {}"
buttons[b].addEventListener(MouseEvent.CLICK, clickMe);
}
function clickMe(e:MouseEvent):void{
trace("hello");
}
In your addEventListener line clickMe is null.
I suspect that we aren't seeing all the code here. Is that code together in the same file ? Or did you cut/paste just parts you thought were important ?

Actionscript 3.0 Get all instances of a class?

I got a ton of movieclips in a class. Is there a more efficient way to apply a function to every instance in the class other than this?
var textArray:Array = [
interludes.interludeIntro.interludeBegin1,
interludes.interludeIntro.interludeBegin2,
interludes.interludeIntro.interludeBegin3,
interludes.interludeIntro.interludeBegin4,
interludes.interludeIntro.interludeBegin5,
interludes.interludeIntro.interludeBegin6,
interludes.interludeIntro.interludeBegin7,
//... ... ...
interludes.interludeIntro.interludeBegin15
];
for each (var interludeText:MovieClip in interludeBeginText)
{
interludeText.alpha = 0 //clear all text first
}
Also for some reason this doesn't work:
interludes.interludeIntro.alpha = 0;
It permanently turns that class invisible, even if I try to make specific instances visible later with:
interludes.interludeIntro.interludeBegin1.alpha = 1;
I have NO idea why the above doesn't work. I want to turn every single instance in the class interludeIntro invisible, but I want to turn specific instances visible later.
(btw I have no idea how to insert code on this website, pressing "code" doesn't do anything, so pardon the bad formatting)
I'm not really sure what you're asking, but what may be useful is that, in ActionScript you can refer to properties by name, like myObject["someProperty"].
Using that, you can iterate over properties if they follow some naming scheme, so for example:
for (var i:int = 1; i <= 15; i ++)
interludes.interludeIntro["interludeBegin" + i].alpha = 0;
That iterates over interludes.interludeIntro.interludeBegin1 through ...15 and sets their alpha properties to 0.
When you do that:
interludes.interludeIntro.alpha = 0;
you turn the movie clip and all its children invisible.
So later when you do that:
interludes.interludeIntro.interludeBegin1.alpha = 1;
You make the movie clip visible, but since its parent is still invisible, you don't see anything. The solution is to loop through the movie clips and make each of them invisible/visible.
// Keep the parent visible at all time
interludes.interludeIntro.alpha = 1;
for (var i:int = 0; i < textArray.length; i++) {
textArray[i].alpha = 0;
}
// Now this will work:
interludes.interludeIntro.interludeBegin1.alpha = 1;

multiple instances of the same object spaced out using a loop is only creating one

I had a hard time trying to word my question properly, so i'm sorry if it seems confusing. Also i'm using the flixel library in flash builder. It may not be that important butcause probably anyone that knows a little more than me or even a little AS3 could probably see what i'm doing wrong.
Anyway, what i'm trying to do is basically create 10 instances of this square object I made. all I have to do is pass it an x an y coordinate to place it and it works. so ive tested if i just do:
var testsquare:Bgsq;
testsquare = new Bgsq(0,0);
add(testsquare);
it works fine and adds a square at 0,0 just like i told it to, but i want to add 10 of them then move the next one that's created 25 px to the right (because each square is 25px)
my problem is that I only ever see 1 square, like it's only making 1 instance of it still.
anyone possibly have an idea what I could be doing wrong?
var counter:int = 0;
var bgsqa:Array = new Array;
for (var ibgs:int = 0; ibgs < 10; ibgs++)
{
bgsqa[counter] = new Bgsq(0,0);
bgsqa[counter].x += 25;
add(bgsqa[counter]);
counter++;
}
There's a lot you're doing wrong here.
First off, you're using a pseudo-iterator (counter) to access array elements through a loop instead of, well, using the iterator (ibgs).
Second, I don't see anything in the array (bgsqa) you're iterating through. It's no wonder you're having problems. Here's what you should do.
var bgsqa:Array = [];
for(var i:int=0;i<10;i++)
{
var bgsq:Bgsq = new Bgsq(i * 25, 0);
add(bgsq);
bgsqa.push(bgsq);
}
That should probably do it if your post is accurate.
for (var ibgs:int = 0; ibgs < 10; ibgs++)
{
bgsqa[counter] = new Bgsq(0,0);
bgsqa[counter].x = counter * 25;
add(bgsqa[counter]);
counter++;
}
They start at 0, so applying += is simply adding 25 to 0. This should do the trick.

Resources