A Flash Developer Resource Site

Page 5 of 5 FirstFirst 12345
Results 81 to 97 of 97

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

  1. #81
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Enough with being nostalgic already!
    What do you have to say about my question above? Big data chunks, external files or hand written in layers?... etc....

  2. #82
    Senior Member hatu's Avatar
    Join Date
    Jan 2007
    Posts
    480
    Yeah most sites do want just one file because it's too much of an hassle otherwise. Basically this means you have to embed everything that you can.
    You should pretty much always embed everything anyway unless you need to stream in some stuff.

    If you want to have large chunks of text, you could just write classes that have static variables.
    Static variables/functions are available always without instantiating the class.

    Let's say you make a LevelData.as which looks like this

    LevelData.as
    code:

    package{
    public class LevelData{

    public static var LEVEL_1:Array = new Array(level,data,and,stuff);
    public static var LEVEL_2:Array = new Array(level,data,and,stuff);

    public function LevelData(){
    }

    }
    }




    Put it in the same folder as your main fla/as file is and just use it like:
    var levelArray:Array = LevelData.LEVEL_1;

    When you compile, all external .as files you use will be included in the .swf
    http://hatu.biz
    Portfolio & games

  3. #83
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Or host the external files yourself and just spread the swf around.

    Squize.

  4. #84
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Thanx, hatu, that looks good! I few basic questions though...
    What does the keyword "package" do? As I could find in Flash Help, a package is just classes in a folder?...
    And static.... is that for not having to create an actual instance of the class? Since it's created for the class, rather than for an instance (as I understood it from Flash Help)?...

    Would you think it's a good idea to make a 2D-array for easier access?:
    public static var LevelData: Array = new Array();
    LevelData[1]=new Array(this, that, and that);
    LevelData[2]=new Array(them, these and those);
    etc.

  5. #85
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Quote Originally Posted by Squize
    Or host the external files yourself and just spread the swf around.

    Squize.
    Ahh! And give them a level editor too, to host their own level data, and make your game engine the most popular Flash game on the planet!

  6. #86
    Senior Member hatu's Avatar
    Join Date
    Jan 2007
    Posts
    480
    Squize's idea is good too. That way you could even manage what sites can access the files.
    Of course it needs reliable hosting and when your hosting is down(it will happen) no one can play it.

    Package is pretty much just a different way to show the folder structure, in As3 all classes have to be inside packages so that's why I used just an empty one.

    Let's say you have two folders, "images" and inside it "character"
    Any .as files inside "character" should have this as their package images.character

    Static is really good when you have data that needs to be accessed from multiple places easily. Static means that they are available without making a instance of the class.

    If you need to access the leveldata only from one place then you could make them a normal class by removing the static keywords. Then you'd just need to make an instance of the class like
    var levelData:LevelData = new LevelData();

    and access any properties like
    levelData.LEVEL_1

    You should use 2D arrays because they're easier to handle. Just don't use the variable name LevelData because that's the class name already.
    The LevelData function is a constructor which is called when you create a instance of it but that's the only place you can use the class name.
    http://hatu.biz
    Portfolio & games

  7. #87
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Great, thanx, hatu!

  8. #88
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    You can make it even easier by having a function return the data:

    var level1:Array = LevelData.getLevel(1);
    Christopher Rhodes
    squarecircleco.

  9. #89
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    !!? It won't work "statically"!
    I tried this test class (for "evils", which I call my baddies)
    Code:
    class EvilLevelData {
    	public static var Level_data:Array = new Array ();
    	public static var dada:Number;
    	public static var yadda=666;
    	
    	public function EvilLevelData () {
    		Level_data[1] = new Array (1, 2, 3);
    		Level_data[2] = new Array (22, 2, 2);
    		dada=5;
    	}
    	static function get Dada() {
    		return dada;
    	}
    Trying to get these data, with different methods:
    Code:
    var LevData:Array=new Array();
    LevData=EvilLevelData.Level_data[1];
    trace(LevData);
    trace(EvilLevelData.dada);
    trace(EvilLevelData.Dada);
    trace(EvilLevelData.yadda);
    yields:
    undefined
    undefined
    undefined
    666

    Only yadda, defined before the constructor, was accessible. A getter didn't help and I also tried importing the class.

    However, if I made an instance:
    var evillevelData:EvilLevelData = new EvilLevelData();
    it all worked!:
    1,2,3
    5
    5
    666

    So, why isn't the static key word working?
    What am I forgetting here?

  10. #90
    Senior Member hatu's Avatar
    Join Date
    Jan 2007
    Posts
    480
    You're assigning the values inside the constructor which only gets called if you make an instance of the class. Except for yadda.

    Here's a couple of ways you can use them to do something like that.
    Note that static properties and methods can't access anything non-static.
    Code:
    		public static var level_data:Array = new Array(
    							new Array(1,2,3),
    							new Array(4,5,6)
    						   	);
    		
    		public static function doStuff(things:String):void{
    			level_data.push(things);
    		}
    or you could make something like a static init() function where you could just copypaste the code in the constructor and call that before accessing the values like a semi constructor
    Last edited by hatu; 09-14-2008 at 07:22 PM.
    http://hatu.biz
    Portfolio & games

  11. #91
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Allright, thanx! Three choices, then. 1) Define the data in the public declaration 2) Make a static init() function or 3) make an instance of the class.

    So, the question that remains - what is so bad about actually making an instance of the class? If I have a function for level change in my main time line, it will only be local.

    I wanted to avoid defining while declaring, since I will have at least 4 dimensions in this array, and it will look messy. (for example [x,y] coordinates -> positions -> level data type -> levels)

  12. #92
    Senior Member hatu's Avatar
    Join Date
    Jan 2007
    Posts
    480
    Nothing wrong with that. In fact if you only need most of the data in the class in one place, it's better to make an instance of the class
    http://hatu.biz
    Portfolio & games

  13. #93
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    Ok, great! All clear now! Thanx for your help!

  14. #94
    Junior Member
    Join Date
    Jun 2009
    Posts
    5
    Hi
    Been reading this post, but i found out most of it is rather old, and AS2 related, so here's how i'm working (subject to change on my part with no prior notice):

    first frame for the preloader
    everything else on second frame, where all the parts of the game are created on runtime.
    Most of the actual code would be in as3 files i import, where i define the classes for pretty much everything.
    So everything an enemy would do, for example, would be in the Enemy class, which i instantiate upon creating the specific enemy. The enemy will in turn dispatch events to interact with the rest of the world, like to notify it's attacking, or dieing.

    At least, this is what i'm trying to implement in the games i'm working on.

  15. #95
    Member
    Join Date
    Jul 2012
    Posts
    39
    I don't tend to have too many frames on the timeline. I use Actionscript 3.0 and the only code I place on the timeline is stop();. I organize everything in classes. I have a main class that creates everything with a state engine. The next most important class is the Game class, which creates and runs the game. I then have classes for the objects in the game. I also use a framework, which contains a lot of classes I extend and other ones which I use functions from.
    My first Flash game was written in Actionscript 2.0 though. It has 5 frames: The intro, the menu, the game, the game over screen and the leaderboards. I wrote things kind of like you did, a lot of main gameplay control on the timeline and the other stuff on instances.

  16. #96
    When in doubt ask Eager Beaver's Avatar
    Join Date
    Feb 2007
    Location
    Planet Earth
    Posts
    911
    Quote Originally Posted by Gogafem View Post
    My first Flash game was written in Actionscript 2.0 though....
    I code all my games in AS2 without any problem, and without any need for AS3.
    <signature removed by admin>

  17. #97
    Member
    Join Date
    Jul 2012
    Posts
    39

    Post

    Quote Originally Posted by Eager Beaver View Post
    I code all my games in AS2 without any problem, and without any need for AS3.
    It's really just personal preference. I like Actionscript 3.0 better though. Not just because of the syntax, but also because of all the game libraries available for it(Like Flixel, Citrus, Starling, Box2D) and 3D(Away3D, Flare3D). Also it's faster.

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