However, I don't feel like I adequately explained what was going on before. You see, I need to fill up content on this blog to get into an A-Level course and i really need to spam as many posts as possible. Leaving out information to be filled in later is a great way of accomplishing this, and it's how some companies and religions (*cough* Scientology *cough*) make a lot of money.
Let's say that you have loads of buttons. When you mouse over them, you want them to all do pretty much the same thing. However, writing the code for each button would be non-efficient. Therefore you'd construct a table of all the buttons, and in that loop write the function for the iterated button in question. In the functions you'd write code that applies to all buttons when moused over or whatever.
This can leave you with a lot of flexibility, as if you make the code as ambiguous as possible you can make each button seem more different and customised than it actually is. You can check out what I've done similarly below. As you can see, the loop runs through a series of buttons, and sets up a function for all of them. This way, if I want to do something for multiple buttons I only need to write the one function and be done with it. I can edit it later, change it, etc, but I only have to do it the once.
For looping to clone functions efficiently for multiple buttons.
A limitation with this method, however, if that this efficiency only goes as far as doing the same thing for the buttons you choose. If a button needs its own special thing (like sweeping in a frame or whatever once clicked) you'd have to do that separately, as you wouldn't want all buttons to do that.
A similar idea would be to write out the code like I've done, but add a function at the bottom wherein it checks a table. If in the table there is a value that has a key matching the name of the button it'll run the value as a function. I could write a table of functions and name them after the key, and this for loop would run through them individually.
All of these skills can be used in a variety of applications.

No comments:
Post a Comment