Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
The ship is currently centered on screen and unfortunately immediately set to explode. So if I refresh this window right here, you'll see that it's showing the explosion part of the animation. So I'll switch that out to the flying part of the animation right here. So now I save this and when I refresh, the ship is animating with the flying sequence. So moving the mouse is super simple. If we look at the documentation for the Stage class, we can see that it keeps track of the mouse position within the stage. It has a mouseX and a mouseY property.
So all we need to do to have the ship follow the mouse is add a couple of lines to this ship function. Now I am going to save this and refresh and you'll see that the ship moves with the mouse. If you want to you can go ahead and delete the part of the code that centers the ship. So let's save that and refresh and you can see that the ship follows the mouse. Now if you want to get rid of the cursor, the easies way to do that is with CSS. So I am going to open up the CSS file and just add a line that sets the cursor to nothing.
So cursor none, save it, and when I refresh while the cursor is on the stage, it's not going to show up. As soon as the cursor leaves the stage it'll show up again. So let's add a bit of friction to the mouse movement so that the ship moves towards the mouse at a fraction of the speed. To do that we're going to need to set up two variables that keep track of the difference between the position of the mouse and the ship. We'll do that inside the Ticker function. So now we can move the ship towards the position of the mouse, but not at full speed, at a fraction of the distance.
So let's see how that works, I am going to save this and when I refresh, you'll see that the ship is moving a little bit slower. Can't really tell because the mouse is hidden, so let's go ahead and show the mouse again. So now we can see that the mouse and the ship don't move at the same speed. The ship moves towards the mouse at a fraction of the speed. We can control that speed by changing this divisor. So let's go ahead and change it to 20 and you'll see that it will move a lot slower. I am refreshing and now ship moves but a lot slower than it used to. So let's say that we wanted the ship to orient itself to the mouse position as it follows the mouse.
Now this is going to return a number in radians, because EaselJS wants the rotation to be in degrees, we're going to multiply it by 180 and divide it by Math.PI. So I am going to save this and refresh. So our ship can follow not only the speed of your mouse movement but also the direction of the movement as well, and that can make up for some really super interesting games and interfaces. The information we're learning to gather with our code, it's going to make it easier to build exciting projects with EaselJS.
There are currently no FAQs about EaselJS First Look.
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.