Referencing a variable based on a combination of strings? - actionscript-3

I don't really know how to word this.
If I have mutliple variables like
var aHeight:Number = 30;
var bHeight:Number = 43;
var cHeight:Number = 02;
var dHeight:Number = 60;
var aHeight:Number = 20;
and I make a function like
function (mc:MovieClip,heightLetter:String) {
mc.y = ?;
}
if I just want to reference the letter but not the whole variable is there a way to find it like
[heightLetter+"Height"]
or something like that?

You can, if these vars are in fact object properties, that is, they are declared outside any functions, and are thus available via this.aHeight etc. And then indeed, you use this[heightLetter+"Height"] to get the required variable. Although it would be better that you use an array instead.

Related

as3 replacing array with another array but same name

this question should be an easy one, but how do I replace an array or define it and then give it values based off a users choice. The code with the if statements below are some choices I need in one or both arrays.
For example
if(choice=="easy")
{
var sorted:Array = new Array("Beau","Dad","Jesus","Mary","Mom");
}
if(choice=="hard")
{
var sorted:Array = new Array("Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley","Daddy","Mommy","Grandma","Grandpa","Nana","Gepa");
}
But this doesn't work above.
Thanks.
Declare the variable outside the condition instead (I also changed if/if to if/elseif as choice can't be both easy and hard at the same time):
var sorted:Array;
if(choice=="easy")
{
sorted = new Array("Beau","Dad","Jesus","Mary","Mom");
} else if(choice=="hard")
{
sorted = new Array("Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley","Daddy","Mommy","Grandma","Grandpa","Nana","Gepa");
}
As an additional option to h2oooooo' answer , you can use something like:
var sorted:Array = {
"easy":["Beau","Dad","Jesus","Mary","Mom"],
"medium":["Jordyn","Presley","Jesus","Mary","Nana"],
"hard":["Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley",
"Daddy","Mommy","Grandma","Grandpa","Nana","Gepa"]
}[choice];
trace(sorted.constructor); // [class Array]

Combining a variable with a number

I'm trying to make a inventory using an array in a game I'm making.
What I need is a way to combine a number with a variable, something like this:
itemBoxNumber = "itemBox" + currentItemBox;
//In this case itemBoxNumber would say itemBox1
that I could use to replace the itemBox1.
function itemsMenuUpdate():void
{
for (var a:int = 0; a<maxInventory; a++){
var currentItemBox:Number = 1;
if(~inventory.indexOf("Potion")){
mainMenu.itemBox1.gotoAndStop("Potion");
}
if(~inventory.indexOf("Hi-Potion")){
mainMenu.itemBox1.gotoAndStop("Hi-Potion");
}
}
}
I can only find working methods for AS2. Any help is appreciated with this.
If I remember right, you can call a method from a String in AS3.
For example, if you want to call the method itemBox1 on mainMenu you can do:
mainMenu["itemBox1"]
Which is the same as:
mainMenu.itemBox1
So in your example you could do this:
mainMenu[itemBoxNumber].gotoAndStop("Potion");
You can retrieve the child display object via a name (which you need to set prior). So:
var itemBoxNumber = "itemBox" + currentItemBox;
mainMenu.getChildByName(itemBoxNumber).gotoAndStop("Potion"); //TODO ensure the child was given such a name
Make a LabelNumber Class, which has both a String label and a Number. You can then add a method that returns a String of the label and the number combined, while keeping the values independent of one another.

AS3 - getting variable named in order within a function

I am sorry if this is a beginner's question.
I made some Arrays named like map01, map02 and so on... As you can see, I'm making a tile-based flash here. And I need to make a function that when you input a number like: createmap(1); it will get the variable map01 and use the information.
Can I do anything like: var temp:Array = Array(["map" + valueInput]);??
Please tell me if you need anything more.
First, instead of having variables with indices in their names, you should create an array of them. Here, an array of arrays.
So you just have to call var temp:Array = maps[valueInput] as Array;.
If you really don't want to do that and stick with your n variables, you can write
var index:String = valueInput.toString();
if (index.length == 1)
index = "0" + index; //have the index on two digits "01", "02"
var temp:Array = this["map" + index];
Note that it will only work for your 99 first variables (oh God...)

AS3 class instances names

I really wonder. I made a MovieClip class Apple, I wrote a function that creates a new instance with a name "apple". Each time a new instance is pushed into an Array "apples". I call the function 5 times and I get 5 apples. I can manipulate them by calling them i.e. apples[0]. And when I trace my array I see 5 [object Apple] things. So maybe I don't really understand the structure of AS3 objects but shouldn't each object have a name?
When I set apple.name and get an array with 5 different names, I can't manipulate objects by names like apple1.x = 10. How does computer know which apple is where if each has own coordinates? Is the only way to call them: apples[0]-apples[4]? And if I create a code that should be same for all apples, how should I address the function, to "this"? Cause when I write class code I don't have any names yet...
For example, if I want to make Apple class a picture(MovieClip) that can be dragged, create any number of apples, up to a million, I can't possibly add apples[0].addEventListener, apples[1].addEventListener ... apples[1000000].addEventListener to the code. How do I make it global?
I'm asking cause when I'm directly coding for a specific instance, it has a name and I know exactly what am I addressing. And working with a class and making many objects I kinda don't... Sorry, I'm green
You don't need names to be able to access and manipulate your instances.
You can do something like this:
var apples:Array = new Array();
for (var i:int = 0; i < 100; i++)
{
var apple:Apple = new Apple();
apples.push(apple);
apple.x = Math.random() * 500;
apple.y = Math.random() * 500;
addChild(apple);
apple.addEventListener(MouseEvent.CLICK, onAppleClick);
}
function onAppleClick(e:MouseEvent):void
{
var ind:int = apples.indexOf(e.currentTarget);
var apple:Apple = apples[ind];
trace(ind);
apples[ind].visible = false;
//or
//apple.visible = false;
}
The same problem, huh? :)
For example Apple extends Sprite and inherits property name.
It's public, this means that you can change its name.
var apple:Apple = new Apple() ;
apple.name = "MegaApple" ;
Also, if you add the apple to stage, you can get him via name.
stage.addChild(apple) ;
get apple back:
var oldApple:Apple = stage.getChildByName("MegaApple") ;
But that never means, that you can use apple like that:
MegaApple.move() - because name of apple is supposed to be a property of apple, not a name of a variable.
Of course, you can create a lot of apples manually:
var apple1 = new Apple() ;
var apple2 = new Apple() ;
var apple3 = new Apple() ;
But if they all behave the same way, there is no point in doing. That's why you store them into array.
Hope you understand what I mean.

AS3 How to make a kind of array that index things based on a object? but not being strict like dictionary

How to make a kind of array that index things based on a object? but not being strict like dictionary.
What I mean:
var a:Object = {a:3};
var b:Object = {a:3};
var dict:Dictionary = new Dictionary();
dict[a] = 'value for a';
// now I want to get the value for the last assignment
var value = dict[b];
// value doesn't exits :s
How to make something like that. TO not be to heavy as a lot of data will be flowing there.
I have an idea to use the toString() method but I would have to make custom classes.. I would like something fast..
Why not make a special class that encapsulates an array, put methods in there to add and remove elements from the array, and then you could make a special method (maybe getValueByObject(), whatever makes sense). Then you could do:
var mySpecialArrayClass:MySpecialArrayClass = MySpecialArrayClass();
var a:Object = {a:3};
var b:Object = {a:3};
mySpecialArrayClass.addElement(a,'value for a');
var value = mySpecialArrayClass.getValueByObject(a);
I could probably cook up a simple example of such a class if you don't follow.
Update:
Would something like this help?
http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/
Update:
Could you use the === functionality? if you say
if ( object === object )
it compares the underlying memory address to see if two objects are the same reference...