PDA

View Full Version : Too much code!



Petwoip
August 23rd, 2009, 01:35 PM
Whenever I make something with AS3 I find myself writing SO much code. Unfortunately, I can't figure out how to organize it in a clean and efficient way. Most of my problems occur when making a menu system. In a game I worked on, the menu had a level select screen, a level editor, and the regular playing screen. In the end, I had about 2,000 lines of code, with tons of functions and even more event listeners.

Are there any good programming practices that will help my projects become less cluttered and more organized?

453.0
August 23rd, 2009, 01:36 PM
Learn OOP. ;)

Petwoip
August 23rd, 2009, 01:48 PM
I know OOP. I made sure to use it as much as possible with the actual game mechanics. However, for something like a menu, not everything is quite so clear-cut.

FizixMan
August 23rd, 2009, 02:07 PM
There's nothing you can really do about reducing code. Sure some implementations and the skill of the programmer contribute to code length and elegance, but some things (especially user interface components) are just complicated by nature and take a lot of code.

You should not be concerned about the amount of code, but rather its quality. If you can write your menu using a single class in 2,000 lines, or using 5 classes and 2,500 lines then you're probably better off with the 5 class option. Ultimately, you want code that is easy to understand, easy to maintain, and easy to use.

Beyond that, for programming games, I suggest you write them in such a way to disconnect the UI from the game engine. You might even be able to write generic UI components or logic wrappers that you can reuse in multiple games.

Petwoip
August 23rd, 2009, 02:23 PM
Thanks for those tips

dandylion13
August 23rd, 2009, 02:32 PM
I know OOP. I made sure to use it as much as possible with the actual game mechanics. However, for something like a menu, not everything is quite so clear-cut.

The strategy is to pack as much code as you can under-the-hood in classes, so that your client class (like the document class) is simple and clutter-free. If you do this right, the only menu code you'd need to deal with might look like this:

_menu.createMenu("Item One", "Item Two", "Item Three");

Yes, there will be a lot of code working behind the scenes, but it will be quietly working away for you in the background and you won't have to deal with it directly.

453.0
August 23rd, 2009, 03:32 PM
If you say you know OOP, then you should step it up and learn some Design Patterns. As FizixMan already pointed out, you should separate the view from the logic that is behind the app ( and not only... You can separate a lot more and even end up reusing some elements ).

You should especially read up on the MVC meta-pattern that helps you do this exact decoupling of the view, data and "logic". There are many more patterns that can help you out in certain tasks too, but for a start, trying to understand and "master" the MVC meta-pattern would be great ( 2000-2500 lines of code do happen... you just take a look at the Object class or most of the core AS 3.0 classes... the only difference is, that those classes really need all that stuff to be there - I'm quite sure that your game is not of such a complexity so it surely could have been broken down to smaller pieces with basic use of OOP ).

Good luck.