how to simplify this code ?

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

Post 3+ Months Ago

Hi,
I have ten buttons (btn1,btn2,btn3...) each one loads a jpg inside a mc
in this way:

Code: [ Select ]
btn1.onRelease = function(){
             loadMovie("myPictures/1.jpg",_root.container1)
}
btn2.onRelease = function(){
             loadMovie("myPictures/2.jpg",_root.container2)
}
btn3.onRelease = function(){
             loadMovie("myPictures/3.jpg",_root.container3)
}
.....and so on.......
  1. btn1.onRelease = function(){
  2.              loadMovie("myPictures/1.jpg",_root.container1)
  3. }
  4. btn2.onRelease = function(){
  5.              loadMovie("myPictures/2.jpg",_root.container2)
  6. }
  7. btn3.onRelease = function(){
  8.              loadMovie("myPictures/3.jpg",_root.container3)
  9. }
  10. .....and so on.......


How can I write all this stuff in a single onRelease function (so if my buttons become 30 I won't have to write again and again, but only change
a value)
in a way like:
Code: [ Select ]
btn[i].onRelease = function(){
            loadMovie("myPictures/"+i+".jpg",_root.container[i])
}
  1. btn[i].onRelease = function(){
  2.             loadMovie("myPictures/"+i+".jpg",_root.container[i])
  3. }


I know this is not the right syntax, I've tried in several ways, but I'm not able to solve this problem

thx a lot
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Instead of "btn[i]", you need to construct the dynamic variable name within the containers namespace.

Something like this, where "container" needs to be the Movieclip containing btn1, btn2, etc.
If all of the buttons are in the _root timeline, container[] would be _root[].

Code: [ Select ]
for(i=0; i<n; i++)
{
   container['btn' + i].onRelease = my_func;
}
  1. for(i=0; i<n; i++)
  2. {
  3.    container['btn' + i].onRelease = my_func;
  4. }

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 14 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.