Flash
  Home arrow Flash arrow Page 4 - Using Function: The Beginner's Nightmare
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Using Function: The Beginner's Nightmare
By: Aleksandar Horvat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2004-10-27

    Table of Contents:
  • Using Function: The Beginner's Nightmare
  • Function statement
  • Do you like functions?
  • More on reusability: practical example
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

    More Flash Articles
    More By Aleksandar Horvat


       · This is the most basic of introductions. S/he could have been more instructive if...
     

    FLASH ARTICLES

    - Decorator Pattern
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT