ForumsProgramming ForumGood Programming Technique 101

82 105675
LonLonRanch
offline
LonLonRanch
172 posts
Nomad

Hey Guys, I'd say I'm still fairly new to the forums but I'm checking more and more often, and one thing I have noticed is that the Programming Help section isn't very active. So I thought I would add my 2 cents and share some of my AS programming experience.

I've been programming actionscript for about 3-4 years now and have even been through 2 ArmorGames challenges. I've been asked by a lot of people for some coding help over my time and, well, I see a lot of times that I code quite a bit differently then they do, and a lot of that is because they are:
1.Still new to Actionscript
2.Have learned only from online tutorials
3.Are used to coding with what works instead of what works best.

So, what I decided to do here is to share with you what I have learned over the years to be Good Programming Technique. So before I start this first one off take note:
This is being coded is Actionscript 2.0 (I still have to fully embrace AS3 although I highly encourage learning it)
Also, this is what I have personally learned from my experience, you may disagree, that is fine, actually if you think I'm wrong let me know so that we can all learn because there is no reason to code ONE way.

Ok so after that intro this is what I have to share with you.

Coding on the Main Timeline

The more complex your game gets, you're going to have more MovieClips, Layers, Frames, lines of code, etc. And with that, it will be harder to organize everything. Which is why it is always good technique to code on the main (or _root) timeline. Now some of you may not even know it, but you can actually control every aspect of your game by coding into the the first frame (not movieClip, frame) of your game. For example, on my latest game I had right around 2000 lines of code on the very first frame.

2000 lines on the same frame!?

Yes. You may be thinking right now, How? Well, there are 2 very very important coding methods I use when I program and they both make use of that frame. The first one, which I'm going to explain on this post is calling movieClip's (onEnterFrame) method from the main timeline instead of on the instance of the movieClip itself. I'm sure you are very used to coding with:
onClipEvent(enterFrame) -What I'm telling you is that I don't do that.

