-
Hi guys, maybe I've got a bit of coders block but this one's got me stumped.......can I call a function as an expression? I think so but it doesn't seem to be working. Take a look below,
I declare the following variable,
------------------------------------------------------------
thefunc = "sweep";
------------------------------------------------------------
I declare the following function,
------------------------------------------------------------
function sweep () {
if (getProperty ("_root.grid.block"+n,_currentframe) != 1) {
tellTarget ("_root.grid.block"+n) {
play ();
}
} else {
n = ++n;
break;
sweep();
}
}
------------------------------------------------------------
I then wish to call this function in the following manner,
------------------------------------------------------------
thefunc + "()";
------------------------------------------------------------
But this doesn't appear to work. Surely the above is just the same as saying,
------------------------------------------------------------
sweep();
------------------------------------------------------------
is it not? I've also tried using an eval statement like this,
------------------------------------------------------------
eval(thefunc + "()");
------------------------------------------------------------
but to no avail......
Help me guys !!!!
Cheers.
-
I have tried it too, but it never worked, and I never have any confirmation about its possibilities or not. So I always end up using "if else" to dynamically call function. Something like
Code:
if (fn == "a"){
//then call a function
} else if (fn == "b"){
//then call b funcion
}
-
Ooooooooooooooooooooooooooooooooooooooooooooooobug ger.
Cheers anyway. If I find a solution (and I invariably do), I'll post it up here.
-
Let me know when you do. Way to go Shad0w
-
rapidcarbon > check this out >
http://pub1.ezboard.com/fflashboard1...cID=1034.topic
It works.
Regards,
Shad0w
-
proud new daddy!
yes, this is possible:
Code:
function showMe()
{
trace("output");
}
fName = "showMe";
this[fName]();
viola!
cheers!
-
Hey thanks LuxFX, the method you posted also works.....and requires less code! Bargin mate!
One last thing, could you possibly explain the difference between the two methods I've shown below, they both work 100% yet are done differently. Cheers.
Method 1 -
------------------------------------------------------------
thefunc = "showMe";
function showMe()
{
trace("output");
}
callfunc = eval(thefunc);
callfunc();
------------------------------------------------------------
Method 2 (your method) -
------------------------------------------------------------
thefunc = "showMe";
function showMe()
{
trace("output");
}
this[thefunc]();
------------------------------------------------------------
It would help a lot. Thanks again.
-
You got my respect FX. Thanks for the beautiful code.
-
Hi shadow,
there is no real difference
eval(varname) evaluates the string "varname" into the actual variable contents (plain variables) or a reference (objects, functions, movieclips) ... and so does this[varname]
The eval is a remnant from Flash 4, and the array notation is somewhat clearer, because you can also write _root[varname]
and other things that are just easier to read than their F4 counterparts.
BTW: not sure about it, but with functions even
thefunc();
might work - doing the same (use name rather than function) with objects definitely does not.
Musicman
-
proud new daddy!
Method 1
------------------------
callfunc = eval(thefunc);
callfunc();
In this method, you are creating an object that points to your function by name, and then you are calling that pointer object. It's like this;
y = 50;
x = y;
trace(x);
The output, of course, is '50'. Flash creates the variable x and gives it the same value as y. Same for Method 1, flash gives the object callfunc the same value as thefunc, it's just a function in this case instead of a variable.
Method 2
------------------------
this[thefunc]();
This method uses 'array access'. See, all objects exist in a grand array, and they can be accessed by name. So if you have a variable called userName in the main timeline, you can access it through array access with this: _root["userName"] and it will be the same as using an eval statement, just different notation. But you can access any object, not just variables, and this includes functions. Functions, too, are found in the grand array that includes all objects.
This method doesn't reassign or create anything, all it does is evaluate the name, and calls it. Here's a variation on this method that might be easier to interpret, it will look like a combination of the two methods:
eval(thefunc)();
See, once you evaluate a variable/object/function, you can use it just like anything else. So if you're evaluating a function, just slap some parenthesis on the end, and you can call the function.
This said, the main difference between these two methods is that the first one assigns a new object, then calls that object. The second just evaluates what the function is and calls it directly.
What this means is that the first method will use more memory and more processing time. Although, as Musicman said, there is no basic difference between the two--and they will function the same--there is a performance difference. I would recommend the second method, or its variation.
hope this is clear enough!
cheers!
-
Hi LuxFX,
since flash functions are constant, I would hope that a function variable just consisted of a memory address and a byte count rather than a copy of the function.
Do you know about any actual performance measurements?
Musicman
-
proud new daddy!
No, I don't know of any actual performance measurements. This is entirely my opinion, based on my experience and deductions.
You are correct (to my knowledge) about the function pointer. If my other post was misleading, I'm sorry. As far as I know, when you are assigning a function to another object, as in Method 1 above, it is simply creating a 'pointer', not a duplication of the object. The same way this code uses a movieclip:
Code:
function dupeMCs(num)
{
for (z=0;z<num;z++)
{
dot.duplicateMovieClip("dot" + z, z);
var tempMC = eval("dot" + z);
tempMC._x = z*10;
tempMC._y = 100;
tempMC._alpha = z*10;
tempMC.num = z;
}
}
Now there are a couple of things to note here. First, the variable 'tempMC' is simply a pointer to the movieclip, it does not contain a duplication of all of the material, simply a way to access it. Second, when I declared tempMC the first time, I declared it as var tempMC = instead of simply tempMC = . What this does is define the variable as a local, not global, object. What this means is that once the function is complete, that variable vanishes. I do this because no matter how insignificant the performance hit is, I want the code to be as lean as possible, and another variable is another variable.
Now in this example, I was performing several functions from the pointer. Knowing that 'eval' is the slowest function in all of flash, I knew that running one 'eval' to create a pointer, and then running the functions off of the pointer, would give me a drastically improved performance over using 'eval' for each function -- even if there is a small hit due to the creation of a new variable, etc. But in Shad0w's question, there is only one line of code that uses the pointer -- the actual calling of the function. So, since both methods have to 'eval' at some point, and exactly once, just getting rid of the pointer will help at least a little.
I know that these performance hits are so small as to be utterly insignificant in any save the most extreme cases. But whether or not it is an issue of speed, lean code is good code, so it is always a good idea.
-
Thanks guys. I see the difference now.
Cheers.
-
Just wonder where did you find all this info. From secret source from Macromedia Team?
-
proud new daddy!
I've fiddled with other programming languages before Flash, and these deductions are based off of similarities between my experiences here and there. No secret source, although if anyone can hook me up, there would be no complaining here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|