The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.39 / 5.00 38,635 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.09 / 5.00 15,161 ViewsIs it possible to change the name of a variable while the program is running
I want to reference 21 different variables all with names like
Ingredient1, Ingredient2...Ingredient21
how (if I can) would I make it so that I can reference them all by using a for loop and just changing the number part of the variable name
hmm something like this?
for(i=1;i<5;i++){
duplicateMovieClip(_root.ingredient, "Ingredient"+i, 1000+i); Kanye West Did What??????
They should make a black guy the manager, they know how to run shit!
2 Girls 1 Cup .... THE SONG!!!!
yah :) buuut is the duplicating movie clip necessary?
Also, I should clarify, it's to shorten this kind of code:
if (values==1) {
var ingredient1:Ingredient1 = new Ingredient1();
ingredientDrawArr[i]=ingredient1;
}
if (values==2) {
var ingredient2:Ingredient2 = new Ingredient2();
ingredientDrawArr[i]=ingredient2;
}
for (var i:int = 0; i < 21; i++) {
var s:String = "Ingredient" + i;
var cl:Class = getDefinitionByName(s) as Class;
var mc:MovieClip = new cl() as MovieClip;
ingredientDrawArr.push(mc);
} You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
At 10/19/10 03:27 AM, Redshift wrote: Holy crap, just use an Array.
Agreed, Arrays are the most powerful too in coding. If you are trying to reference things with variable names, DON'T! use array locations instead.
use the push() method to add items to an array, and to access item use:
ingerdientArray[location];
to access whatever variable is stored there. and since location is a number, to loop through an aray is as easy as believing in god.
I think you guys should read the thread and not just the title. "Use an array" is not the complete solution to his problem.
You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.
At 10/19/10 03:58 PM, ProfessorFlash wrote: I think you guys should read the thread and not just the title. "Use an array" is not the complete solution to his problem.
Why not?
#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}
At 10/19/10 04:08 PM, Redshift wrote:At 10/19/10 03:58 PM, ProfessorFlash wrote: I think you guys should read the thread and not just the title. "Use an array" is not the complete solution to his problem.Why not?
Well if you read his third post, he seems to have 21 classes in his library conveniently named "Ingredient"+Number. How are you going to create an instance of them all with "use an array"? You push the created instances to an array yes, but you first need to create them, and there comes in my solution.
You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.
If I was doing this, I'd personally prefer something like this:
public static var ingredients:Array =
[
Ingredient1,
Ingredient2,
Ingredient3,
...,
Ingredient<n>
]
Sure, you have to actually construct the array instead of dynamically using strings to fetch a class. But you get the added benefit of them being explicitly defined, and so that if one of those classes doesn't exist then you'll get a compile error, instead of some weird run-time error that might pop out randomly in the middle of your program. If that Array definition compiles without errors, then it's guaranteed that all elements of that array are actually defined classes.
Then all you have to do is:
for(var i:int = 0; i < ingredients.length; i++){
var ingredient:MovieClip = new ingredients[i]() as MovieClip;
// ingredient is now an instance of one of the elements of the ingredients Array
}
// or applied to the snippet of code Tygerman provided:
var ingredient:MovieClip = new ingredient[values]() as MovieClip;
ingredientDrawArr[i]=ingredient;
What you could do to improve on this scheme, is to make an Ingredient class like this:
package SomePackage{
import flash.display.Sprite;
import flash.display.MovieClip;
public class Ingredient extends Sprite{ // use Sprite as a light-weight wrapper
private static var ingredients:Array =
[
Ingredient1,
Ingredient2,
Ingredient3,
...,
Ingredient<n>
];
public var id:int; // indicates which ingredient it is
public var display:MovieClip; // the actual MovieClip instance of the ingredient
// this class is simply a nice wrapper
public function Ingredient(n:int){
if(n < 0){ // correct the index if it's below 0
n *= -1;
}
n %= ingredients.length; // if n is too large, loop back around
id = n;
display = new ingredients[id]() as MovieClip;
addChild(display);
}
}
}
Now you have a nice clean wrapper, where you can add more custom properties and methods to deal with ingredients if you wish, and initializing an ingredient is much nicer:
var ingredient:Ingredient = new Ingredient(someValue); #include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}