call multiple functions through a for cicle

  • esse_bi
  • Novice
  • Novice
  • esse_bi
  • Posts: 15

Post 3+ Months Ago

I'd like to call three function from a cicle for
when I trace "carica" inside my cicle it returns the name of my 3 functions but they don't execute,
where is my mistake?

thx

Code: [ Select ]
caricare1 = function() {
    trace("function1")
}
caricare2 = function() {
    trace("function2")
}
caricare3 = function() {
    trace("function3")
}

for (i=1;i<4;i++) {
    carica="caricare"+i+"()"
    this[carica];
// I tried also with carica instead of this[carica] and with this.carica and with [carica] but nothing // works
    trace(carica)
}
  1. caricare1 = function() {
  2.     trace("function1")
  3. }
  4. caricare2 = function() {
  5.     trace("function2")
  6. }
  7. caricare3 = function() {
  8.     trace("function3")
  9. }
  10. for (i=1;i<4;i++) {
  11.     carica="caricare"+i+"()"
  12.     this[carica];
  13. // I tried also with carica instead of this[carica] and with this.carica and with [carica] but nothing // works
  14.     trace(carica)
  15. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

well the problem is that functions are not objects so you can't build their names dynamically.

Are you trying to run each of the functions 3 times? If so the code would be
Code: [ Select ]
for(i=1;i<4;i++){
caricare1();
caricare2();
caricare3();
}
  1. for(i=1;i<4;i++){
  2. caricare1();
  3. caricare2();
  4. caricare3();
  5. }


Or are you trying to chain the functions so that one runs after the previous one? In this case I would simply add the function call to the end of the previous function.
Code: [ Select ]
caricare1 = function() {
trace("function1");
caricare2();
}
caricare2 = function() {
trace("function2");
caricare3();
}
caricare3 = function() {
trace("function3")
}
  1. caricare1 = function() {
  2. trace("function1");
  3. caricare2();
  4. }
  5. caricare2 = function() {
  6. trace("function2");
  7. caricare3();
  8. }
  9. caricare3 = function() {
  10. trace("function3")
  11. }
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

It's been at least a year since I've even opened Flash so something might have changed and I could be thinking of another language all together, but try adding the function execution operators at the end of your call instead of your definition.

Code: [ Select ]
carica="caricare" + i;
this[carica]();
  1. carica="caricare" + i;
  2. this[carica]();
  • esse_bi
  • Novice
  • Novice
  • esse_bi
  • Posts: 15

Post 3+ Months Ago

joebert wrote:
try adding the function execution operators at the end of your call instead of your definition.

Code: [ Select ]
carica="caricare" + i;
this[carica]();
  1. carica="caricare" + i;
  2. this[carica]();


yes! it works

thx a lot

:-D

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 15 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.