


First of all, to make a game playable, it must combine movement and movement. (Haha, everyone thought I was going to talk about composition... But in fact, what I want to talk about today is Javascript) Who doesn't know how to do static things? Because things are static when they are born (unless you use gif animation), no processing is required to achieve static status. Then I will tell you how to use Javascript to turn static pictures into dynamic pictures.
1. Picture preparation
fight01.pngfight02.pngfight03.pngfight04.png03.png02.png01.png First of all, I found some games from the classic game "Three Kingdoms" Materials from "The Biography of Cao Cao" (these are pictures of Wei general Pound). Below I'm going to use these still images to demonstrate how to turn stillness into movement. If you want to demonstrate the code, please download the above picture. The picture name corresponds to the column below.
2. Code explanation
First look at the following javascript code:var picSub = 0;
var time = 150; //Time interval (milliseconds)
var pic1 = "./01.png";
var pic2 = "./02.png";
var pic3 = "./03.png";
var pic4 = "./01.png";
var picArr = [pic1 , pic2, pic3, pic4]; //Define the array and put the variable corresponding to the position of the picture into it
setInterval(changeImg, time); //Make the picture switch according to a certain time
function changeImg()
{
var xElem = document.getElementById("ID_IMG_ROLE");
if(picSub == picArr.length-1){
picSub = 0;
}else{
picSub = 1;
} //Determine whether the array length is exceeded. If so, reset the array subscript to 0 so that it does not exceed
xElem.src = picArr[picSub]; //Switch picture
}
function changeFight()
{
pic1 = "./fight01.png";
pic2 = "./fight02. png";
pic3 = "./fight03.png";
pic4 = "./fight04.png";
picArr = [pic1, pic2, pic3, pic4];
setTimeout(reduction, 600);
}
function reduction()
{
pic1 = "./01.png";
pic2 = "./02 .png";
pic3 = "./03.png";
pic4 = "./01.png";
picArr = [pic1, pic2, pic3, pic4];
}
These codes use my favorite array. Of course, the array here is also the core of the entire program. The following is my explanation word for word:
var pic1 = "./01.png";
var pic2 = "./02.png";
var pic3 = "./03.png";
var pic4 = "./01. png";
var picArr = [pic1, pic2, pic3, pic4]; //Define the array and put the variable corresponding to the position of the picture into it
First of all, In the array, I put several variables corresponding to the positions of the pictures. for the following operations.
Look at the code again:
var xElem = document.getElementById("ID_IMG_ROLE");
if(picSub == picArr.length-1){
picSub = 0;
}else{
picSub = 1;
} //Determine whether the array length is exceeded. If so, return the array subscript to 0 so that it does not exceed
xElem.src = picArr[picSub]; // Switch pictures
Here, use the if...else statement to determine whether the array subscript exceeds the array length. If it exceeds, the subscript will be returned to 0. Then take out the image position corresponding to the subscript in the array and assign it to the src attribute in the img tag with the id attribute ID_IMG_ROLE. This will allow the picture to keep changing. So at this point just give him a place to call the function and you're done! In order to make the picture display not instantaneous, we need to give it a waiting number of seconds, and then display the next picture after waiting. So I used the following code to make function calls:
var time = 150; //Time interval (milliseconds)
setInterval(changeImg, time); //Make the picture switch at a certain time
This will make the picture move . I have also added a function here: when you press the left mouse button in the green border, the characters inside will attack. The principle is also very simple. Let's study it slowly!
For everyone’s convenience in testing, I put all the code including html below for everyone to download:
I provide the download code:
3. Demonstration effect
The first is:
Then:
Demo location:
4. Postscript
After reading this article, you must have a preliminary understanding of dynamic characters using Javascript.
In the future, everyone can use their imagination to their fullest and use this method to create beautiful dynamic games.
Of course, these are not the only mysteries of the program, and it is not easy to figure it out! I will tell you about other Javascript game development technologies in the future. Hope you all like it.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
