Home arrow Flash arrow Page 4 - Using Function: The Beginner's Nightmare
FLASH

Using Function: The Beginner's Nightmare


Going past the basic stages of programming for any language usually involves the idea of modularizing code, especially when dealing with opbject oriented programming languages. ActionScript is no exception, and today we'll be getting started with functions, with a few samples to boot.

Author Info:
By: Aleksandar Horvat
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
October 27, 2004
TABLE OF CONTENTS:
  1. · Using Function: The Beginner's Nightmare
  2. · Function statement
  3. · Do you like functions?
  4. · More on reusability: practical example
  5. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using Function: The Beginner's Nightmare - More on reusability: practical example
(Page 4 of 5 )

To further encourage you not to give up on your way in becoming advanced actionscript programmer, here are some practical situations that should give you an idea of what benefits you could count on when you decide to move on from gotoAndStop() to some serious work.

Example:

Let’s say that you’ve built a Flash application that uses sound effects which will be repeated multiple times through your code. This is fine and it probably works fine. Then, your client requests an option to be able to switch sounds and sound effects on and off. That gets you lots of trouble and tons of additional code for every sound.

What about using a sound handling function?

Place all sounds in separate movie clips, first frame is empty and on stop(), second one is containing your sound. Set instance names to them regarding sounds they contain and then declare a function on main timeline with the parameter that will call the sound movie clip.

function makeSound(sound){
_root[sound].gotoAndPlay(2);
}

Now, instead of doing different actions with sounds you could simply invoke this function with the sound name as a parameter from wherever in your entire application:

_root.makeSound("bird");

In the same situation when you need to control overall sound appearance in your movie, you could just alter your function with a condition that checks if sound is allowed or not.

First, visualize your sound on/off in a toggle button or any other aspect that could give user interaction ability. Set it up so it changes the variable on the main timeline, which would be used as a kind of flag. Initial setup is true which means the sound is on.

allowSounds= true

Using your option you must be able to toggle this variable to true or false. Then just alter your makeSound function:

function makeSound(sound){
if (_root.allowSounds==true){
_root[sound].gotoAndPlay(2);
}
}

It is obvious that all sounds will be played only when your flag is set to true, otherwise, function will be called without doing any actions.

Can you imagine what numerous solutions can you do with this basic approach? Could you see more solutions to your everyday beginners actionscripting? Once you feel the power of code optimization, you will be able to do increasingly satisfying high-end solutions. However, not to forget, you better start with functions first.


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials