dcsimg
A Flash Developer Resource Site

Page 1 of 5 12345 LastLast
Results 1 to 20 of 97

Thread: [disc/question] How do you organize your code?

  1. #1
    FK's Resident ...... Resident flashMasterHand's Avatar
    Join Date
    Aug 2003
    Location
    Connecticut, USA
    Posts
    168

    [disc/question] How do you organize your code?

    Hey all.

    I was just wondering (like the title says), how do you organize the code in your games.

    For instance, in one of my first games (a side scrolling asteriods), I had four keyframes: the first was the title screen, second was the instuctions,credits,etc, third was the actual game, and fourth was the game over screen. All the code relating to the spaceship was placed on the spaceship, asteriod code on asteriods, etc. Nothing (except duplicating the asteriods and lasers) was placed at runtime.

    However, I am leaning more and more towards having everything coded in the frames, and everything placed at runtime. I have noticed this in other people's flas, too.

    So, how do you organize your code? Do you put everything in objects? Classes? You get the idea (I hope ) Anyway, thanks for listening to me.

    Thanks
    Dave

    [edit] 90th post!! Only ten more to go till my own avatar![/edit]
    Last edited by flashMasterHand; 07-20-2004 at 08:33 AM.
    Programmers don't die, they just GOSUB without RETURN.

  2. #2
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    Menu's etc. I like to have as frames, makes changes alot easier. As for games, Im pedantic about everything being runtime, so the game itself is an empty frame with code bringing in all the graphic elements.

  3. #3
    leight.com.au leight's Avatar
    Join Date
    Sep 2002
    Location
    australia
    Posts
    1,437
    Hey mate,

    I try to code in frames. I heard that this was a quicker way to do things, not only for the flash player, but for yourself to edit and add things quickly because everything was in a central location.

    This is how I have created my platform engine. Yes I know, I do need to get my site up and running but hey. It's coming along slowly - mainly because of school work.

    With the intro, help, game over stuff - Yes, I use frames. Or I just call the movieclip for help over the top of the others.

    Also, I hate using &&, and in if statements. I prefer to create 2 if statements:
    code:

    if (check1){
    if (check2){
    // something happens
    }
    }


    Well that's it for me. Hope it helped to answer your question.

    I also want to know which is the best way to code, the fastest for the flash player - coz in games... it's all about speed!

    Edit: Iopred, you think that I should attach all the movieclips from the library? Not have them always there? As in the: hitTest movieclip, graphics movieclip, hero?
    Last edited by leight; 07-20-2004 at 08:42 AM.

  4. #4
    Filthy Gorgeous DancingOctopus's Avatar
    Join Date
    Sep 2003
    Location
    Sunny Australia.
    Posts
    478
    Originally posted by iopred
    Menu's etc. I like to have as frames, makes changes alot easier. As for games, Im pedantic about everything being runtime, so the game itself is an empty frame with code bringing in all the graphic elements.
    I'm exactly the same.

    To expand on that, I generally organise all my code into different layers. So I'll have my player controls on one layer, map/scrolling stuff on another, collision checking, enemey routines, object handling, etc. All seperated for easier code maintenance.

    I'm still not sure if having them on different layers has a performance hit, but it's easy enough to cut-paste it all to the same layer before compiling the final release.

  5. #5
    FK's Resident ...... Resident flashMasterHand's Avatar
    Join Date
    Aug 2003
    Location
    Connecticut, USA
    Posts
    168
    Wow, quick replies.... thanks

    I know that most people like to put everything in code for games. But how do you organize the actual code. Maybe for top down, artbased (ahhh) game, would you store all the player info in a "char" object? or do you import classes? or just make a bunch of different variables, no organizational structure?

    Dave
    Programmers don't die, they just GOSUB without RETURN.

  6. #6
    Senior Member kendude's Avatar
    Join Date
    Sep 2003
    Location
    Hartford, CT USA
    Posts
    877
    Same as IOPRED, all the code for the actual game is on one frame in a layer called AS. 1 onEnterFrame on maintimeline, etc...runs all the code.

  7. #7
    leight.com.au leight's Avatar
    Join Date
    Sep 2002
    Location
    australia
    Posts
    1,437
    Probably not very good of me, but I don't use classes or objects. I just use different variables and functions which do the trick.

    PS: check out the file attached. That is the way I organise my code.
    Attached Files Attached Files

  8. #8
    flash mangler drunkenmaster2000's Avatar
    Join Date
    Apr 2001
    Location
    England
    Posts
    156
    I started my game about 18 months ago, and my knowledge of actionscript wasn't even close to what it is now (now it's average..), so my game is currently migrating from nightmare to mostly runtime.

    All graphics are attached from the library. they are then given onEnterFrame functions through the code.

    Each object (char/platform/enemies/picksups) has it's own variables, and the onEnterFrames are added at runtime (not via functions yet, unfortunately)

    the menu/options system is largely frame-based, due to the (probably) poor way I wrote it. Luckily, it runs fine, because I don't feel like rewriting that.

    All the new things I've been adding (credits/between-level page/etc.) have been prototypes of one sort or another, and everytime I want to update a routine, I'll try to tear it out and make a function out of it.

    the character animations are done in the old-fashioned way.

    Most of the game just needs a tidy up, I was in the habit of splitting the code across several frames to make it more managable, so now I have to dive in, and clean that lot up too.

    whether I'll actually achieve one frame-total runtime, I don't yet know. I know it's possible, but I don't know whether I have the patience to re-code something that seems to work perfectly well.

    one frame-total runtime is my goal for the next game I do though.

  9. #9
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    leight, do you get a percentage every time someone gets a copy of your scroller ?

    I'm the exact opposite of pred. Lots of layers, externally loaded scripts ( A new habit which I've picked up from nGFX ) and over 70% of everything is author time content.
    The only thing we do similar is that all my attract mode stuff ( Title screen, game over, hi-score input etc. ) is spread over multiple frames, and the game itself is over 2 frames ( Not counting the pre-loader, which is out of the way in it's own scene ).

    I still put the player code on the actual player mc, which is kinda lame I know, but I find it's a really nice way to code. Same with the baddie handler.

    oop only when it's worth doing. Mainly for the baddies in a game, otherwise I think it's more effort than it's worth.

    local vars where ever possible, and heavy use of functions and function pointers.

    Squize.

  10. #10
    Senior Member kendude's Avatar
    Join Date
    Sep 2003
    Location
    Hartford, CT USA
    Posts
    877
    Squize, 2 frames for the game? Tell us more about how that works. Is that instead of an onEnterFrame, just run all the code in 1 frame, goto the next and say "gotoAndPlay(1);" in the subsequent frame? I know you got a great reason why, maybe we could all learn from it?

  11. #11
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    Just habit mainly.

    The first frame has everything defined on it, prototypes / functions / classes etc and then the 2nd frame is just the actions layer with:

    init();
    attractMode();
    stop();

    and that's it. It just my way of making sure everything is initialised for when I start running things.
    It's prolly not needed, but another one of my quirky little habits.

    Squize.

  12. #12
    Half Empty happinessSam's Avatar
    Join Date
    May 2003
    Location
    London
    Posts
    442
    My last few games were all just 1 frame jobs, not including any preloaders. There might be some menu/gui stuff already on the 1st frame which I change the _visible for when I need or don't need it, but everything else is done in runtime. Everything is written in .as files, which I include on the 1st frame, and I generally have loads of different files so I can easily find what I'm looking for.

  13. #13
    Senior Member kendude's Avatar
    Join Date
    Sep 2003
    Location
    Hartford, CT USA
    Posts
    877
    I see you mention "attractMode();". Talk to us about that. Is that where you call a cut-scene, or something similar during game play, or is it the opening scene?

  14. #14
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    Ken, you make it sound like therapy.

    "Now talk to us about attract modes, and this desire to dress up as a woman".



    Attract mode is just the traditional arcade term for the title sceen, in that it's job was to attract players to put their money in and play.
    So you'd get the titles / company credits and then a rolling demo and then the hi-scores.
    All the trilogy games had attract modes cause I really wanted that arcade feel and to distance them from Flash games.

    I still call the function that, even if it's only something simple like a logo with some buttones ( ie UXB Kid, JBJ etc ).

    I just make a mc and slap all the different frames in that. When the game is running I just park the mc at the first frame, which is always empty.
    Fairly simple way to do it, and your main timeline isn't full of stuff that isn't really needed in your game.

    I don't know why, but I'm really thinking about open sourcing UXB Kid when it's done, albiet in a really cut down version.
    I think the main thing holding me back is seeing all the God awful messes people make of open source game engines.

    Squize.

  15. #15
    Senior Member kendude's Avatar
    Join Date
    Sep 2003
    Location
    Hartford, CT USA
    Posts
    877
    With all our back and forth, we should make the transition to badminton or something.

    Thanks for enlightening us with some more tips and tricks.

    In regards to UXB, I would only encourage you to demonstrate the collision detection using your screen quadrants/math method, I am certainly not interested in seeing "Super Albert's Whatever Kind Of Game" or anything else of that nature..

    For those of us just joining in, we've all seen skinned games built upon an open source model, usually followed up with "Mommy, look what I did!" And they often tend to forget the original author in the credits, wait...what credits?

    *edit:spelling.
    Last edited by kendude; 07-20-2004 at 11:26 AM.

  16. #16
    Senior Member
    Join Date
    Jul 2004
    Posts
    153
    hey,
    we've been staying away from "proper" code-in-seperate-files because.. it seems kind of silly to work that way when the IDE doesn't really support it

    we use "quake-style" programming -- not totally oop but very clearly defined objects and functions which work on the objects. code for each object/module is on it's own layer, sometimes split into several layers if it gets longer than ~1000 lines; we put the most general stuff in the top layer and break the rest into meaningful sections.

    then we group layers into categories -- for instance, all the game entities would be in a single category -- and stick them all in a "folder" (or whatever you call those breifcase things that let you expand/collapse groups of layers).

    this might seem a bit over-structured, but it's really great to work with since you can collapse everything but the area you're working on so you don't need to scroll up/down forever to find stuff.

    in terms of program flow, the lowest two layers are:
    INIT
    MAIN LOOP

    init just builds everything (using all the layers above it, which contain all of the game code) while main is just the typical Main() loop.

    i'd link to some of our source so you can see an example, but our host got DOSd last night so it's down ;l

    raigan

  17. #17
    Member
    Join Date
    May 2004
    Posts
    50
    I lay mine out pretty much how metanet does. All the code goes on the first frame and is separated into layers to organize the code. Then I'll use folders to organize those layers together. Like I'll have a folder with each individual enemy layer inside of it. I'm crossing my fingers hoping that Macromedia will give the IDE some of the improvements it needs so badly (shortcuts to functions!?!?).

  18. #18
    Untitled-2.fla
    Join Date
    Jul 2002
    Posts
    391
    In my current game template I have 300 frames, not all used, I space the sections out every 20 or so frames. the sections go as follows (frame labels):

    *preload *logo *error *splash *menu *controls *options *instructions *highscores *gameInit *game

    *gameInit is 5 frames long and holds all the code that is needed in the actual game
    *gameInit frames 1 and 2 contain globals, prototypes, here I place all movieclips that will be attached at some point during the game.
    *gameInit frame 3 contains all variables used in building the game, this may be tilesize, worldwidth / height, buildspeed (used for eye cany in the games); things that will directly change the gameplay
    *gameInit frame 4 contains level information
    *gameInit frame 5 containg all the code functions used in the game, these may be seperated into sections and stored as seperate .as files (to make finding and changing code easier), depending on the number of lines.

    the game frame simply contianes two lines used to set the ball rolling

    stop ();
    newGame (0);

    any other code used in the game is generally imported as packages (classes), if possibl I make it a point never to directly attach scripts to buttons and movieclips. all code is put onto frames > myButton.onPress=function(){}. and where possible; I try to keep code on the main timeline (apart from the obvious stop() commands)

    A game I'm currently re-writing has code on two frames, and 1 class. that's all (and the gameplay is complete)

    Having said all that, go with what works best for you!
    always comment your code : )

  19. #19
    FK's Resident ...... Resident flashMasterHand's Avatar
    Join Date
    Aug 2003
    Location
    Connecticut, USA
    Posts
    168
    metanet: Thanks a lot. I tend to try that, but sometimes I over-structure things, and I just end up bogged down in it all.

    squize: How do you handle cutscenes? I have played the UXB beta on your blog, and I loved the smooth transition between levels. Do you have seperate loops for the levels in general, and the jump out of those and start a cutscene loop (or mc animation, whatever) Sorry if I am being a little vauge. I am beginning to start larger game projects (that of course never go anywhere ) and half the time I get stuck on how to organize my code. I know that seems kinda lame, but these larger projects can mean more code, and I just dont know where to start sometimes. btw, I love the classic arcade feel of UXB

    Dave

    edit: forgot my domo!!
    Programmers don't die, they just GOSUB without RETURN.

  20. #20
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    Thanks for the kind words mate, I'm glad you like it in it's pretty alpha state.

    I guess you mean the stats screens between levels and the [ Stolen from Rainbow Islands ] map between islands ?

    These are just in the attractMode movieclip. I space that out like token3 does, I use frame labels in there and stuff.
    I know that sounds painfully newbish, but it's a handy feature and silly not to use. Plus all the options and stuff are forever being messed around with, so I don't want fixed frame numbers.

    Because I use layers and author time content, I just make sure the attractMode mc is in the highest layer, so when need be, I just set it's _visible=true and gotoAndPlay the correct frame.
    On that frame it'll fade up an image, thereby covering all the other stuff on the screen.

    It's as easy as me with a drink inside me basically.

    After all that, I hope that's what you meant

    Squize.

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