Viewers: in countries Watching now:
In ActionScript 3.0 in Flash Professional CS5 Essential Training, Todd Perkins shows Flash designers how to incorporate ActionScript code into their projects and create interactive presentations and applications. The course includes a review of ActionScript language basics and the object-oriented programming (OOP) methodology, a tour of those Flash Professional CS5 features designed for developers, such as code hinting and the Code Snippets panel, and instructions on interacting with objects in the Library and placing code on the Timeline. Exercise files are included with the course.
The next step in making the game is to add drag and drop functionality. Now you may already be familiar with adding drag and drop functionality through code snippets. This time we are going to write the code all by hand, so you get some more practice and become more comfortable writing the code. The first thing I am going to do is go in the constructor, and right below where it says, constructor code, I am going to type (MouseEvent.MOUSE_DOWN, and then we'll run a function called drag.
So we will create the drag function now. Go down a few lines below the constructor, so right below that next curly brace, and then type function drag(evt: MouseEvent), type :void, all lowercase and write some curly braces. Now to drag an object, all you need to do is write this one line of code, startDrag, with capital D. Close out the parentheses. Type a semicolon.
Now to test out the drag code, save this file and then test the movie. You should be able to drag any one of these objects if you click your mouse down, there we go. Now it looks like the only way to drop one is to pick up something else. We will look at how to fix this in just a second. Right above startDrag, let's add an EventListener so we can stop dragging as well, but instead of adding the EventListener just directly to this object, like we are doing with addEventListener, we are going to add it to the Stage.
So stage.addEventListener and event is going to be MouseEvent.MOUSE_UP. Remember, that's all caps. Now we are adding it to the stage because if someone clicks and drags an object around and releases the mouse button, they might drag really fast and release the mouse over something that's not the object that are meaning to drag. Now in case that happens, we are applying this EventListener to the stage, so wherever they release their mouse, the object will drop.
Just type a comma and then type drop, and then close up the parentheses, and then type a semicolon. So now let's just copy and paste the drag function, and change the name to drop. Change stage.addEventListener to stage.removeEventListener and then change startDrag to stopDrag. Save the file and test the movie. We should then be able to click and drag and drop all the draggable objects.
So just in case, you should try to drag and drop each one of these. It looks like it works for every one. So now the drag and drop functionality is there, and all the objects that link to this class are draggable. Again, you can see the power of writing this code in a class file, instead of writing it in a main timeline. It's easy to recycle, and it applies to as many objects as you want.
Find answers to the most frequently asked questions about ActionScript 3.0 in Flash Professional CS5 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.