Learn to use the Easeljs class library to control the movement and jumping of HTML5 game characters

Source: Internet
Author: User

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:

  1. Clone 5 ground images to create a ground effect on the game area
  2. For(i =0; i <5; I+ +) {
  3. var tempblockground = ground. Clone();
  4. Tempblockground. X = I *;
  5. Tempblockground. Y = stageheight - blockheight;
  6. Stage. AddChild(tempblockground);
  7. }

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:

  1. Spritesheet =new createjs. Spritesheet({
  2. "Images": [' Http://cdn.gbtags.com/gblibraryassets/libid108/charactor.png '],
  3. "Frames": {"height":$,"Count":ten,"width":
  4. "Animations": { run: [0,9]}
  5. });
  6. 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:

  1. function handlekeydown(e) {
  2. Switch (e. KeyCode) {
  3. case keycode_left:
  4. case : //A
  5. MoveLeft = true;
  6. Character. Play();
  7. break;
  8. case keycode_right:
  9. case : //D
  10. MoveRight = true;
  11. Character. Play();
  12. break;
  13. }
  14. }
  15. function handlekeyup(e) {
  16. Switch (e. KeyCode) {
  17. case keycode_left:
  18. case : //A
  19. MoveLeft = false;
  20. Character. gotoAndStop(0);
  21. break;
  22. case keycode_right:
  23. case : //D
  24. MoveRight = false;
  25. Character. gotoAndStop(0);
  26. break;
  27. }
  28. }

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:

    1. function Jump() {
    2. If(isjumping = =false) {
    3. Yvel =-;
    4. Isjumping =true;
    5. }
    6. }

This method will control the movement speed of the game characters on the Y axis, and add the following processing to the Tick method:

  1. function tick(event) {
  2. ...
  3. Control jumps
  4. If(isjumping) {
  5. Yvel + = gravity;
  6. Character. Y + = yvel;
  7. If(character. Y > characterground) {
  8. Character. Y = characterground;
  9. Yvel =0;
  10. Isjumping =false;
  11. }
  12. }
  13. Stage. Update();
  14. }

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:

  1. function Handlekeydown(e) {
  2. Switch(e. KeyCode) {
  3. Case keycode_up:
  4. Case87://W
  5. Jump();
  6. Break;
  7. Case keycode_left:
  8. Case65://A
  9. MoveLeft =true;
  10. Character. Play();
  11. Break;
  12. Case keycode_right:
  13. Case68://D
  14. MoveRight =true;
  15. Character. Play();
  16. Break;
  17. }
  18. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.