Linking
In this video we create the logic of our application. in the end of this video our game will actually feel like a game. At the core of our logic lays a simple Dictionary we will see how we can use our dictionary to create a very simple logic to detect if words match in our application.
We finish up this video by figuring out how to know when the game ends.
Video Transcript
00:00So, now that we could drag both the first word and the second word, the next step is
00:05actually to create a linkage between the two elements, so we would know when these two
00:11elements drop on top of each other.
00:15And to do that, I'm going to have to create a little dictionary, a global dictionary
00:19up here, and I'm going to use that dictionary basically as a linkage to create a two-directional
00:25relationship between our elements, and look how I'm going to do that.
00:28So, first of all, I'm going to create a private var, and I'm going to call it dick for a
00:32dictionary, and we're going to instantiate our dictionary as soon as we start up our
00:39application.
00:40Let's just do that right here, goals a new dictionary.
00:46So, now we have our dictionary, and now that we have our dictionary, we could go back
00:49into our for loop, and what are we going to do in that for loop?
00:53Basically, what we want to do in our for loop is basically combine the two, making
00:58sure that there is a direct relationship between the two different words, and it's
01:02quite easy to do that.
01:04The way we're going to do that is actually just we're going to take our dictionary, and
01:07we're going to give it a value, a key value, of one of those clips, and we're going
01:14to set its value of that key, so we have our dictionary has a key which could accept
01:19objects of different types, and we're going to say that its value would be word2, and
01:24we could do the same thing, and we need to do the same thing for a relationship in
01:28the other direction.
01:29Why are we doing this?
01:30By doing this, we're actually creating a linkage between the two.
01:34So now, anytime that I get word1, I could ask the dictionary, and it would return
01:38to me word2.
01:40Every time that I would have word2, I could then get word1, and that's immensely
01:45powerful.
01:46So now that we have that information, we have that linkage between the two, it's
01:50all we really need to create a relationship between the two.
01:55So now the next step is actually to go into our drop.
02:00Now that we have a relationship, and the user is dropping their element, the next
02:04step is really simple, because all we really need to do is we just need to take,
02:10and we're going to ask your if statement.
02:12If our target has a hit test, so if it hit test object, if it touches a specific
02:24object, what object?
02:27It's counterpart, right?
02:28So we're going to ask from the dictionary to receive his counterpart.
02:33And now that we got the counterpart, we're basically asking for whatever
02:36we just dropped, is it touching its counterpart, its linkage?
02:41So for example, if we pick word1 in French, and if we look again into our
02:45dictionary definition inside of our loop, we'll see that if we had word1, now
02:49it's going to test to see word2.
02:53So it's basically, by making these really simple dictionary, I don't need
02:57almost any logic inside of my drop, because all I really need to do is first
03:01of all check, what I just got right now, does it hit test, does it have a
03:05relationship, and that basically captures really all of our if statement.
03:10And that's all the logic that we actually need to complete the logic of
03:14our drag and drop application.
03:17So only if the relationship is established, only if it finds that element,
03:21we want to actually remove both elements.
03:24And to do that, we're going to create a new method, which we'll call
03:27remove element, just as we created a create element.
03:32And a remove element, first of all, we will send to it our target.
03:39We want to take our target, and again, we want to make sure that it's as a
03:42sprite, because we're going to send a sprite to our remove element.
03:46And what other element do we want to remove?
03:48We want to remove this element, but we also want to remove our dictionary.
03:52And to do that, we're going to remove element, and there's actually
03:56an importance in the order of how we're doing this here, and we're
03:58going to see why in just a second.
04:00So I'm going to set, again, I'm going to grab my e.target in our
04:04dictionary to get my other element, and I'm going to set it as a
04:06sprite to make sure that my remove element realizes that it's a sprite.
04:11So now that I removed both elements, let's see how I would actually
04:15implement the remove element method and see why was it so important,
04:19the order where we first take the item that's in the dictionary, and
04:23then only after that we're taking the element remove element.
04:34And our remove element is going to accept only one parameter.
04:37It's going to be our item, and it's going to be a sprite.
04:42It's not going to return anything.
04:44And let's see what logic do we actually want to do here.
04:46First of all, when we receive an item, we want to remove its
04:52event listener, right?
04:55So let's do that, first of all.
04:56We're going to do that directly with the dictionary.
04:58So what I want to do is actually, OK, so what I want to
05:03do is I want to take my item, and I want to remove my event
05:12listener, right?
05:13To what event listeners do we want to remove?
05:15We have two event listeners.
05:16So let's just grab those events, because it would be
05:18way easier.
05:19We're actually going to grab those two events, and then we
05:24could just change the method instead of retyping again.
05:27So I'm just going to take my item, and first of all,
05:31before anything we want to remove, right?
05:33So we want to, instead of add event, we want to remove event
05:35listener to both.
05:37And we just remove both events of that item that we
05:39send, and we send both items, so we remove both items.
05:42Only thing is we want to also get rid of it from the
05:44dictionary, so the next item we'd want to do is actually
05:47break apart that relationship, right?
05:51So we'd want to take our, we want to delete from the
05:56dictionary our item, which would then break one of the
06:04directions, one of the relationships.
06:06And now you see why it was important, actually, to first
06:10send the dictionary value.
06:11Because if we would break the relationship, then this item
06:14would never be found, right?
06:16So we first have to break apart the relationship from
06:19the counterpart, because we need access to that
06:23dictionary value.
06:24And after we got rid of that dictionary value, then we
06:26could send our regular target.
06:29So hopefully that makes sense.
06:31So let's run our application one more time and actually
06:33see an action if it's working.
06:35But one last thing that we're missing is actually to remove
06:38our element, right?
06:38So we're going to remove child of our item.
06:44And let's run our application and see, oh, a little
06:47error here.
06:48Let's see what's going on.
06:49We see we're missing here.
06:50OK, because we started to hit test, but we never closed it.
06:53And again, one of my favorite shortcuts is command, comma.
06:57And we'll see the command, comma is showing me one is
06:59right, oh, but two isn't.
07:00All right, so we want to close that bracket as well.
07:03And then we'll see that it should, oh, we have to save
07:06first, of course.
07:07And we'll see that it should be good now.
07:10So if we run our application one more time, we'll see
07:12now we could drag around our elements.
07:16And the only thing that we're missing now is actually to
07:18test out to see, OK, so we have no, and we know that's no.
07:21So if I drop it on no, it should then end.
07:23There we go.
07:24It went away.
07:26And our shah should be a cat.
07:28And when, how, goodbye, or vahr would be goodbye, oh, vahr.
07:35All right, so now our drag and drop functionality is
07:38working as well.
