ForumsProgramming ForumGood Programming Technique 101

82 105680
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
NightSh4d0w1
offline
NightSh4d0w1
1 posts
Nomad

Trust me Flash takes over your life but it is so much fun to create your own games and animations also leaves you with somewhat a sence of pride in yourself! Also you always know there is something new you can learn and never know what is around the corner! Forum actionscript is mostly the best due to people giving HONEST scripting most of the time and can tell them if it don't work.

Darkroot
offline
Darkroot
2,763 posts
Peasant

flash program to start doing it myself.... got an artist, and a musician...guess i've gotta be a programmer haha. Thanks guys, this really REALLY does help.


Don't forget to practice use flash develop to hone your actionscript skills for the future in which you might get flash and make a game .
Ongokiller50
offline
Ongokiller50
12 posts
Nomad

To make a Combing Game

Fighting Frames after the attack Make a var saying something like _parent.comboMove = 1 then on the movieclip put
Punch = 65// A key
if (comboMove == 1){
if (Key.isDown(Punch)){
// Making sure its on the attacking animations
gotoAndStop("fightMoves"

//The MoveClip To go to the next attack combo//
Fighter.gotoAndPlay("attack2"

//Canceling The code because you already press it. No need to make it repeat it//
combo = 0
}
}

Stranger087
offline
Stranger087
2 posts
Shepherd

enterFrame is not good...

everybody should use setInterval(function name, 1000/fps,params)

like this:



i=0;
del = 1000/10;
inid = setInterval(this.game, del);
function game() {
i++;
trace(i);
}
this.onMouseDown = function () {
clearInterval(inid); /*because you can set a lot of intervals for a single function*/
del/=2;
inid = setInterval(this.game, del); // gamespeed doubled
}
now u can make dynamic gamespeed, like in gemcraft xD

Stranger087
offline
Stranger087
2 posts
Shepherd

oh, and updateAfterEvent() at the end of main function... =/

Darkroot
offline
Darkroot
2,763 posts
Peasant

Yeah but most people only need an enterFrame for their purposes.

Matrix159
offline
Matrix159
337 posts
Nomad

If only I could understand all of that code.... XD

domecraft
offline
domecraft
333 posts
Nomad

thx for the tips!!!!

crazyape
offline
crazyape
1,606 posts
Peasant

Begginers should start on basic html coding XD just use notepad....I can just barely do that...then again...it takes me half an hour to FIND notepad....

vikder
offline
vikder
9 posts
Nomad

its quite complex...S:S

Ggbuddy
offline
Ggbuddy
1 posts
Nomad

I was wondering how I should get started. I am new to programming. However, I have basic knowledge in Java, both Procedural and Object Oriented. So, it would be helpful if you could tell me where I should get started and what I should learn.

ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

I...actually...get it.

I'll see if I can get Flash, but... $699 is a lot of dough.

Xinito
offline
Xinito
109 posts
Nomad

I want to make Games so bad...But i'm not good in Coding ect.. :S

Darkroot
offline
Darkroot
2,763 posts
Peasant

I want to make Games so bad...But i'm not good in Coding ect.. :S


Practice makes perfect, there is a barrier into everything that's worth investing time into.
haxxattack
offline
haxxattack
3 posts
Nomad

And a hefty barrier it can be to those new to programming, but great design 'aint worth a **** without the ability to execute it in a manner fitting your vision.

Showing 46-60 of 82