


Javascript game development: 'Three Kingdoms Cao Cao Biography' component development (5) Implementation of movable maps_javascript skills
May 16, 2016 pm 05:42 PM1. Introduction
The content of this lecture is very simple, and everyone will understand it faster. Therefore, I will only analyze the key points, and it is your turn to think about the rest! First of all, I don’t have much experience in game development, because today’s programmers love to use canvas, but I can only make up a few divs. But it doesn’t matter, because the result is still a game. ha! I've been talking a lot lately, I hope you'll forgive me. Next, please look at the code analysis.2. Code explanation
Today I will adjust the order of explanation. Look at the code first and then the pictures.This is the content in slg.js:
var subtractedMargin = 0;
var subtractedMarginL = 0;
var mousedown = 0;
var toright;
var toleft; >
window.onmouseup = function(){
mousedown = 0;
clearInterval(toright);
clearInterval(toleft);
clearInterval(todown);
clearInterval (toup);
}
function mapMove(direction)
{
switch(direction){
case "down":
subtractedMargin -= 15;
$("#ID_IMG_MAP").animate({marginTop: subtractedMargin "px"}, 120);
break;
case "up":
subtractedMargin = 15 ;
$("#ID_IMG_MAP").animate({marginTop: subtractedMargin "px"}, 120);
break;
case "right":
subtractedMarginL -= 15;
$("#ID_IMG_MAP").animate({marginLeft: subtractedMarginL "px"}, 120);
break;
case " left":
subtractedMarginL = 15;
$("#ID_IMG_MAP").animate({marginLeft: subtractedMarginL "px"}, 120);
break;
}
if(subtractedMarginL < -415){
clearInterval(toright);
clearInterval(toleft);
}
if(subtractedMarginL > -20){
clearInterval(toright);
clearInterval(toleft);
}
if(subtractedMargin < -640){
clearInterval(todown);
clearInterval(toup);
}
if(subtractedMargin > -20){
clearInterval(todown);
clearInterval(toup);
}
}
$("body") .ready(function(){
$("#ID_DIV_TORIGHT").mousedown(function(){
mousedown = 1;
if(subtractedMarginL > -415 && mousedown == 1) {
mapMove("right");
toright = setInterval(function(){mapMove("right");}, 120);
}
});
$("#ID_DIV_TOLEFT").mousedown(function(){
mousedown = 1;
if(subtractedMarginL < -20 && mousedown == 1){
mapMove(" left");
toleft = setInterval(function(){mapMove("left");}, 120);
}
});
$("#ID_DIV_TODOWN") .mousedown(function(){
mousedown = 1;
if(subtractedMargin > -640 && mousedown == 1){
mapMove("down");
todown = setInterval (function(){mapMove("down");}, 120);
}
});
$("#ID_DIV_TOUP").mousedown(function(){
mousedown = 1;
if(subtractedMargin < -20 && mousedown == 1){
mapMove("up");
toup = setInterval(function(){mapMove("up" );}, 120);
}
});
});
When you see the title of this article, you will definitely not understand the meaning of the title because it is too abstract . Now let me explain: Movable means that you can move in all directions. A map is a map. It can be a world map, a Chinese map, or a game map. . . It has to be a picture anyway. I have to say a few more words here, but it doesn’t matter, because it is much better to understand the situation than not knowing the true face of Mount Lu.
Next I will publish the html code again, because this time the html code is very important:

