A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: When to use functions?

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    391

    When to use functions?

    Is using custom functions a preference? It's possible to make a game work with everything happening inside event listeners but I want to know how often should I use functions. Like in a game of Tic Tac Toe, if I click on a square it will change color, the turn variable will change, check if there are any winners or anymore moves. For all that I just put all the results in that Mouse_CLICK event listener. I don't know when I should use custom functions.

  2. #2
    Member
    Join Date
    Mar 2009
    Posts
    62
    use custom function when you are repeating a lot of code. If you notice you are maybe drawing a line, or attaching several textboxes, create a function that means you only have to enter a line of code every time you want to draw that line or attach that textbox.

    For instance

    PHP Code:
    function createTextField(varName:TextField,varWidth:Number,xPos:Number,yPos:Number,wWrap:Boolean){
                
    varName.width=varWidth;
                
    varName.xPos;
                
    varName.yPos;
                
    varName.wordWrap=wWrap;
                
    trace(varName)
                
    addChild(varName); 
    then each time you want to attach a textbox its a case of

    PHP Code:
    createTextField(output5_txt,170,330,269,true); 
    which makes it look tidy and it's less typing for you

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    It's worth noting that eventHandlers are functions. Essentially you'd want to use a function for any code that is deferred (eg. not run when you hit a frame on a timeline or when your document class is initialized) or any code that you want to run more than once.
    Please use [php] or [code] tags, and mark your threads resolved 8)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center