all groups > flash actionscript > june 2005 >
You're in the

flash actionscript

group:

syntax for concatenate a function name?


Re: syntax for concatenate a function name? Chip W.
6/27/2005 12:00:00 AM
flash actionscript:
Hey K,

It doesn't work because you're not incrementing continuously, only once with
the n++ upon initiation. You'll need to use a for loop for incrementing. Try:

for(n=-1;n<10;n++){
// -------------
varfun = "variation" add n;
//
function varfun() {}
//..... etc
}

Maybe I'm missing something about how you're executing this, so if you already
know about for loops, then my apologies.

~Chipley


Re: syntax for concatenate a function name? Kleus Belt
6/27/2005 12:00:00 AM
Hi Chipley

I know about loops... also just to elaborate, the n++ will be added
after every new one, like this;

n++;
varfun = "variation" add n;
function varfun() {
..... etc
}
n++;
varfun = "variation" add n;
function varfun() {
..... etc
}
n++;
varfun = "variation" add n;
function varfun() {
..... etc
}

This way I can insert a new set of function definations at any point in
the list (since I need to keep it alphabetically but numerical)

Since all functionX (with X the number) has its own set of
parameters/variables, I see no way to use the for() loop, all need to be
defined seperately... therefor I need some way to make the function
names use the n++ counter in the name without being in a for() loop

Hope this makes sense
Basically I need to have the correct accepted by flash syntax for
function "variation" add n ();
since this way it gives error
Any thoughts?

[quoted text, click to view]
syntax for concatenate a function name? Kleus Belt
6/27/2005 1:57:12 PM
I'm trying to define a set of functions
Hardcoding them like

function variation0() {
..... etc
function variation1() {
..... etc
function variation2() {
..... etc


works, but I would like them to use a n++ counter so I can more easily
insert new functions inbetween. If I try

n = -1;
// -------------
n++;
varfun = "variation" add n;
//
function varfun() {
..... etc

It does not work, als not if I use "variation" add n instead of varfun
directly in function () AS line.
Anybody knows what the appropriate syntax is to do this?

Kleus
Re: syntax for concatenate a function name? Kleus Belt
6/27/2005 5:32:54 PM
Hi Rothrock,

[quoted text, click to view]

That would be really a pitty

[quoted text, click to view]

well, I don't think will be the case...
I can show you simplified what I have now. I am using 4 test - functions
like this (shortened list just to demonstrate;

effectstotal = 3;
function variation0() {
variationname = "Default";
_root.xch.starttype = 0;
_root.xch.aligntype = 0;
_root.xch.duration = 20;
_root.xch.startXscale = 100;
_root.xch.startYscale = 100;
_root.xch.endXscale = 25;
_root.xch.endYscale = 25;
}
function variation1() {
variationnr = 1;
variationname = "Bypasser";
_root.xch.starttype = 2;
_root.xch.duration = 16;
_root.xch.startXscale = 400;
_root.xch.startYscale = 0;
_root.xch.endXscale = 0;
_root.xch.endYscale = 400;
}
function variation2() {
variationnr = 2;
variationname = "Final countdown";
_root.xch.starttype = 4;
_root.xch.startXscale = 0;
_root.xch.startYscale = 0;
_root.xch.endXscale = 800;
_root.xch.endYscale = 100;
}
function variation3() {
variationnr = 3;
variationname = "Waterfall";
_root.xch.startYscale = 0;
_root.xch.endYscale = 800;
_root.xch.startalpha = 100;
}

according to the total (4 in example) 4 MC's are duplicated and given
numerically named instance names, all containing a button... the button
then have

on (release) {
// reset all variables to default
_parent.variation0.call();
// now do the variation variables from list
_parent.butnr = this._name;
_parent.fx = _parent.butnr.charAt(3);
eval("_parent.variation" add _parent.fx).call();
}


What these buttons basically do is calling function variation0 ()
resetting a base list of variables to default ones and then variation1
() or variation2 () or variation3 () (depending on instance name) to set
a new set of variables/parameters.

This works fine.
But when I need to insert a new set (a new function) inbetween, because
the list is alphabetically then it gets workintensive updating
everything manually

By using a counter n++ I can simply insert a new function anywhere
inbetween but this will need a common


n++;
varfun = "variation" add n;
function varfun() {
..... etc
}
n++;
varfun = "variation" add n;
function varfun() {
..... etc
}
n++;
varfun = "variation" add n;
function varfun() {
..... etc
}

routine since in a for() loop it can't be done... the above way also
facilitates naming of buttons by e.g. using

n++;
"variationname" add n = "Bypasser";

The list of variables comes from a .txt already made and I use a batch
to simply convert the list from .txt to a flash-actionscript text as the
one described above.

Any suggestion?
Kleus
Re: syntax for concatenate a function name? Rothrock
6/27/2005 7:47:05 PM
I don't think you can do this in ActionScript. You are trying to mix runtime
things like variable concatenation with author time things like defining your
functions and the two won't mix. I'm unaware of any way to dynamically generate
AS code.

This also seems like a really bad strategy to me ? perhaps there is something
I'm missing? But if you insert a new variation, then all subsequent variations
change their names. Meaning that all references to them in the rest of your
code will be off by one.

What are you trying to accomplish? Perhaps there is a better way.
Re: syntax for concatenate a function name? Kleus Belt
6/27/2005 8:10:26 PM
You're a life saver ;)
works like a charm, knew there must be some easy solution
Kleus

[quoted text, click to view]
Re: syntax for concatenate a function name? Kleus Belt
6/27/2005 9:26:42 PM
[quoted text, click to view]

That is something I still think might be true. But my knowledge of AS is
not on guru-level ;)

Just out of curiosity, so far I have a script working. I stripped it
down a bit to show

n = -1;
// -------------
n++;
set("variationname" add n, "Default");
this["variation"+n] = function () {
variationnr = 0;
_root.xch.starttype = 0;
_root.xch.aligntype = 0;
_root.xch.duration = 20;
_root.xch.startXscale = 100;
_root.xch.startYscale = 100;
_root.xch.endXscale = 25;
_root.xch.endYscale = 25;
};
// -------------
n++;
set("variationname" add n, "Bypasser");
this["variation"+n] = function () {
variationnr = 1;
_root.xch.starttype = 2;
_root.xch.duration = 16;
_root.xch.startXscale = 400;
_root.xch.startYscale = 0;
_root.xch.endXscale = 0;
_root.xch.endYscale = 400;
};
// -------------
n++;
set("variationname" add n, "Final countdown");
this["variation"+n] = function () {
variationnr = 2;
_root.xch.starttype = 4;
_root.xch.startXscale = 0;
_root.xch.startYscale = 0;
_root.xch.endXscale = 800;
_root.xch.endYscale = 100;
};
// -------------
n++;
set("variationname" add n, "Waterfall");
this["variation"+n] = function () {
variationnr = 3;
_root.xch.startYscale = 0;
_root.xch.endYscale = 800;
_root.xch.startalpha = 100;
_root.xch.endalpha = 0;

};
effectstotal = n;
for (n=0; n<=effectstotal; n++) {
duplicateMovieClip("varbut", "varbut" add n, n);
setProperty("varbut" add n, _y,
Number(varbut._y)+Number(varbut._height+1)*n);
}


The list of variables per function is a loooot bigger but this should
give the general idea.

The main issue is that if I want to insert a new function (read "a new
menu item button") I can simply insert it at any point to keep the list
Alphabetical. The variables before the function AS lines like "Final
countdown" etc are used by a list of duplicated menuitems (see last part
of script) as button texts, and that is why I have the approach as I do
now. The function definitions I extract from a .txt file (from a
different application source) and use a MACRO to batch a whole file (or
a newly added ~variation~) with one click into the AS format as I use above.
Hope you get the idea.

Anyway, do you still think a different approach would save more time and
trouble? If so, any sugestions are welcome
Kleus
Re: syntax for concatenate a function name? Rothrock
6/27/2005 10:53:11 PM
Doh! Can't believe I missed that.

Re: syntax for concatenate a function name? Jeckyl
6/28/2005 12:00:00 AM
yeup .. definitely better with an array of functions.
--
Jeckyl

Re: syntax for concatenate a function name? Jeckyl
6/28/2005 12:00:00 AM
without seeing what you've done .. no.

I'm just giving you some ideas for a different design .. they aren't just
going to plug into what you've already got without you fitting them and
debugging etc.
--
All the best
Jeckyl

Re: syntax for concatenate a function name? Jeckyl
6/28/2005 12:00:00 AM
variations = new Array;
// add "Default"
var f = function () {
_root.xch.starttype = 0;
_root.xch.aligntype = 0;
_root.xch.duration = 20;
_root.xch.startXscale = 100;
_root.xch.startYscale = 100;
_root.xch.endXscale = 25;
_root.xch.endYscale = 25;
};
f.name= "Default";
f.variationnr = variations.push(f);

// add "Bypasser"
var f = function () {
_root.xch.starttype = 2;
_root.xch.duration = 16;
_root.xch.startXscale = 400;
_root.xch.startYscale = 0;
_root.xch.endXscale = 0;
_root.xch.endYscale = 400;
};
f.name = "Bypasser";
f.variationnr = variations.push(f);

// add "Waterfall"
var f = function () {
_root.xch.startYscale = 0;
_root.xch.endYscale = 800;
_root.xch.startalpha = 100;
_root.xch.endalpha = 0;
};

f.name = "Waterfall";
f.variationnr = variations.push(f);

for (n = 0; n < variations.length; n++) {
var newclip = duplicateMovieClip("varbut", "varbut" add n, n);
newclip._y = varbut._y+(varbut._height+1)*n;
newclip.num = n;
// in here one can refer to variations[n] to be the correspoding
function
// can refer to variations[n].name to refer to the name (like
"Waterfall")
// within a clip, can refer to 'num' property to know which number clip
it is
// not sure why you were setting variationnr .. do you need that????
}

Nicer would be to put the values for startYScale etc in an object, and keep
an array of those object. Then you just need a single function that copies
the settings from the appropriate object to the _root.xch eg

variations = [
{ name : "Default", starttype : 0, aligntype : 0, duration : 20,
startXscale : 100, startYscale : 100, endXscale : 25, endYscale : 25 },
{ name : "Bypasser", starttype : 2, duration : 16, startXscale : 400,
startYscale : 0, endXscale : 0, endYscale : 400 },
{ name : "Waterfall", startYscale : 0, endYscale : 800, startalpha : 100,
endalpha : 0 }
];
callVariation() = function (n) {
for (var j in variations[n]) {
_root.xch[j] = variations[n][j];
}
}

then you can use 'callVariation(1)' to set up variation 1 etc

Jeckyl

Re: syntax for concatenate a function name? Kleus Belt
6/28/2005 12:00:00 AM
Hi Jeckyl
Tnx for your help, much appreciated!

[quoted text, click to view]


[quoted text, click to view]

hm... this one does not seem to work, I can verify/see the buttons are
duplicated but not set in correct places.
Replacing with my old AS line
setProperty("varbut" add n, _y,
Number(varbut._y)+Number(varbut._height+1)*n);
does work however.. very strange

[quoted text, click to view]

.... <cut> can refer to variations[n].name to refer to the name

works like a charm

but ... <cut> refer to 'num' property to know which number clip it is
// not sure why you were setting variationnr .. do you need that????

gives blank when I try it... e.g. putting a texfield inside each MC
button with 'num' as variable leaves them blank.
Pity, I actually only maybe/might need a parameter to remember the last
button selected, that is why I left the "variationnrX" variable but I
might not even need it, and in worst case I can substring it from button
instance names even if the 'num' you added does not do the trick.


[quoted text, click to view]

Very interesting! Also started testing this one out..

Seems to me that callVariation() will perform while j++ within 'n' the
maximum number of parameters in the function defined, correct?


I noticed callVariation() = function (n) {
gives error "Left side of assignment operator must be variable or
property", so stopped testing at that point.
see any error?

-K-
Re: syntax for concatenate a function name? Jeckyl
6/28/2005 12:00:00 AM
[quoted text, click to view]

Is strange

[quoted text, click to view]
that's a typos
callVariation = function (n) {

Re: syntax for concatenate a function name? Bart Calixto
6/28/2005 12:00:00 AM
What about parameters and a switch?
maybe I didn't get the point of the question due to my native is spanish or
something, but sounds more 'logical' from the other side of the world :P


function variation(type) {
switch (type) {
case 0:
.... bla bla bla
break;
case 1:
.... bla bla bla
break;
case 2:
.... bla bla bla
break;
case 3:
.... bla bla bla
break;
case 4:
.... bla bla bla
break;
default:
.... bla bla bla
break;
}
}

Cheers!
Bart Calixto.


[quoted text, click to view]

Re: syntax for concatenate a function name? Jeckyl
6/28/2005 12:00:00 AM
n = -1;
n++;
this["variation"+n] = function () { ...};
n++;
this["variation"+n] = function () { ...};
n++;
this["variation"+n] = function () { ...};
--
Jeckyl

Re: syntax for concatenate a function name? Kleus Belt
6/28/2005 4:20:32 AM
[quoted text, click to view]

yeah, got it working now.
The only drawback to this is that the last variable line will need to
finish without a comma and that might require some extra modification in
the MACRO used for original .txt to AS definitions batching.

ps.. any idea why the newclip.num = n; results in blanks?
Re: syntax for concatenate a function name? Kleus Belt
6/28/2005 1:36:03 PM
[quoted text, click to view]

exactly copying from your sugestion, no mod's. Anyway, just thought you
might see some syntax error in it. Never mind, got it working as is anyway

[quoted text, click to view]

yeah, but they do p&p about 90%, very efficient ;)
Thanks again for your input, it solved my problem even better then I had
hoped for.
K
AddThis Social Bookmark Button