


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

JavaScriptcommentsareessentialformaintaining,reading,andguidingcodeexecution.1)Single-linecommentsareusedforquickexplanations.2)Multi-linecommentsexplaincomplexlogicorprovidedetaileddocumentation.3)Inlinecommentsclarifyspecificpartsofcode.Bestpractic

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

Java and JavaScript are different programming languages. 1.Java is a statically typed and compiled language, suitable for enterprise applications and large systems. 2. JavaScript is a dynamic type and interpreted language, mainly used for web interaction and front-end development.
