|
-
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.
-
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.x = xPos; varName.y = 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
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|