Join David Gassner for an in-depth discussion in this video Modifying an existing code snippet, part of Flash Professional CS5: Code Snippets and Templates.
There are times when you might want to modify the code snippets that are included with Flash Professional CS5. For example, if you frequently use a particular code snippet, but find yourself always making the same changes to it. For this demonstration, I'll use a beginning file from the Ch05_CustomizingSnippets folder under Exercise Files named LogoWithKeyboardEvents.fla. This is the same presentation I've used in a previous video in this series, that's designed to allow me to apply keyboard events to this biker object.
The particular code snippet I'm going to apply allows me to move the object around the screen when the user presses the Up, Down, Left, or Right keys. I'll show you the initial state of the snippet, then show you how to make a change and how to retest it. I'll click on the biker object to select it, and then go to the Code Snippets panel. From there, I'll go to the Animation panel, and I'll double-click to insert the snippet Move with Keyboard Arrows. This keyboard snippet listens for the key-down event and then handles four different cases for the Up, Down, Left, and Right keys.
Let's say that I find myself using the snippet a lot, but I typically don't need the Up and Down keys. I only need the Left and Right. So I go in, and then I add some comments and my modification is done. But if I want to make that the default appearance of the code snippet, I can do that through the Code Snippets XML file. I'll close the Actions panel, and then in the Timeline I'll right-click on the Actions layer and delete it. Then I'll go to the Code Snippets XML file. I'll open the Options menu, and choose Edit Code Snippets XML.
I'll close the Code Snippets panel, and press F4 to look at CodeSnippets.xml in full screen. Then I'll press Ctrl+F on Windows or Command+F on Mac and I'll search for the string Move with Keyboard. That takes me to that code snippet. Make sure that you're modifying the correct code snippet by just comparing the title. Now, I'll go down to the code that's included with the snippet, and I'll add comments around these two cases, for keyboard.UP and keyboard.DOWN. I'll select all of the code I want to comment out, then I'll go to the top of the editing area, and I'll click the item Apply block comment.
That comments out that code. I'll then close the file and say yes, I want to save the changes. Then I'll repeat the actions I took at the beginning of this demonstration. I'll click on the biker object that I want to move around the screen, go to the Code Snippets panel, open the Animation category, and double-click to insert the Move with Keyboard Arrows code snippet. Now, my default code snippet has changed. The keyboard.UP and keyboard.DOWN cases are commented out and each time I use this code snippet in the future, that will be the code that's inserted.
You can make as many modifications to your XML file as you want to, and you will have configured your code snippets to behave the way you like.
- Touring the Code Snippets panel
- Adding simple and complex code snippets
- Handling mouse and keyboard events
- Starting and stopping video and sounds
- Loading external SWF files, images, and text
- Customizing code snippets
- Understanding template types and categories
- Replacing template content
- Creating a slide show