Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Combining Code Snippets in Flash Professional CS5 (episode 41)

0 (0 Likes / 0 Dislikes)
[Adobe Developer Connection] [Doug Winnie - Principal Product Manager; Flash Catalyst, Flash Platform Workflow] Hello there. In this video, we're going to introduce how you can combine multiple code snippets together to get the desired results. In this case, we're going to combine a timer and then with ClickToPosition object. Now, obviously, I'm not going to click to position the object. I'm going to use the timer to position it. So I'm going to show you how you can change your code to get the result based on what we are going to build here. So let's go ahead and first create the timer. So using the code snippets, I can select the Simple Timer. I'm going to select that code snippet and then say Apply. Again, it's going to apply the code into our Actions Panel here. Let's go ahead now and add in the second code snippet. So I'm going to select to object, and I'm going to say Click to Position an Object. Now, the reason why I have to select the object is--let me just show you what happens if I don't select it. I'm going to get an error that says that this action requires an object selected on the Stage. Even though we're not going to use that, we need to have it selected to add the code to the project. So let's go ahead and add it now, and you'll see that that code is placed at the bottom of the Action Script file. Now, we need to combine the two of these together, so let's take a look at the first one. In this case, I have this event handler that has been created that I want to use to then control this function here at the bottom in this ClickToPosition. So let's modify the top function here. Instead of increasing the time elapsed, let's instead add a line after this, where I'm going to execute the event handler from the second code snippet. The code snippet names can sometimes be confusing, so let's actually just copy and paste this to make sure we get the spelling right. So hit Copy and Paste, and, again, this is a function so I want to have my open and closed parentheses. Now, if I ran this, I'm going to get an error, and the reason why is because I need to actually provide a MouseEvent when I execute this function. I'm not passing a MouseEvent on line 19 here, so I'm going to modify this and remove the MouseEvent required parameter inside of our event handler. I'm also going to remove the addEventListener for the blueSquare. Okay, let's go ahead and test this and see what we have. You'll see that the seconds have elapsed, and I have the object moved correctly.,. But the movement of it isn't exactly what I wanted, so let's modify that next. Inside of my event handler down here at the bottom, I don't want to move it to a specific location. I want to modify it's existing location and maybe add, say, 5 pixels each time. So let's go ahead and modify this. I'm just going to say blueSquare.x = blueSquare.x + 5. And let's do the same thing with y. Okay, let's go ahead and save this and run it again. There. You'll see now the object is moving every single time that the timer hits. Now, we've done a similar example with this before, but in this case, I'm using two separate code snippets, so I'm able to do it first, using the simple timer, and I've been able to use the ClickToPosition object and re-purpose it to do something else. Now, I can use this to then modify and use it in other ways in my project as well. In the next video, we're going to show how you can use code snippets to learn new ActionScript. We're going to introduce how you can work with text fields in ActionScript using code snippets and explore how you can work with the code and then find out new things about ActionScript. [Adobe Developer Connection]

Video Details

Duration: 4 minutes and 6 seconds
Language: English
License: All rights reserved
Genre: None
Views: 68
Posted by: adobetv on Oct 21, 2010

Complex coding can help you build efficiency and flexibility into a Flash project.  Doug explains how to achieve a desired result by combining snippets together.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.