Results 1 to 4 of 4
Thread: Split big functions?
-
March 2nd, 2011, 11:52 AM #134Registered User
postsSplit big functions?
Is there a limit for how many statements I should put in the same function? I have a function that takes care of all of the code for a movieclip and it's up in 16 "if statements" allready, should i split it up in more functions or not? I'm a novice, and never coded anything big so it might not even be that many, i don't know
-
March 2nd, 2011, 12:19 PM #2651Registered abUser
postscould probably have hundreds dare I say thousands...not exactly manageable though.
there is usually a better way to handle things if its getting more complex than a few if statementsblog (experiments and code)-> http://blog.bwhiting.co.uk/ <-
as3 helper/shortcut lib (v1.2) -> http://code.google.com/p/bwhiting/downloads/list <-
agal helper -> Clicky here <- UPDATED
-
March 2nd, 2011, 12:21 PM #3
It's not much, unless the Class with this function is instantiated thousand of times (e.g. it's a particle). Then You could consider dividing the function to parts executed in different frames, or optimizing the code.
You should care more about loops, like for (var i:int = 0; i<10000; i++) { doSomething() }(I wish to develop my English, feel invited to correct me.)
-
March 2nd, 2011, 12:41 PM #434Registered User
postsThank you for the replays

the function is instantiated twice so i guess it should be ok. You are right, i should learn to use loops, though i don't think it would be useful for this particular thing. I'm making a game similar to bomberman, and that function handles the controls for both players.
It's a tilebased game that works like this ( not sure if it's the best solution, but it's what i could work out, appreciate feedback and tips
)
When you hit a directional key a walk animation will play that moves the player a certain distance in that direction. Afterwards the player is reset to the "idle" animation (moved back to it's original position) and pixles are added to the x/y position so it looks like you walked a tile and then stopped. Theres a if statement for each directional key, and one for resetting the player to the idle stance when he reaches a certain frame. ( 8 statements * 2 players)
If there is any way to optimize this that you want to share with me i would be very grateful

Reply With Quote


Bookmarks