You can do onClipEvent(enterFrame){ outside the movieClip?

Yes, yes you can. And here is how:

on the main timeline for a movieClip with the instance of myClip you would have something like:

myClip.onEnterFrame = function():Void{
Which would be the same doing a onClipEvent(enterFrame){ on the myClip movieClip, you just have to make sure you have the instance name (in Properties) set to 'myClip' and that it is on the main timeline.

-If it is not in the main timeline, lets say its in a movieClip called 'otherClip' you simply have to add a bit of code:
otherClip.myClip.onEnterFrame = function():Void{

Umm ok, but I still don't see why you do this...

Fine fine, I understand this might be a little confusing at first, but in time I'm sure it will make sense, it might take a little trial and error. The reason I say it is good technique is that having all of your code on the main timeline makes it very easy to keep your code organized and easy to locate. If you have several movieClips/Frames/Layers in your game that you want to apply code to, it going to get confusing. Where did I put that clip? Where is the code that deals with the hitTest? What frame is it on? etc, when you code in one place you don't lose, forget, or repeat code. Its all in the same place so it is easy to work with, and if you ever code with with someone else it will make their lives a lot easier because they won't be looking all over for code they don't even know exists.

Alright, so just a few notes to end on: (gosh this post long, sorry)

It is always a good idea to code on the main timeline.
You can save lots of lines of code and better organize your code by calling .onEnterFrame functions (functions are a very very big part of coding as well, if you don't know about them I'm sure I'll make a topic on it someday)
Even if you code AS3 you can still practice this.

Let me know if something doesn't make sense, I'm sure it may be quite confusing.

  • 82 Replies
Klaushouse
offline
Klaushouse
2,770 posts
Nomad

Very good job. Could be expanded. If you expand it we could add this and my game making thread together and make a super sticky thread. Talk to me if you're interested! XD

LonLonRanch
offline
LonLonRanch
172 posts
Nomad

I do plan on expanding this, if the people demand it, I just thought the post was already incredibly long so I'll save more for another post. We cooouuuulllddd make a super sticky thread, or since that is a lot of text we could just provide links to a main sticky

Klaushouse
offline
Klaushouse
2,770 posts
Nomad

People don't always show it but they love this stuff. And is crucial in starting to make flash games(my thread). So totally expand it. We could rule the Programming forums, you and I... bwahahaha!

Programpro
offline
Programpro
562 posts
Nomad

Nice guide, you make good points. I am working on a game, and it was getting a little hectic with code all over the place. Luckily, I haven't done much yet, so I think I'll take your advice : )

Thanks!

LonLonRanch
offline
LonLonRanch
172 posts
Nomad

Ok then, hopefully this has been helpful. I decided that I might as well expand this a little bit and since this doesn't have that many replies I might as well just add on to this one so Here We Go!

Wheeeeeeee!

Ok so what we learned from up above was that it is considered good programming technique to keep a the majority of your code on the main timeline (the first frame is always a good place). Ok so what I want to talk about now takes off from our last little segment of code:

myClip.onEnterFrame = function():Void{

-If you look here you see we are referencing the onEnterFrame method as a function; Functions, are great. If you do not use functions on a daily programming basis then listen up.

Check out Adobe's Live Docs for full documentation of Functions()

Ok so what is so great about functions?

Well, I'm glad I asked... Functions are a very useful way to program because they allow you to reuse a segment of code. A function holds a section of code that you can use over and over again without having to re-write your code.

For example:
Lets say you have a game, and everytime you press a button you want to move 3 different movieClips 3 different directions. The code would look something like this:

myButton.onRelease = function(){
mc1._x += 5;
mc2._x -= 10;
mc3._y += 5;
}


This would work just fine, but lets say you want this to happen many times, or want this to happen, say, when another event happens, you would have to copy and paste that code each time you used it, that is where we use a function:

moveMC = function():Void{
mc1._x += 5;
mc2._x += 10;
mc3._y += 5;
}


now you could call this event like so:

myButton.onRelease = function(){
moveMC();
}


And that would do the same thing. So as you can see, functions are very easy to use, and there are even more uses for a function, one thing that is very useful about functions is the ability to add parameters to them. Lets say you want to do the moveMC(); function but you want to have control over the _x and _y values your movieClips move. This is what you do:

moveMC = function(value1:Number, value2:Number, value3:Number):Void{
mc1._x += value1;
mc2._x += value2;
mc3._y += value3;
}


(The ':Number' is not required but helps to classify what the parameter should be)

Then you can do this:

myButton.onRelease = function(){
moveMC(5, 10, 5);
}


If you did this, it would do the same thing as before, but you can now decide the values you want to pass into your movieClips, if you wanted them to move larger distances you would do something like:

moveMC(20, 20, 20);

Ok so I think I know how to do this but I don't really know when or where I should use a funciton.

Ok yes, I admit the example here isn't perfect, but I didn't want to make this post too complex (or too long for that matter) I use a function anywhere I would normally reuse a section of code (my functions usually consist of several lines of code)

-For example lets say you have a game and every time your character gets hit by an enemy, touches lava, or gets hit by a car - you want him to take damage. And lets say that when you take damage you need to do several things such as lose 10 hp, lose a heart, lose 10 points, and blink red.
Now instead of writing all that code, and then rewriting it every time your character gets hit by an enemy, touches lava, or gets hit by a car, you could instead just write that code in a function and then call the function instead. This is good practice because it not only keeps your code clean, but also saves space and file size by reducing the lines of code, and it also makes it easy to update the function.
For example, if you have that code inside a funciton you could just change a line of code (in the function) rather than having to go back to all the different places you used that code and update the code everywhere you placed it before.

Hope that makes sense, feel free to comment or ask questions if you're confused.

im_tha_man
offline
im_tha_man
394 posts
Nomad

Coding confuses me very much, I do not understand how the commands and arguments work, yes I am new to coding and I understand I should feel stupid because I cannot do anything on it at all accept make movie clips, I am a good artist and I can make objects look like they are in motion, I don't think I have walking down so much but anyways coding is very difficult and I think I am never going to learn from online tutorials if I have no idea what an argument is or something and the key functions I understand somewhat but I am used to Game Maker coding where it is like When key is pressed (left) move in direction at speed yata yata and so on but with actionscript there is even more code that you can add than there is on Game Maker, I knew I was in for a challenge when I downloaded flash and one of these times I am going to have to face it head on and figure it out. Within the next 30 days before my trial runs out haha but anyways I am extremely confused and hope to figure it out, but I am definitely not the greatest programmer yet. -.-

Programpro
offline
Programpro
562 posts
Nomad

I remember when I learned, the hardest thing was figuring out what you can do with a word and what you can't.

Ok, actionscript is like what you mentioned, im_tha_man, just for complicatedly worded.

when key is pressed (left)

move left


if (Key.isPressed(Key.LEFT)) {

this._x -= 5;

}

The same, in two different languages.

Before I ramble, do you have actionscript 2.0 or 3?

dank
offline
dank
983 posts
Peasant

Basically what you're saying is:
"Actionscript is an OOP language, so use it like one."

For some reason, Flash programmers just don't seem to grasp the fact that AS is OOP. They always want to do things on the stage instead of programming modularly.

AS3 is 100% proper OOP language which is why it is so efficient and easy to use.

MikeRLee
offline
MikeRLee
10 posts
Nomad

More on modular programming...
When you create any kind of program, you should start by splitting it down - in head or on paper, whatever works for you. Then tackle them one at a time by splitting them downn into subproblems again and again. This is where functions come in. Each subproblem should have its function. For example,
Running the AI is one of the &quotroblems" in a game so we would have a function UpdateAI().
When we update the AI, however, there are many things to consider - wether things are in contact with each other, if they are in each other's line of sight etc. - before anything is even done to the characters. So the UpdateAI() is split further into other functions.
CheckForOverlap()
InLineOfSight()
etc.
So this will make you coding much more legible and you have the added bonus of being able to copy these functions into other programs to reuse like the CheckForOverlap() function stated above can be used in many programs (if coded correctly).

I, personally, am not a AS programmer as I prefer the world of void pointers and pointers to void pointers

Basically what you're saying is:
"Actionscript is an OOP language, so use it like one."


The OO paradigm does not concern itself with splitting your code up into functions but splits your code up into classes. It is the Procedural Programming paradigm that deals with splitting it all up into functions.
I will also disagree on saying that AS is purely OO. Just looking at some source code and you can see that it is also largely event-driven. OO does not necessarily mean efficiency and ease of use. All programs are compiled into machine language - the code relies only the compiler, the machine its running on the programmers knowledge to run quickly and smoothly. OO isn't the answer to everything and you should always be open to other paradigms when it is needed.
I will agree that if you are making games - make it object orientated. Create a class for the character, create a enemy class, world class, weapon class, inventory class etc. as it makes it so much easier when you want to go back and make a sequel to that awesome game you wrote.
MikeRLee
offline
MikeRLee
10 posts
Nomad

If I could I would edit that post as one of the paragraphs made no sense so here it is in English.

I will also disagree on saying that AS is purely OO. Just looking at some source code and you can see that it is also largely event-driven. OOP does not necessarily mean efficiency and ease of use. All programs are compiled into machine language - the code relies only on the compiler, the machine its running on and the programmers skill, to run quickly and smoothly. OOP isn't the answer to everything and you should always be open to other paradigms when it is needed.

LonLonRanch
offline
LonLonRanch
172 posts
Nomad

That's a good point Mike, I too wouldn't necessarily say that Actionscript is completely OOP, although (expecially with AS 3.0) it is headed in the right direction. And yes, OOP may not be best always, but it certainly has had history of good result in terms of good programming technique.

MikeRLee
offline
MikeRLee
10 posts
Nomad

In certain areas OOP has had a good history, mainly because it has become a craze which has pushed a lot of development into it. While this may be good for Flash, which I assume is prodominately used for animation and games, it is not so good for others. Functional, logical, procedural, generic and metaprogramming are still very important (procedural is a necessity even with OOP currently).
In fact, I just thought of a very good thought about OOP. It is almost dependant on procedural programming. A class isn't a class without methods, its a structure.
For a language to become completely object-orientated, everything from If statements to boolean operations need to be encapsulated in classes but you can see how ridiculous this is. Basically, even if AS ever becomes COMPLETELY object-orientated, it will have shot itself in the foot as it will have become tedious to create a new IfManager object and then ask it to run this method in this class if one value is {insert operation} than the other. This highlights that OOP will not nor ever work efficiently on its own.
After all that though, I agree it can bring out good results but I believe that that is also due to the amount of education that is spent on it. It is drilled into new programmers now to use it constantly and, as you will know, there are awful OO programs out there that could do with a bit of, lets say maintenance.
That was a bit of a ramble but what I mean to say is, and this is good advice so if you read anything, read this.
Find a paradigm that works for you. If concrete factory design patterns work for you, use them. If you prefer to split your programs into functions that each do a specific process, do that. Don't use a coding practise that does not work for you, otherwise your programs wont either. An application will only ever be as good - or as bad - as its programmer.

gameingkid
offline
gameingkid
156 posts
Peasant

right now i have gamemake 7.0 i think but it wont let me post my game on AG so i need to know what program to use to post games on AG and if its free or how much it costs so i know to start saving.

haYnguy
offline
haYnguy
30 posts
Nomad

If AS was purely OO, then you would NOT be able to directly modify the _x member of an object ( myObject._x = 34 )because it would violate encapsulation. Thus, AS is not purely OO...

Q.E.D :P

walkerm930
offline
walkerm930
4 posts
Nomad

hey gamingkid

if you want to make a game that you can upload to AG you gotta have make flash games using adobe flash. a 30 (i think) day trial can be found here-[url=https://www.adobe.com/cfusion/tdrc/index.cfm?product=flash&ampromoid=EBYEX&loc=en]

Showing 1-15 of 82