Dynamic Variable Name - actionscript-3

I need to create a variable:
var numDots0:Number=0;
But when a button is clicked the variable numDots0 becomes numDots1, then numDots2 on a second click, and so on. I then need to be able to grab that new variable name and use it in a function.

That's a really, really weird request, but anyways:
You can use the key name of an Object to store the property and then change that:
var obj:Object = { numDots0: 0 };
And then when you want to change the name:
delete obj.numDots0;
obj.numDots1 = 1;
Or to easily increment you can use this:
var i:int = 0;
function increase():void
{
delete obj["numDots" + i];
obj["numDots" + (++i)] = i;
}
To access:
trace(obj.numDotsX); // where X is the most recent variable name.
I see absolutely no benefit or need for this, so I strongly suggest taking a look at what you're trying to do and making sure it makes sense and doesn't have a different application.

I am pretty sure you are going the wrong way about the problem you are trying to solve. Dynamic variable names are not something you read in the best practices book.
Anyway to answer your question in AS2 you could use the command eval which would evaluate a string as ActionScript, so you would use something like:
function onClicked(e:MouseEvent):void
{
counter++;
eval("var numDots" + counter +"+:Number=0;");
}
In AS3 that command has been removed (because it leads to bad coding practices - like the things you are trying to do), nevertheless someone implemented an evaluator in AS3:
http://eval.hurlant.com/
With this evaluator add the library to your project and add the following to the snippet above:
function eval(expression:String):void
{
var evaluator:com.hurlant.eval.Evaluator = new com.hurlant.eval.Evaluator();
var bytes:ByteArray = evaluator.eval(expression);
bytes = ByteLoader.wrapInSWF([bytes]);
var context:LoaderContext = null
var loader:Loader = new Loader();
loader.loadBytes(bytes, context);
}

the answer is to not do what you are trying to do and use an array, hash or vector instead. give us a bit more context, or the reason you want to achieve exactly what you want to and why you might believe you'd need a dynamic variable name like that. you shouldn't be using evals or anything that changes variable name at runtime because the gods of programming will strike you down where you stand. i.e., your program is going to break, and when it does, it's going to be harder to debug for sure.
if you are sure this is what you want to do, then i'm wrong, haha. good luck!

Related

Find what _local_x or _arg_x means?