現(xiàn)在大家可以對(duì)照兩段代碼看,值得注意得是有一點(diǎn):在html代碼中,我把一張圖片放入了div,這時(shí)div原本是不會(huì)遮蓋住圖片的,但當(dāng)你加上overflow: hidden;時(shí)就可以遮蓋住圖片了。
在這個(gè)程序中為了點(diǎn)擊邊框附近就能使地圖移動(dòng),我用了四個(gè)div壓住圖片四邊,當(dāng)邊框附近被點(diǎn)擊時(shí),就等于點(diǎn)了div,點(diǎn)了div就會(huì)調(diào)動(dòng)相應(yīng)函數(shù)。這樣做雖然技術(shù)會(huì)有點(diǎn)差,不過也是一個(gè)好辦法。
另一點(diǎn)值得注意得是:當(dāng)點(diǎn)擊邊框附近調(diào)用函數(shù)時(shí),我用了一個(gè)很特別的方法讓地圖一直移動(dòng),除非你松開鼠標(biāo)。這個(gè)方法就是用setInterval()等待幾秒后又移動(dòng),setInterval()又是個(gè)不停循環(huán)的東東,于是就一直調(diào)用下去,當(dāng)松開鼠標(biāo)時(shí),就用clearInterval();讓setInterval()停下,于是,這個(gè)環(huán)節(jié)搞定了?。?!哈哈哈。
對(duì)了,不忘把圖片給大家,圖片有點(diǎn)大。。。
?
三、演示效果
先看演示圖片:然后是:
?
演示地址:
注意:進(jìn)入demo后點(diǎn)擊圖片邊緣便可以移動(dòng)地圖。
希望大家多支持!謝謝!
“用Javascript開發(fā)《三國志曹操傳》”系列文章差不多到此就該結(jié)束了,下次我們來研究研究html5游戲開發(fā)。相信那更是智慧的挑戰(zhàn)。敬請(qǐng)期待!---Yorhom Wang

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java and JavaScript are different programming languages, each suitable for different application scenarios. Java is used for large enterprise and mobile application development, while JavaScript is mainly used for web page development.

The following points should be noted when processing dates and time in JavaScript: 1. There are many ways to create Date objects. It is recommended to use ISO format strings to ensure compatibility; 2. Get and set time information can be obtained and set methods, and note that the month starts from 0; 3. Manually formatting dates requires strings, and third-party libraries can also be used; 4. It is recommended to use libraries that support time zones, such as Luxon. Mastering these key points can effectively avoid common mistakes.

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScriptispreferredforwebdevelopment,whileJavaisbetterforlarge-scalebackendsystemsandAndroidapps.1)JavaScriptexcelsincreatinginteractivewebexperienceswithitsdynamicnatureandDOMmanipulation.2)Javaoffersstrongtypingandobject-orientedfeatures,idealfor

Event capture and bubble are two stages of event propagation in DOM. Capture is from the top layer to the target element, and bubble is from the target element to the top layer. 1. Event capture is implemented by setting the useCapture parameter of addEventListener to true; 2. Event bubble is the default behavior, useCapture is set to false or omitted; 3. Event propagation can be used to prevent event propagation; 4. Event bubbling supports event delegation to improve dynamic content processing efficiency; 5. Capture can be used to intercept events in advance, such as logging or error processing. Understanding these two phases helps to accurately control the timing and how JavaScript responds to user operations.

JavaScripthassevenfundamentaldatatypes:number,string,boolean,undefined,null,object,andsymbol.1)Numbersuseadouble-precisionformat,usefulforwidevaluerangesbutbecautiouswithfloating-pointarithmetic.2)Stringsareimmutable,useefficientconcatenationmethodsf

If JavaScript applications load slowly and have poor performance, the problem is that the payload is too large. Solutions include: 1. Use code splitting (CodeSplitting), split the large bundle into multiple small files through React.lazy() or build tools, and load it as needed to reduce the first download; 2. Remove unused code (TreeShaking), use the ES6 module mechanism to clear "dead code" to ensure that the introduced libraries support this feature; 3. Compress and merge resource files, enable Gzip/Brotli and Terser to compress JS, reasonably merge files and optimize static resources; 4. Replace heavy-duty dependencies and choose lightweight libraries such as day.js and fetch

The main difference between ES module and CommonJS is the loading method and usage scenario. 1.CommonJS is synchronously loaded, suitable for Node.js server-side environment; 2.ES module is asynchronously loaded, suitable for network environments such as browsers; 3. Syntax, ES module uses import/export and must be located in the top-level scope, while CommonJS uses require/module.exports, which can be called dynamically at runtime; 4.CommonJS is widely used in old versions of Node.js and libraries that rely on it such as Express, while ES modules are suitable for modern front-end frameworks and Node.jsv14; 5. Although it can be mixed, it can easily cause problems.
