how to simplify this code ?

  • esse_bi
  • Novice
  • Novice
  • No Avatar
  • Joined: Sep 19, 2008
  • Posts: 15
  • Status: Offline

Post December 14th, 2008, 10:44 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 14th, 2008, 10:44 am

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post December 16th, 2008, 9:57 am

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. }
Strong with this one, the sudo is.

Post Information

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

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.