I am working on a large Flash project.
I have tried "Goto Declaration" but that doesn't seem help.
Btw I am using FlashDevelop. And Yes I can perfectly compile and build TO 100% working source.
Here is a code sample. I know you can't do much with this but tell how I can work with this.
public function aim_(_arg_1:Vector3D, _arg_2:Vector3D, _arg_3:ProjectileProperties):Vector3D
{
var _local_4:Vector3D;
var _local_5:GameObject;
var _local_6:Vector3D;
var _local_7:Number;
var _local_8:Number;
var _local_9:int;
var _local_10:Boolean;
var _local_11:int;
var _local_12:Boolean;
var _local_13:* = undefined;
var _local_14:int = Parameters.data_.aimMode;
var _local_15:Number = (_arg_3.speed_ / 10000);
var _local_16:Number = ((_local_15 * _arg_3.lifetime_) + ((Parameters.data_.Addone) ? 1 : 0));
var _local_17:Number = 0;
var _local_18:Number = int.MAX_VALUE;
var _local_19:Number = int.MAX_VALUE;
aimAssistTarget = null;
for each (_local_5 in map_.goDict_)
{
if (_local_5.props_.isEnemy_)
{
_local_10 = false;
for each (_local_11 in Parameters.data_.AAException)
{
if (_local_11 == _local_5.props_.type_)
{
_local_10 = true;
break;
};
};
What you're trying to achieve is reverse engineering a decompiled code. With "_local" variables you need to investigate what values they are assigned, in what algorithms do they participate, and here you just need to read this single function in its entirety to be able to discern meaning of those local variables. But, you would also need to understand many of the named parameters to get some of those meanings. For example, _local_11 iterates through some Parameters.data_.AAException list of ints, and is compared with current outside loop iterator's props.type_, therefore "AAException" should mean "AA exemption" and _local_10 provides check result, whether current enemy is exempt from AA (whatever is that AA). And so on.
Same with _arg_X variables, you need to find out what's being passed into a function from wherever it's called, and retrieve the context of those parameters, also taking their type into consideration, like here _arg3 is of type "ProjectileProperties", meaning this function should relate to some projectile which properties affect its outcome somehow. Most likely it's taking two vectors of projectile (or source, this is outside of this code) and target (or speed, same here), and generates another vector of yet unknown purpose.
When you have investigated every function like this, you'll have quite a bunch of pieces to a single puzzle that you can combine by references, discovering all the algorithms that combine the code of whatever app you've decompiled. Then you will be able to do targeted modifications of whatever kind you wanted initially. But yes, it'll be better if you'd have access to actual sources from whoever created this the first time.
In short: Think. Think, think and think.

How can I make a function to make a child of an object in the library based on the variables supplied?

I'm trying to do a little project for my class and though I know how to do it the long way I'd prefer to do it in a more intuitive way so that I can avoid having to copy and paste a load of essentially the same code. The idea is to have a function which will create an instance of a class object with it's own unique name, set it's position/size/etc, and then add that child to the stage. Looking at this (what I have now) might help out a little bit.
//Set up variables for all deco pieces
var decoGreen:GreenBall;
var decoRed:RedBall;
var decoStar:Star;
var decoFlower:Flower1;
var decoYellow:YellowBall;
var decoBlue:BlueBall;
//Functions to allow easier object placement
function makeDeco(posX:Number, posY:Number, decoName:String, rootClass:Object):void
{
decoName = new (rootClass)();
decoName.x = posX;
decoName.y = posY;
addChild((decoName));
}
makeDeco(90,320,"greenBall",GreenBall)
Now obviously this code doesn't work and it's pretty rough right now but I think it's sufficient to understand what I'm trying to accomplish here. Thanks for any and all who attempt to decipher my mess! :D
You are pretty close from what I can tell and if I understand your question, it would simply be using the getDefinitionByName class
function makeDeco(posX:Number, posY:Number, decoName:String):void
{
var DecoClass:Class = getDefinitionByName(decoName) as Class;
var deco:DisplayObject = new DecoClass();
deco.x = posX;
deco.y = posY;
addChild((deco));
}
makeDeco(90,320,"greenBall")
You don't need to define the variables initially like you did, granted they've all set to "Export as actionscript" in the library. For example calling a string of "greenBall" would mean you have a movie clip in the library with a class name of greenBall

Passing local variable to loader anonymous handler function

Why isn't this working as I am thinking it would:
var i:int=-1;
for each(obj in myData)
{
i++;
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(event:Event)
{
trace(i);
});
}
There are 3 objects in myData and the trace statement looks like:
2
2
2
Instead of:
0
1
2
If I add i to an array (like myArr.push(i)) it will have 3 elements, 0, 1 and 2.
Any ideas?
Thank you.
That's a very bad approach you've taken... Just don't do any of those things you are trying to do, and it'll be fine... No point in using anonymous function here (it's never actually in AS3), no point to use for-each, because what you need is for(;;). You use dynamic typing for no benefit what so ever (there's no benefit in dynamic typing in AS3 and never was anyway). And, yeah, the closure will capture the context, the context has only one i, and it's value is 2, so the first trace is what you should expect.
What you should be doing - store the loaders in some data structure and fetch them from that data structure later (when you need that identifier). And please, for the sake of us users, load whatever you are trying to load sequentially - because if you don't, we'll get the IO errors you aren't handling...
First let me tell you why it doesn't work as you expect.
What is happening is, the for is looping through your elements, and creates all the loaders, incrementing i, but the Event.COMPLETE happens sometime later, where the i is already at the value 2, so that's why you get that output.
As wvxvw suggested, you need some more data structure, something like this:
class MyLoader {
private var i: int;
private var loader: Loader;
function MyLoader(i:int) {
this.i = i;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
}
function onLoaded(event:Event)
{
trace(i);
}
}
And you will use it in your loop:
var i:int = 0;
for each(obj in myData) {
var loader:MyLoader=new MyLoader(i++);
}
Of course, you will need to add lots more to that MyLoader, like handling the errors, and pass more meaningful things to make everything work.

Changing the value of a var via reference in AS3

I'm pretty positive what I want to do isn't possible with ActionScript, but it would be nice to be wrong.
I need to pass a variable reference to a function, and have the function change the value of the variable.
So, simplified and not in completely correct syntax, something like this:
function replaceValue(element:*, newValue:String):void
{
element = newValue;
}
var variableToModify:String = "Hello";
replaceValue(variableToModify, "Goodbye");
trace(variableToModify) // traces value of 'Hello', but want it to trace value of 'Goodbye'
Of course, in the replaceValue function, element is a new reference to fungibleValue (or, rather, a new reference to fungibleValue's value). So, while element gets set to the value of newValue, fungibleValue does not change. That's expected but totally not what I want, in this case.
There's a similar question, for Ruby, here Changing value of ruby variables/references
As the question points out, Ruby has a way to accomplish this. BUT is there any way to do this in ActionScript?
If so, it's going to make something stupid a lot easier for me.
No it's not possible the function will always get the value and not the reference. But if you are able to call replaceValue why not returning the new value from your function :
function replaceValue(element:*, newValue:String):String
{
// .. do your work
return newValue;
}
var variableToModify:String = "Hello";
variableToModify = replaceValue(variableToModify, "Goodbye");
trace(variableToModify)
If you pass an Object or a Class, you can modify one fiels based on his name as :
function replaceValue(base:Object, fieldName:String, newValue:String):void {
// do your work
base[fieldName] = newValue;
}
var o:Object={ variableToModify:"Hello" };
replaceValue(o, "variableToModify", "Goodbye");
trace(o.variableToModify);

How do I reference an object, add a tween to it and put this in an Eventlistener in AS3?

I'm having quite some trouble to try and get an app I wrote in AS2 to AS3. The reason I need to go to AS3 is something icky, so I won't go into detail about it.
I've got 90% of the application running with the new code.
Now I've come to the point where I have to convert this code from AS2,
function setAnimation(theObject,id)
{
theObject.vensterid=id;
theObject.onEnterFrame = function()
{
var myHoriTween:Tween = new Tween (this,"_x",Strong.easeOut,this._x,(130+((theObject.vensterid-frameno)*260)),1,true);
}
}
setAnimation(venster0,0);
, to AS3. My attempt of doing this ended up like
function setAnimation(anObject,id) {
var theObject = this[anObject];
theObject.vensterid=id;
function slideHorizontal(event:Event)
{
var myTween:Tween = new Tween (theObject,"x",Strong.easeOut,this.x,(130+((theObject.vensterid-frameno)*260)),1,true);
}
theObject.addEventListener(Event.ENTER_FRAME,slideHorizontal);
}
setAnimation(venster0,0);
and gives me the following non-error (it doesn't show as a compiler error, but as output):
TypeError: Error #1010: A term is undefined and has no properties.
at sliding_windows_as3_fla::SlideMenu_1/setAnimation()
at sliding_windows_as3_fla::SlideMenu_1/frame1()
I think this is very strange since it doesn't say anything about which term (and there are quite a lot) and googling didn't find me an explanation either.
I didn't get the chance to test your code, because it's difficult to set up a context for it, but my thoughts would be:
You should declare the parameter types: function setAnimation(anObject:Object,id:uint):void. It's at least good practice.
var theObject = this[anObject]; is completely unnecessary if your variable anObject is an object. I think var theObject = this[anObject]; doesn't work, theObject ends up being null and that's why you get your error. If you have declared a variable called venster0, that is the instance of a class that extends Object, then you can pass the reference to it without any other trouble.
Depending on the object you work with, theObject.vensterid=id; might not work. The class that theObject instances must have the 'vensterid' property, or you will get `1119: Access of possibly undefined property vensterid through a reference with static type ...
I think your problem here is following string:
var theObject = this[anObject];
Just replace it with
var theObject = anObject;
I hope that's what you need.
Alternatively instead of
setAnimation(venster0,0);
you could pass an instance name (i.e. String):
setAnimation("venster0",0);
That will work because by this['propertyname'] you are actually accessing Object's property by name.
Just going to throw out that using the built-in Tween classes in Flash/Flex is a pain. Look into using Tweening libraries instead: Tweener, TweenLite, etc. They are much easier to work with, and you don't have to worry about maintaining references until the Tween completes.