JS game development, let your static characters move

Source: Internet
Author: User

First, to make a game playable, you must combine Dynamic and Static resources. Haha, everyone thinks I want to talk about the composition... But what I want to talk about today is Javascript. Who will not do static things? Because things are static in a lifetime, unless you use GIF animation), you can complete the static state without any processing. Then I will show you how to use Javascript to convert static images into dynamic images.

I. Image preparation

First, I found some materials from the classic game "The Legend of the Three Kingdoms". These are images of Wei zhipound ). Below I will use these static images to demonstrate how to convert static images into dynamic images. If you want to demonstrate the code, download the above image. The image name corresponds to the following column.

Ii. Code explanation

 
 
  1. Var picSub = 0;
  2. Var time = 150; // time interval (MS)
  3. Var pic1 = "./01.png ";
  4. Var pic2 = "./02.png ";
  5. Var pic3 = "./03.png ";
  6. Var pic4 = "./01.png ";
  7. Var picArr = [pic1, pic2, pic3, pic4]; // defines the array and places the corresponding variables of the image position into it.
  8. SetInterval (changeImg, time); // enables the image to be switched over for a certain period of time
  9. Function changeImg ()
  10. {
  11. Var xElem = document. getElementById ("ID_IMG_ROLE ");
  12. If (picSub = picArr. length-1 ){
  13. PicSub = 0;
  14. } Else {
  15. PicSub + = 1;
  16. } // Determine whether the length of the array is exceeded. If the length is exceeded, the array subscript is set to 0 so that it does not exceed
  17. XElem. src = picArr [picSub]; // switch the image
  18. }
  19. Function changeFight ()
  20. {
  21. Pic1 = "./fight01.png ";
  22. Pic2 = "./fight02.png ";
  23. Pic3 = "./fight03.png ";
  24. Pic4 = "./fight04.png ";
  25. PicArr = [pic1, pic2, pic3, pic4]
  26. SetTimeout (ction, 600 );
  27. }
  28. Function compute ()
  29. {
  30. Pic1 = "./01.png ";
  31. Pic2 = "./02.png ";
  32. Pic3 = "./03.png ";
  33. Pic4 = "./01.png ";
  34. PicArr = [pic1, pic2, pic3, pic4];
  35. }

These Code uses my favorite array. Of course, the array here is also the core of the entire program. Here is my explanation:

 
 
  1. 1 var pic1 = "./01.png ";
  2. 2 var pic2 = "./02.png ";
  3. 3 var pic3 = "./03.png ";
  4. 4 var pic4 = "./01.png ";
  5. 5 var picArr = [pic1, pic2, pic3, pic4]; // defines the array and puts the corresponding variables of the image position into it.

First, I put the variables corresponding to the positions of several images in the array. For the following operations.

Look at the code again:

 
 
  1. Var xElem = document. getElementById ("ID_IMG_ROLE ");
  2. If (picSub = picArr. length-1 ){
  3. PicSub = 0;
  4. } Else {
  5. PicSub + = 1;
  6. } // Determine whether the length of the array is exceeded. If the length is exceeded, the array subscript is set to 0 so that it does not exceed
  7. XElem. src = picArr [picSub]; // switch the image

Here, the if... else statement is used to determine whether the array subscript exceeds the array length. if it exceeds the array length, the subscript is set to 0. Then retrieve the corresponding image location of the subscript in the array and assign it to the src attribute in the img tag with the id attribute ID_IMG_ROLE. In this way, images can be continuously changed. Therefore, you only need to give him a function call! In order to make the image display not an instant, we need to give it a waiting time in seconds, and then display the next image after it is finished. Therefore, I use the following code to call a function:

 
 
  1. Var time = 150; // time interval (MS)
  2. SetInterval (changeImg, time); // enables the image to be switched over for a certain period of time

In this way, the image can be moved. I also added a feature here: When you press any key, the characters in it will attack, and the principle is very simple. Let's study it slowly!

For the convenience of testing, I wrote all the code including html below:

 
 
  1. <Html>
  2. <Head>
  3. <Script language = javascript>
  4. Var picSub = 0;
  5. Var noa = 0;
  6. Var time = 150; // time interval (MS)
  7. Var pic1 = "./01.png ";
  8. Var pic2 = "./02.png ";
  9. Var pic3 = "./03.png ";
  10. Var pic4 = "./01.png ";
  11. Var picArr = [pic1, pic2, pic3, pic4]; // defines the array and places the corresponding variables of the image position into it.
  12. SetInterval (changeImg, time); // enables the image to be switched over for a certain period of time
  13. Function changeImg ()
  14. {
  15. Var xElem = document. getElementById ("ID_IMG_ROLE ");
  16. If (picSub = picArr. length-1 ){
  17. PicSub = 0;
  18. } Else {
  19. PicSub + = 1;
  20. } // Determine whether the length of the array is exceeded. If the length is exceeded, the array subscript is set to 0 so that it does not exceed
  21. XElem. src = picArr [picSub]; // switch the image
  22. /* For (var I = 0; I <2; I ++ ){
  23. SetInterval (function () {changeFight (); noa + = 1 ;}, 2000 );
  24. }*/
  25. }
  26. Function changeFight ()
  27. {
  28. Pic1 = "./fight01.png ";
  29. Pic2 = "./fight02.png ";
  30. Pic3 = "./fight03.png ";
  31. Pic4 = "./fight04.png ";
  32. PicArr = [pic1, pic2, pic3, pic4]
  33. SetTimeout (ction, 600 );
  34. }
  35. Function compute ()
  36. {
  37. Pic1 = "./01.png ";
  38. Pic2 = "./02.png ";
  39. Pic3 = "./03.png ";
  40. Pic4 = "./01.png ";
  41. PicArr = [pic1, pic2, pic3, pic4];
  42. }
  43. </Script>
  44. </Head>
  45. <Body onkeydown = "changeFight ()">
  46. </Body>
  47. </Html>

Iii. demonstration results

Because it is dynamic, it is difficult to show you the demo diagram. But at least on my browser, the operation was successful.

Where I provide download code: http://files.cnblogs.com/ducle/chgpicpangde.rar

The main image sequence is as follows:

If you press any key, the attack sequence is as follows:

Iv. Postscript

After reading this article, you must have a preliminary understanding of Javascript's dynamic characters. In the future, you can make full use of your imagination and use this method to create beautiful Dynamic Games. Of course, the mysteries of the program are not just that. It is not easy to understand it! I will talk about other Javascript game development technologies in the future. I hope you will like it.

Related Article

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.