Community
Good Programming Technique 101
|
Posted Jul 29, '08 at 10:12am 178 posts
|
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: 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: 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: 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{ -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: 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. Let me know if something doesn't make sense, I'm sure it may be quite confusing. :D |
|
Posted Jul 29, '08 at 11:13am 2,853 posts
|
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 |
|
Posted Jul 29, '08 at 11:35am 178 posts
|
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 :D |
|
Posted Jul 29, '08 at 12:21pm 2,853 posts
|
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! |
|
Posted Jul 29, '08 at 12:25pm 501 posts
|
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! |
|
Posted Aug 12, '08 at 10:26am 178 posts
|
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: myButton.onRelease = function(){ 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{ now you could call this event like so: myButton.onRelease = function(){ 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{ (The ':Number' is not required but helps to classify what the parameter should be) Then you can do this: myButton.onRelease = function(){ 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. Hope that makes sense, feel free to comment or ask questions if you're confused. |
|
Posted Aug 12, '08 at 12:19pm 272 posts
|
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. -.- |
|
Posted Aug 12, '08 at 12:30pm 501 posts
|
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? |
|
Posted Aug 12, '08 at 6:30pm 1,044 posts
|
Basically what you're saying is: 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. |
|
Posted Oct 27, '08 at 6:29pm 10 posts
|
More on modular programming... I, personally, am not a AS programmer as I prefer the world of void pointers and pointers to void pointers ;)
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. |
|
Posted Oct 29, '08 at 1:10pm 10 posts
|
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. |










