Drag And Drop
In this video we will learn how to create a Drag and Drop operation in Flash Using ActionScript 3.0.
To be able to do that we are missing one element of information to figure out what connects with what. In this video we will learn a very cool trick using Dictionaries to create linkages that will cut down the amount of logic in our application drastically.
Video Transcript
00:00So now we have all of our elements on the stage,
00:03and really the only thing missing at this stage
00:05is actually to start making our elements interactive.
00:08And to do that, what I want to do
00:09is actually send the created element to our loops,
00:13because our loop actually knows the relationship.
00:15Our loop knows the relationship between the English
00:17and the French word.
00:18But when we create our element separately, it has no clue.
00:22So we can't really add much more logic into our area here.
00:26Last item, two things we could add here probably,
00:29just making sure that our sprite is clickable, right?
00:32So we're going to set it as,
00:34we're going to set its button mode to be true.
00:37And we're going to tell our sprite
00:39that its children are not.
00:45We have all the logic that we need
00:47inside of our application.
00:48Only thing that we're missing from our for loop,
00:51for our for loop was actually to be able
00:53to receive a created element.
00:55And to do that, I'm just going to set the return value
00:59of create element to be a sprite,
01:01and then send our new created sprite,
01:05and just send our new created sprite
01:07directly to our create element.
01:09So now that we're done with creating elements,
01:11we could go back into our initial RSS complete
01:14and see how could we complete the logic
01:16that we're still missing.
01:17The next step would be to store that information,
01:20first of all.
01:21So we'd probably want to create two variables here, right?
01:23So one would be, let's call this sprite word one,
01:28which would be a sprite.
01:30And we'd have a sprite word two,
01:33which would be a sprite as well.
01:34And we could grab that value of sprite one,
01:39and we could grab the value of sprite two.
01:45Perfect.
01:45So now that we have access to our sprite elements,
01:48we could probably start adding our event listeners.
01:52And to do that, let's see what type of events
01:54are we going to have.
01:55We're going to have a drag and we're going to have a drop.
01:58So let's just quickly create our drag and drop,
02:00just a quick little drag and drop.
02:02First of all, let's just drag down here
02:04and we'll just create two new private functions.
02:09First one is going to be on drag,
02:12which will be accepting a mouse event.
02:19By default, it fills out for me those curly brackets
02:22and CS5, so I don't have to fill them out.
02:25And we have on drag and we have on drop.
02:30So let's add those events.
02:33If we go back up here,
02:34we first want to add events to both of them.
02:36So we want to take our SP word one, add event listener.
02:41And what event do we want to add?
02:43We want to add a mouse event,
02:45mouse event dot mouse down, right?
02:48Because we want to capture as the user clicks down
02:52and what do we want to do in that scenario?
02:53You want to call on drag, right?
02:57The next one is we want to set also
03:01when the user releases it, right?
03:04So again, mouse event dot mouse up, out this time.
03:10Oh, excuse me, not out, but up, right?
03:12Out is when the user leaves that area,
03:14but we want to actually when the user picks up their mouse
03:17or releases their mouse and we want to call on drop.
03:21So let's add quickly the logic of on drag and on drop.
03:24So first of all, when on drag is called,
03:26what would we want to do?
03:28We want to actually start dragging the element, right?
03:32So we're going to take our target,
03:34which is our movie clip or is our sprite in this scenario,
03:37and we're going to tell it start to drag.
03:43And on drop, what do we want to do?
03:45We want to take our target
03:47and we want to tell it to stop drag.
03:51All right, let's see if that did what we want it to do.
03:55So first of all, as we grab something,
03:57it should now be draggable and it is.
04:00And as we release it, it should let go.
04:03And let's see what's going on here.
04:04So first of all, not everything seems to be clickable.
04:07Some are clickable, some are not, of course,
04:09because we only did one side.
04:11We did only toward one, we didn't do it toward two yet.
04:14So let's continue and actually on stop drag,
04:18there's one more thing we wanted to add.
04:20Before, let me show you why.
04:22So if I drag something and I put it on top of something,
04:24I want to make sure that my item
04:26is going to be now on top of everything.
04:29And to make sure that my item is on top of everything,
04:32what I could do is as soon as the user clicks on something,
04:35as soon as the user starts pulling it,
04:37let's just tell Flash again,
04:40put it back, add that child again.
04:42And what that would do is actually tell Flash
04:44that as we click on something,
04:46we want it to be the top of everything.
04:47Ooh, let's see what's going on here.
04:49Well, it's expecting a display object,
04:51so what we're going to have to do,
04:52and that's just a little thing that Flash
04:55isn't smart enough to realize
04:56that this target is actually a display object.
04:59So I'm just going to tell Flash,
05:00remind it that is a sprite.
05:04So I'm basically telling Flash,
05:05don't worry, this target is a sprite
05:06because we know it is a sprite.
05:11Ooh, not is, as.
05:16There we go.
05:17So if I click here, you'll see that Orvoir goes above,
05:21and Xian goes above as soon as we click on it.
05:24So every time we click on something,
05:25it will be the top of the list.
05:34So now that we got that sorted out,
05:35and just a little note,
05:36so as we're just, we're not changing anything,
05:39we're just telling Flash, treat this as a sprite,
05:41versus is, what is does is basically asks Flash,
05:45is this a sprite?
05:47And then Flash says yes or no.
05:49And that's why we're getting an error
05:50telling us that there was a Boolean value
05:52when it wasn't expecting a Boolean.
05:55All right, so we have our start drag
05:57and we have our stop drag.
05:58Let's make sure that we do this also for Word 2
06:00because without that, we don't really have an application.
06:04And all we really have to do is change,
06:06copy those two lines and change it to Word 2 as well.
