Hey, welcome back in the last lesson, we showed you how we can enter in a sequence and we can have
the element moving and also you might have noticed that it stays with the last command.
So we need to take care of that and we're going to add in and else here and if it's else.
So that means that there are no more commands and that we've finished running the commands.
So this is where we can update that inner HTML and set back that back to path and also have a return
there so we don't execute anything else on this move or function.
So let's try that.
And now and we execute it, we end up with set path.
So that's we have the ability to do that.
And we can also add in the ability to get a random movement.
So we'll make an application a little bit more interesting.
So let's set up an array and we need an array with movement array.
And then here is where we've got all the different directions that we want to move.
So we've got right, we've got left up and of course, down.
So now that we have an array with all the different possible movements, let's generate a random movement
and add it into the function, into the add fun.
So how do we do that?
We need to select a random item from that array.
So going into the console, we can use math random.
And now that we do have that array, so that map movement array, you can see we've got all of the movements
there.
So if we take that movement array.
And if we return back and index values of if want it to get the first one, we could do one and so on.
So if we randomize this using a math flaw to bring the math value down to the floor so it gives us more
control and then using math random, we need to multiply math random by the number of possible options
and we can get that from the movement array using its length.
So what?
This will return back every time we enter it.
It's going to enter in a random movement.
So we see we've got all of them accounted for so we can apply this.
And this is going to return back a random movement.
And now all we need to do is associate it to a specific key so we can try that out.
And how about we do the AKIE?
So that's number 82.
So I just pressed are just to see what it comes Barkhouse so we can check to see if key is equal to
82.
So if we do are we're going to get a random movement.
So let's create a temporary variable for that.
We can just call it temp and this is going to return back that random value coming out of that array.
And then here we just simply add it to the array.
So let's try that.
So I'm just pressing R and you can see I'm getting random movements added in and then everything is
still going to work.
The same of sequence is going to work the same.
So coming up next, so go ahead and add this into your project.
So I add in and you can also have other keys, do different things and different movements as well.
And you can also do combinations if you want.
So coming up next, we're going to add in the ability to remove some of these commands.
So if, for instance, you've got one of these commands, you don't want it within that array anymore.
There's an option to remove it.
So if you could click on it and remove it out, that's what we're going to add in an upcoming lesson.