Keyboard
Its time to listen to our keyboard events and start animating our dot.
Video Transcript
00:00Now the next step after this is actually we want to start listening to what's going on actually
00:05in our keyboard because we want to be able to listen to the keyboard and based on the movements
00:09of the keyboard we want to be able to move our dot around. Now when we created the dot we could
00:14if we wanted to put our listener that we need in that create dot but for our second purpose
00:20I am going to put that all inside of my main right now and we could always move that around
00:27later on but there's no wrong and right in this scenario of creating the dot even though
00:31you know let's actually put it right into the create dot because that makes sense right we're
00:35while we create the dot we're also starting to listen to whatever that dot does. All right so
00:41we're going to use the stage we're actually going to access the stage itself to ask it for keyboard
00:47changes so I'm going to add an event whoopsie we're going to add an event listener and that
00:56event listener is going to be a keyboard event. Keyboard event dot key up because we want to listen
01:05to when the keyboard is clicked up and then we're going to call the function on key change and
01:11notice that I'm not getting the hints even though I really want them but I'm not worried about
01:15it too much because as soon as I create the function on key change automatically in Flash CS5
01:20and Flash CS5.5 the keyboard event will be imported. Now if you're working with an older
01:26version of Flash which does not have it or download the source files so you could see
01:30all the different all the different classes that are getting imported we're just going to
01:34assume they're there and if you don't have access to the source files you just go into Google
01:39AS3 whoopsie go into Google AS3 and in Google AS3 just search for keyboard event and you'll
01:46find the whole path googleas3.com and it's going to give you all that information that you need
01:50so just by going into Google AS3 and just clicking keyboard event you'll see that you'll get that
01:56flash dot event a keyboard event and that's exactly the package that you want. All right
02:01so let's create that function so private function and it's fair enough to say it's not only a function
02:06it's also a listener and let's see what we're going to put in this listener so first of all
02:10we want to call an on key change which is going to be an event and it's going to be a keyboard
02:18event all right now in this keyboard event there's one property of a keyboard event and
02:25i'm going to use a switch for this because i want to listen only to specific key points
02:29so i'm going to ask for the key code and the key code is going to give me actually that
02:33code of the of the key that's being pressed on so if even before we do anything here let's
02:37just comment this out for a second and we'll see what i have trace out here e dot key code
02:48and i'm just going to click into the application and just click on the letter a you'll see that now
02:5265 is coming out in the output if i click up down left well that was right and left you'll
02:59see that the values actually that i care about are 37 38 39 and 40 so let's add these this
03:07elements this logic into our switch statement and in our switch statement we're just going
03:13to add cases we're going to have a case 37 case 38 case 39 and case 40 and as we've seen before
03:24and we've seen we've seen just a second ago 37 is left 38 is up 39 is right and 40 is down
03:36all right so let's start with 37 37 what do we want to do we first want to call the function
03:40move it we want to call the function moving we're going to create it in a second and what we want
03:44to do is we want to move our element one pixel to the left whenever we call whenever the keyboard
03:50is pressed up now i'm just going to put here a break because we don't want to call any of the
03:54other cases after that i'm just going to copy this into up so we can adjust it and up as well
04:01and in up what do we want to do we don't want to move the x position we want to move the y
04:04position and we want to move it up and up is a minus because we're going to move up which
04:10is a negative values with y let's do this for the right as well for the right we want to actually
04:15move an x by one and last but not least for 40 we want to actually move it down to move it down
04:22we want to push one pixel down in the y coordinates now any other value our default should be to just
04:29break and just to keep things clean this double break is not needed because it will be breaking
04:37anyways so we could actually delete this line and this is valid there's no need for it because our
04:42move it will break right after because the default has nothing in it so let's save this and before
04:48we can actually test it out and see if it works what we'll have to do is we're going to have
04:51to create that move it function now to create the move it function is quite easy so let's do
04:56that provide it function move it now i'm going to use the first parameter is going to be my x
05:06x um x speed or x uh change let's just do it let's call it uh x
05:15new x there's going to be an integer we're going to be new y which is going to be an integer as
05:24well now what i want to do is i want to store this information that just came in to store this
05:29information that just came in even before i store it well before i even store it i could just
05:35directly just move my element right i have my little shape and my little shape is called dot
05:39and i could go to my dot and just directly dot dot x and say you know plus equals new x dot dot y
05:47plus equals new y but this is not going to work for us right because if i just do that and i just
05:55press up i'm going to have to press 100 times to get my element to go up and in the real game as
06:00soon as you press once the dots just starts moving and moving and moving until you change
06:03this direction or until it gets hit so to start this problem out what we're going to have to do
06:09is we're going to have to create some sort of logic that actually creates some sort of animation
06:13now to create this animation what i want to do is i first want to store the information this change i
06:18want to store it in some sort of variable to save that variable i'm just going to create a new private
06:22var and that private bar is going to be called p for point because we're only going to have one
06:27point so we don't have to give it too many names in this scenario this is going to be our point
06:31whoopsie i didn't write your private correctly so let's fix that so i'm going to call it p for
06:36point which is going to be our direction point so if we want to if we want to be really good
06:40people we could call this pder so it's a point for direction our direction point
06:48or we could call direction point if it makes it easier for you so let's delete our move it
06:53and direction point we want to set our position point and first of all i want to create a dot
06:57right in the beginning because the reason why i want to create the dot right in the beginning is
07:01i don't want to need to create a new dot each time i want to just update my dot all right so
07:06before we actually do this we're going to go back into our create dot and right before our event
07:11listener we're just going to create a new pder which is going to be a new point and again we
07:17have a lot of imports that are happening throughout but we're not worried about them because we know
07:21that you guys have flash ds5 flash ds 5.5 or you're already familiar with working with action
07:27script with googleas3.com which i encourage you deeply to use it it's much better than me reading
07:34out to you all these import lines because it'll make you used to getting used to these stuff and
07:39i do encourage you to upgrade to at least flash cs5 or flash cs5.5 if you're capable of doing so
07:46all right so we have our dot our pder now our pder we want to update our x to be that new x
07:53and pder.y same deal we want to update our new y all right now we're ready our
08:03our direction controller is now in control it's working the next step that we actually want to
08:08do after such a scenario like this is we actually want to start listening to some sort of event and
08:12we're going to use a enter frame and for that for this time around i'm just going to use my mains
08:17enter frame now this enter frame is going to be very simple so first of all i'm going to add
08:21here an add event listener let me make sure that i'm making not making any spelling errors here
08:31add event listener here we go now the type of the event is going to be event dot enter
08:38frame because we want this to be called every single time of the frames refresh and i'm going
08:42to call the function on enter and it's time for me to create that on enter function so i'm going to
08:49create our private function on enter it's going to be accepting only it's going to be accepting an
08:56event and it's time for us to start moving our element moving the element is easy right so i
09:03could just go directly to my dot dot x and just say move that x with by the direction of pder.x
09:12take my element dot y plus equals pder.y and this should in theory do the trick let's see
09:21if we have your little error type not found was not compile time event well we want to make
09:26sure that it's imported for some reason it didn't import it for us automatically so we're going to
09:30go here and add it manually so i'm going to import flash events event there we go
09:39and we have our dot and let's see what happens i'm going to click only once on the up and release
09:43and we'll see that our element will start moving upwards and as soon as i click right
09:47and release our element will start going right down and so on and so forth and we might not
09:52be happy with the speed itself right it could be too slow but you know what slow is good because
09:56then it gets makes the game harder because we might have our enemy going faster and faster
10:01so the slower we go the harder it is for us to actually win the game so that sounds actually
10:05really good
