First step: Generate ground in the game scene
First, you need to use the bitmap class to generate the ground effect of the characters in the game, using the following game image:
With a slice width of 75px, if you set the width of the game area to 350px (canvas width), clone Five to fill the screen with the following code:
- Clone 5 ground images to create a ground effect on the game area
- For(i =0; i <5; I+ +) {
- var tempblockground = ground. Clone();
- Tempblockground. X = I *;
- Tempblockground. Y = stageheight - blockheight;
- Stage. AddChild(tempblockground);
- }
If you are not familiar with the bitmap class, please refer to the following series of tutorials:
easeljs Processing Pictures
The above code will use a picture and clone 5 times to generate the ground effect in the game scene
Step Two: Create a running effect for the characters in the game
Using the Spritesheet class, you can create a character animation effect using the following image:
You can set the animation effect by following the code:
- Spritesheet =new createjs. Spritesheet({
- "Images": [' Http://cdn.gbtags.com/gblibraryassets/libid108/charactor.png '],
- "Frames": {"height":$,"Count":ten,"width":
- "Animations": { run: [0,9]}
- });
- Character =new createjs. Sprite(spritesheet);
The above code uses Spritesheet to generate animated character related sprites and set the X, Y axis position of the relevant game characters
Use the play () method to play an animated effect and use ticker to control the animation frame
Step three: Add keyboard direction control for game characters
This section learns to add a keyboard to control the character effect, the relevant code is as follows:
- function handlekeydown(e) {
- Switch (e. KeyCode) {
- case keycode_left:
- case : //A
- MoveLeft = true;
- Character. Play();
- break;
- case keycode_right:
- case : //D
- MoveRight = true;
- Character. Play();
- break;
- }
- }
- function handlekeyup(e) {
- Switch (e. KeyCode) {
- case keycode_left:
- case : //A
- MoveLeft = false;
- Character. gotoAndStop(0);
- break;
- case keycode_right:
- case : //D
- MoveRight = false;
- Character. gotoAndStop(0);
- break;
- }
- }
The above two methods define the related processing of keyboard KeyDown and KeyUp, when the user presses the specified key, for example, A,d or the left and right arrow keys, the player's playback method is called, and when the user releases the specified key, the terminating animation method is called gotoAndStop (0)
Fourth step: Control the jumping of the game characters
To control the character jumps, you need to add one of the following methods:
- function Jump() {
- If(isjumping = =false) {
- Yvel =-;
- Isjumping =true;
- }
- }
This method will control the movement speed of the game characters on the Y axis, and add the following processing to the Tick method:
- function tick(event) {
- ...
- Control jumps
- If(isjumping) {
- Yvel + = gravity;
- Character. Y + = yvel;
- If(character. Y > characterground) {
- Character. Y = characterground;
- Yvel =0;
- Isjumping =false;
- }
- }
- Stage. Update();
- }
The above method controls the gravity effect of the character and controls the position of the person's final landing.
Finally, you need to bind the W key and the UP arrow of the keyboard to the jump action, as follows:
- function Handlekeydown(e) {
- Switch(e. KeyCode) {
- Case keycode_up:
- Case87://W
- Jump();
- Break;
- Case keycode_left:
- Case65://A
- MoveLeft =true;
- Character. Play();
- Break;
- Case keycode_right:
- Case68://D
- MoveRight =true;
- Character. Play();
- Break;
- }
- }
For a detailed code explanation and interactive online demo, please read the following courses:
Learn to use the Easeljs class library to control the movement and jumping of game characters
Learn to use the Easeljs class library to control the movement and jumping of HTML5 game characters