Be a Supporter!

as3 checking variable name

  • 240 Views
  • 7 Replies
New Topic Respond to this Topic
HuDsOn90Five
HuDsOn90Five
  • Member since: Nov. 5, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
as3 checking variable name Mar. 14th, 2013 @ 05:01 PM Reply

Is there a way to check a variables name or if not the class's name.
For example:

Var targets:Array = []

Targets = zombies;
//targets array contains other objects apart from zombies

For(var I:int = 0; I < targets.length; I++)
{

If (targets [i] .kinGroup == zombies)//kinGroup is the name of the array it is part of.
{
//do stuff
}//doesnt work

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Online!
Forum Stats
Member
Level 26
Programmer
Response to as3 checking variable name Mar. 14th, 2013 @ 05:52 PM Reply

At 3/14/13 05:01 PM, HuDsOn90Five wrote: Is there a way to check a variables name or if not the class's name.
For example:

cast to the desired type, if it turns out to be null, ignore it

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to as3 checking variable name Mar. 14th, 2013 @ 06:24 PM Reply

I'm not sure what your trying to do, but it might help you to know that all objects have string name properties for instance:

myObj.x = 5;

is the same as:

myObj['x'] = 5;

and you can see if any object has a public variable of a certain name by saying

if("x" in myObj){trace(myObj[x]);}
Mattster
Mattster
  • Member since: Nov. 21, 2007
  • Offline.
Forum Stats
Member
Level 20
Game Developer
Response to as3 checking variable name Mar. 14th, 2013 @ 08:08 PM Reply

I think what you're looking for is the "is" keyword. You can check to see a class type like so:

if (targets[i] .kinGroup is Zombies){

}else if (targets[i].kinGroup is Elves){

}..etc
HuDsOn90Five
HuDsOn90Five
  • Member since: Nov. 5, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to as3 checking variable name Mar. 15th, 2013 @ 07:20 AM Reply

At 3/14/13 08:08 PM, Mattster wrote: I think what you're looking for is the "is" keyword. You can check to see a class type like so:

if (targets[i] .kinGroup is Zombies){

}else if (targets[i].kinGroup is Elves){

}..etc

Thank you, this is what i was looking for

HuDsOn90Five
HuDsOn90Five
  • Member since: Nov. 5, 2009
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to as3 checking variable name Mar. 15th, 2013 @ 07:33 AM Reply

At 3/14/13 08:08 PM, Mattster wrote: I think what you're looking for is the "is" keyword. You can check to see a class type like so:

if (targets[i] .kinGroup is Zombies){

}else if (targets[i].kinGroup is Elves){

}..etc

Thank you, this is sort of what I am looking for, but rather than checking the class name is there a way to check the array name? Im not really sure how to explain it but targets is combined of:

zombie classes which kinGroup is the "zombies" array which contains all zombies on stage.
tank and bunker classes which both kinGroup is "towers" array which contain all the tanks and bunkers on stage but could contain different classes in the future.

So rather than doing :

if(targets[i] is zombie){}

else if(targets[i] is tank){}

else if(targets[i] is bunker){}

Could i do something like:

if(targets[i].kinGroup is zombies){}

else if(targets[i].kinGroup is towers){}

if you catch my drift?

Mattster
Mattster
  • Member since: Nov. 21, 2007
  • Offline.
Forum Stats
Member
Level 20
Game Developer
Response to as3 checking variable name Mar. 15th, 2013 @ 07:56 AM Reply

At 3/15/13 07:33 AM, HuDsOn90Five wrote: if you catch my drift?

I haven't tried this before, but you could make your arrays vectors. So you could so something along the lines of

//example vector
var v:Vector.<Zombie> = new Vector.<Zombie>();

if(targets[i].kinGroup is Vector.<Zombie>){

}else if(targets[i].kinGroup is Vector.<Bunker>){

}...

Now, that's if you want that format. Another solution (which I suggest) would be to make "kinGroup" an Object. Then check:

var group:Object = targets[i].kinGroup;
if ("zombies" in group){

}else if ("bunker" in group){

}...

or you could loop through it

for (var s:String in group){
    if (s == "zombies"){
       trace("zombie",group[s]);
   }
}
GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to as3 checking variable name Mar. 15th, 2013 @ 02:50 PM Reply

At 3/15/13 07:33 AM, HuDsOn90Five wrote:
if(targets[i].kinGroup is zombies){}

else if(targets[i].kinGroup is towers){}

if you catch my drift?

assuming "zombies" is a variable and not a type then you would say
if(targets[i].kinGroup == zombies)

flash can tell if 2 objects are copies of the same thing, but they can't be 2 different objects with the same properties, let me explain:

var a:Array = [1,2,3];
var b:Array = a;
trace(b == a); // --- true

that works fine, in fact if you change b in any way, a will be changed too.
however this does not

var a:Array = [1,2,3];
var b:Array = [1,2,3];
trace(b == a); // --- false
}

that won't work because they are 2 different arrays that just so happen to have the same information. you can change b without changing a. This is why you'll notice that classes like Point have a clone() function