[Note: I just released version 1.1 of ASExpander. https://www.bit-101.com/2003/1627 . This post is still worth reading for background and usage, but check out the other one for the latest version.]
There’s been a lot of talk about AS3 being “hard”. A lot of it stems around the fact that its is more verbose than AS1 or AS2. It just takes a lot more typing to do things that were really simple in earlier versions of Actionscript.
I’ve been using Flash CS4 a LOT lately, for spiking ideas, doing quick visual experiments, etc. In fact, the vast majority of stuff on my other site, Art from Code, is created directly in Flash CS4 right on the timeline.
So of course, I find myself typing a whole lot of the same few lines of code over and over (and over). The obvious solution was to use a snippets panel. Lee Brimelow has a nice one. https://theflashblog.com/?p=336
But that didn’t quite fit my workflow – making sure the panel is open, grabbing the mouse, clicking the item you want in the list, clicking the copy to clipboard button, clicking back in your code in the right place, and pasting.
One of the comments there mentioned that functionality like in Text Expander would be nice. I thought about it for a minute and realized that wouldn’t be too hard to do. And so I bring you ASExpander.
ASExpander is a simple JSFL command script. Save the following in your commands directory as ASExpander.jsfl:
[as]map = new Object();
map["&meta"] = “[SWF(width=800, height=800, backgroundColor=0xffffff, frameRate=31)]”;
map["&stage"] = “stage.align = StageAlign.TOP_LEFT;\nstage.scaleMode = StageScaleMode.NO_SCALE;”;
map["&resize"] = “stage.addEventListener(Event.RESIZE, stageResizeHandler);\nfunction stageResizeHandler(event:Event)\n{\n\ttrace("stage resize");\n}”;
map["&bitmap"] = “var bmpd:BitmapData = new BitmapData(800, 800, false, 0xffffff);\nvar bmp:Bitmap = addChild(new Bitmap(bmpd)) as Bitmap;”;
map["&enterFrame"] = “addEventListener(Event.ENTER_FRAME, enterFrameHandler);\nfunction enterFrameHandler(event:Event):void\n\t\n}”;
map["&mouseDown"] = “stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);\nfunction mouseDownHandler(event:MouseEvent):void\n{\n\ttrace("on mouse down");\n}”;
map["&mouseUp"] = “stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);\nfunction mouseUpHandler(event:MouseEvent):void\n{\n\ttrace("on mouse up");\n}”;
map["&mouseMove"] = “stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);\nfunction mouseMoveHandler(event:MouseEvent):void\n{\n\ttrace("on mouse move");\n}”;
map["&keyDown"] = “stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);\nfunction keyDownHandler(event:KeyboardEvent):void\n{\n\ttrace("on key down");\n}”;
map["&keyUp"] = “stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);\nfunction keyUpHandler(event:KeyboardEvent):void\n{\n\ttrace("on key up");\n}”;
map["&loop"] = “for(var i:int = 0; i < 10; i++)\n{\n\ttrace(i);\n}”; map["&2loop"] = “for(var i:int = 0; i < 10; i++)\n{\n\tfor(var j:int = 0; j < 10; j++)\n\t{\n\t\ttrace(i, j);\n\t}\n}”; _doc = fl.getDocumentDOM() _tl = _doc.timelines[_doc.currentTimeline]; _layer = _tl.layers[_tl.currentLayer]; _frame = _layer.frames[_tl.currentFrame]; script = _frame.actionScript; for(key in map) { script = script.split(key).join(map[key]); } _frame.actionScript = script;[/as] Then, I HIGHLY recommend assigning a shortcut key to this command. I assigned Command-E, which overrides “Edit Symbol”, which I never use anyway. Now I can just type: &enterFrame hit Cmd-E, and I have an enter frame listener set up. Notice that my hands never left the keyboard! It’s easy to add your own snippets. Just give it a token and text: [as]map[“token”] = “text to replace token with;”;[/as] If you’re not a coder, get one to write some useful snippets for you and put them in string format. They’ll know what to do. I’ve gone with an ampersand (&) prefix just to avoid possible conflicts with other code in the script. You might have a “enterFrame” in your code that means something, but probably not a “&enterFrame”. But that’s not built into the command at all. If you don’t like the ampersands, change them to %s or *s or !s or just use straight text if you are brave. Anyway, hopefully this makes AS3 a tiny bit less painful for a couple of people. Let me know if it helps.