用Javascript开发《三国志曹操传》-开源讲座(四)-用地图块拼成大地图
http://blog.csdn.net/yorhomwang/article/details/8033922
用Javascript开发《三国志曹操传》-开源讲座(三)-人物对话中,仿打字机输出文字
http://blog.csdn.net/yorhomwang/article/details/8008759
用Javascript开发《三国志曹操传》-开源讲座(二)-让目标人物移动
http://blog.csdn.net/yorhomwang/article/details/8007871
用Javascript开发《三国志曹操传》-开源讲座(一)-让静态人物动起来
http://blog.csdn.net/yorhomwang/article/details/7984576
这是slg.js里的内容:
var subtractedMargin = 0; var subtractedMarginL = 0; var mousedown = 0; var toright; var toleft; var todown; var toup; 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); } }); });当大家看到本文题目时肯定不大了解题目的含义,因为太抽象了。现在做一下解释:可移动是代表可以向四面八方走动,地图就是地图,可以是世界地图,中国地图,游戏地图。。。反正必须是一张图。在此多说了两句,不过没关系,因为了解情况比不知庐山真面目要好得多。
<html> <head> <title>SLG</title> <link rel="stylesheet" type="text/css" href="./main_looks.css" /> <script type="text/javascript" src="jquery-1.8.0.js"></script> <script type="text/javascript" src="./slg.js"></script> </head> <body> <div id="ID_DIV_SLGPAGE" style="width: 718px; height: 500px; border: 5px solid lightgray; overflow: hidden;"> <img src="./picture/map01.jpg" id="ID_IMG_MAP" style=" margin-top: 0px; margin-left: 0px;" /> </div> <!--<input type="button" value="Down" id="ID_BUTTON_DOWN" /> <input type="button" value="Up" id="ID_BUTTON_UP" /> <input type="button" value="Left" id="ID_BUTTON_LEFT" /> <input type="button" value="Right" id="ID_BUTTON_RIGHT" />--> <div id="ID_DIV_TORIGHT" style="width: 30px; height: 500px; border: 0px solid lightgray; position:absolute; z-index: 9; margin: -508px 0 0 689px;"></div> <div id="ID_DIV_TOLEFT" style="width: 30px; height: 500px; border: 0px solid lightgray; position:absolute; z-index: 9; margin: -508px 0 0 0px;"></div> <div id="ID_DIV_TODOWN" style="width: 718px; height: 30px; border: 0px solid lightgray; position:absolute; z-index: 10; margin: -41px 0 0 0px;"></div> <div id="ID_DIV_TOUP" style="width: 718px; height: 30px; border: 0px solid lightgray; position:absolute; z-index: 10; margin: -508px 0 0 0px;"></div> </body> </html>现在大家可以对照两段代码看,值得注意得是有一点:在html代码中,我把一张图片放入了div,这时div原本是不会遮盖住图片的,但当你加上overflow: hidden;时就可以遮盖住图片了。
图片名称:map01.jpg |
源码下载地址:http://files.cnblogs.com/ducle/moveMap.rar
然后是:
演示地址:http://www.cnblogs.com/yorhom/archive/2012/09/29/2708977.html
注意:进入demo后点击图片边缘便可以移动地图。
希望大家多支持!谢谢!
“用Javascript开发《三国志曹操传》”系列文章差不多到此就该结束了,下次我们来研究研究html5游戏开发。相信那更是智慧的挑战。敬请期待!---Yorhom Wang
----------------------------------------------------------------
欢迎大家转载我的文章。
转载请注明:转自Yorhom's Game Box
欢迎继续关注我的博客