Why is it better to use classes in AS3
I don't like external as files. Most developers prefer this and claim it's better. Explain why is it better to use classes in ActionScript 3.0.
My ActionScripts are different. I strip the classes away and paste it in the Flash IDE. 9 out of 10 times it works fine. My question is Socratic, but I really feel ignorant about this, since there is less code for what I'm trying to do. Have fun with this.
Here's a tutorial I dissected - "example tests keyboard output"
//AFTER
//remove curly brackets and "outer shell of class definitions"
//remove class function "don't need it"
//function at bottom, remove "public", make sure it has a body {}
var _time_scale:Number =开发者_C百科 .25;
var _frames_elapsed:int = 0;
var _clip:MovieClip;
function handleEnterFrame(e:Event):void {
_frames_elapsed++;
_clip.gotoAndStop(Math.round(_clip.totalFrames * _frames_elapsed * _time_scale));
}
//BEFORE
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite {
private var _time_scale:Number = .25;
private var _frames_elapsed:int = 0;
private var _clip:MovieClip;
public function Main():void {
_clip = new SomeClip;
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
}
private function handleEnterFrame(e:Event):void {
_frames_elapsed++;
// we multiply the "real" time with our timescale to get the scaled time
// we also need to make sure we give an integer as a parameter, so we use Math.round() to round the value off
_clip.gotoAndStop(Math.round(_clip.totalFrames * _frames_elapsed * _time_scale));
}
}
}
There may be no answer, but there must be a definative explanation that makes sense.
This sounds like 2 questions:
1) Why is it bad to put the code in the Flash IDE (CS4)? First, the editor in the flash IDE is historically awful. It just doesn't have the features that I expect from an editor for code.
Second, it can be hard to find code that's in the timeline.
Third, when you make a change in the ide and check it into your code repository (you do use version control, right?), the changes are binary and can't be tracked and diffed easily.
2) Why is object orientation good? Object orientation is about encapsulation. It's not the only way to do things, but currently, it's the most popular. Here's a page that does a decent job of explaining it: http://www.alagad.com/blog/post.cfm/what-does-object-oriented-programming-do-for-me
I wouldn't say it's necessarily better to use classes. I would say it is better to follow some type of structure and/or paradigm, especially when coding larger programs that cannot just be copy and pasted. Actionscript gives you many options with how to structure. You can stick with the prototypical/classless paradigm, code using only procedures, etc.
However, when you get past small programs, then the structure will pay off in ease of maintainability.
This is a great discussion question. +1
Steve makes some great points, specifically follow SOMETHING. is CamelCase variable names better than under_score variable names? No real preference, but if you mix and match it makes it harder to follow up. Using a variable named varA looks cleaner and makes for a smaller source code file than thisVariableThatShallDescribeTheLetterA but once your app starts to grow, the more descriptive name helps more and more.
It may add to the size of your app or your source code, but the ability to maintain and follow up make a huge difference. An app we're working on right now started out small, so classes didn't seem important, and thanks to the trickster god of scope creep this app is now HUGE. Now we're playing catch up trying to split our AS out into classes so that we can find them, instead of just relying on our programmers knowledge of where things are.
- Classes promote logically splitting your code into reusable objects.
- In turn this helps to make your program Object Orientated. There is a good reason why C#, Java, Objective-C, AS3 are object orientated languages.
- OO built systems generally scale well, are built on the abstracted data of real life objects (sort of) thereby making them easier to visualise, are reusable, and by encapsulating the information is somewhat safer.
- As mentioned by others external classes can be edited with far better IDE's and can be version controlled.
And not to mention all the pitfalls of timeline code. The only time I code on the timeline is if I am doing a quick experiment or test of a class. Usually 40 lines of code max.
The answer is "It depends". I know it's lame but it's true.
My personal philosophy is to use classes if you have more than 50 lines of code (at least a document class).
1) Using classes will give you all of the advantages of Object Oriented Programming Here, I'll highlight some of them:
- Scalability : You can reuse your classes if you choose to and make different instances of them. On the same token, you can drop the class into another flash project and it will instantly work.
- Extensible/Inheritance : If you have to use the same class but you need to make very small modifications of it, you can create a child or subclass to inherit then override the parts where you need to make your modifications.
2) As mentioned by previous feedback, Flash isn't meant to be used for coding. Although it has been modified significantly over the years to accommodate coding, it is still lacking a good coding interface.
3) If you have code within your flash projects that are hidden in layers, it could be hard to find them. Separating your code and your time line animation would increase your projects' maintainability.
In your example, I think it is totally fine to put it in your flash fla file if you use it only once and you will never change it again.
But hypothetically, let's assume you need to create more flash projects (FLA) or Movie Clips in the same projects using the same code. Of course, you can initially copy and paste them in to the new FLA files or Movie Clips with no problem. But when/if you want to change the _time_scale all projects, you'll have to go to the frame where the code is and change it for each FLA. Imagine if you have 10 changes in your code. If you use a class and put it in a common area, all you need to do is change that class code one time and recompile all FLA files.
In my experience, 90% of the time you will want your code to be in a class. The other 10% is when you have something really straight forward and simple to do, and the code is only a couple of lines - like a navigateToURL (getURL) method...etc.
精彩评论