Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
This chapter is from the book

Using Functions to Make Efficient Code

Functions are self-contained bits of code that are executed or "called" when you need to use them. The best way to describe it is to give you an example. Say, for example, you have a button that, when it's clicked, will increase the scale of myClip in the X and Y axes. You could write the following code:

  on (release) {
  myClip._xscale = myClip._xscale + 10;
  myClip._yscale = myClip._yscale + 10;
  }

This would, of course, work fine. But what if you needed to do this on several buttons? Writing the same two lines would be a pain. Instead, you can define a function in the first frame of the main timeline as follows:

  function increaseScale() {
  myClip._xscale = myClip._xscale + 10;
  myClip._yscale = myClip._yscale + 10;
  };

Now whenever you want to increase the scale of the movie clip with the button, you can simply write the following code:

on (release) {
  _root.increaseScale()
  }

This will have exactly the same effect, but it's much more efficient. It also means that if you wanted to amend the code—to decrease the alpha as well as the scale, for example—you could just add it to the function. Before I would have had to go into every single button and alter each piece of code.

  • + Share This
  • 🔖 Save To Your Account