For any major project, you couldn't pull me away from classes and an external editor. Nowadays it's Eclipse and Flex Builder. Before that it was FlashDevelop, and before that PrimalScript. In general, I hate coding in the Flash IDE.对于很多的大项目来说,你当然无法将我从编辑器以及类身边抽出来,现在ECLIPSE和FLEX BUILDER就是这样的编辑器,以前那是FLASH DEVELOP PRIMALSCRIPT,然而总的来说我却痛恨在IDE里面编写代码
But often enough, when I'm doing some quick experiment or proof of concept, nothing beats opening up Flash creating a new movie, slamming some code down on the timeline, and pressing Control/Command-Enter. No concern about workspaces and projects. You don't even have to save the damn file. "untitled-1.swf"! Amen!但是通常足够了,当我做一些小的实验来验证某个观念,没有东西能够和创建一部新的影片,在时间轴上写下代码,创建命令按键控制相媲美。不需要考虑工作空间和项目,甚至不需要该死的文件。“未命名-1.swf”
Half the time I do that, that's as far as it goes. But sometimes those quick experiments pan out into something I want to develop further. Then I'm left with the task of scraping that timeline code out of the Actions Panel, and turning it into a class. With my recent Mandelbrot experiment, I realized that there are things you can do to make that conversion process easier, if and when it comes.我一半的时间在做这,当然意义远不止这些,然而有时一些快速的实验可以将我需要进一步开发的东西阐释清楚,那时我便会打开一个AS面版在时间轴上欢快的写起来。关于我最近的MANDBOLERT(分形)实验,我意识到了可以做些事情能够让传统的过程更加容易,如果确实要这样做的话
Most of these go back to good practices I tried to follow way back in AS1 before I was even writing classes, and everything was on the timeline.大多数包含了好的习惯,我想尝试变回咋AS1上开发,所有的编程都围绕时间轴展开
0. All imports right up top.所有的引用要在最右上方【废话】
1. All timeline vars right after imports. Use var, and type your data.所有的时间轴变量在导入语句后申明,使用VAR关键字以及所有的数据
2. ALL code within functions. ALL code. NO stray statements on the timeline. Sometimes when you are just messing around, you'll wind up with a mishmash of functions and statements. Gather up all those loose statements and put them into functions.所有的代码包含在函数里,是的所有的代码,时间轴上没有直接的代码段,如果有时候你确实混乱的不行了,收集那些重复了很多次的代码片段将他们封装成函数【这一点个人感觉很有技术含量的一个事情】
3. All the statements that are just there to set things up go into an init function.所有用来准备的代码封装到INIT函数里
4. Now you are allowed one statement on the timeline itself: init(); The stuff in the init function should be designed to run only once. "init" means "initialize", which means, "set to the value or put in the condition appropriate to the start of an operation". If you find yourself calling init() again, pull some of that stuff out into a "reset" function or something of the sort, and that's what you call multiple times.这样你在时间轴上,只允许一条语句INIT();这里面的代码设计来只运行一次,INIT意味着初始化,如果你的INIT运行了两次,那么抽出那一部分重新组装成RESET()函数
This leaves you with something like this:向下面的代码
That's timeline code, but hell, it's almost starting to look like a class. Not only is it well organized and easy to read, but when the time comes to convert it to a class, there are a few simple steps to get it there:这就是时间轴代码,不仅组织的很好,而且也易于阅读,即使是需要将这些代码转换成类,那么也只需要几个简单的步骤而已
1. Wrap the whole thing in a package statement.将所有的语句包含在包语句里
2. Wrap everything after the imports in a class statement that extends MovieClip or Sprite.在导入语句之后包含类申明,或者是扩展MC类
3. Add indentation to taste.添加标识符
4. Add private to all your vars and functions.为你所有的函数添加private修饰符
5. Wrap the call to init() in a constructor. (Or remove the call to init, and turn the init function into your constructor. I prefer to keep the constructor as just a call to init.)将INIT包裹到构造函数里
6. You might have to add some more imports, as the IDE auto-imports a lot of common stuff.也许你要添加更多的导入语句
If all goes well, you should have a working document class.如果顺利那么通过这些步骤你便得到了一个良好文档的类
【zinking的话:】觉得这篇文章介绍了如何编写时间轴代码,讲的更多的是代码风格,我也很推崇这些风格,这样的代码是所有的程序员都乐于看见的。