您的位置:无忧脚本
» 秦皇也爱JS的个人空间
» 日志
想做个web游戏 刚做了人物移动 希望大家帮助实现躲障寻路
上一篇 /
下一篇 2007-06-23 16:25:54
查看( 1651 ) /
评论( 27 )
CODE:
<style>
.abs{ position: absolute;}
.0{ width:30px; height:30px; float:left; border:1px solid gray;}
.1{ width:30px; height:30px; float:left; border:1px solid gray;background:gray}
</style>
<body bgcolor="#eeeeee" background="http://www.cpp114.com/cppjob/map.png" style="margin:0px">
<scrīpt>
var mainImg = new Image();
mainImg.src = "http://www.cpp114.com/cppjob/boy.gif";
//document.css = document.createStyleSheet("css");
//document.css.addRule(".1", "color:red")
var map = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1],
[1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]];
document.write("<div style='width:600px;height:600px;'>")
for(var i=0;i<map.length;i++){
for(var j=0;j<map[0].length;j++){
document.write("<div class='" + map[i][j] + "'></div>");
}
}
document.write("</div>")
function movingMan(p){
this.oldPoint = p;
this.nowPoint = p;
this.container = $("container");
this.direction = 0; // 0--下; 1--左; 2--右; 3--上; 4--左下; 5--右下; 6--左上; 7--右上
this.order = 0; //行走顺序
this.count = 0; //行走延时切换图片
this.endPoint;
this.timer;
srcObj = this;
this.shiftImage = function(){
if(arguments[0] == true) this.order = 0;
this.container.scrollTop = 125 * this.direction + (125-75);
this.container.scrollLeft = 125 * this.order + (124-40)/2;
this.order++;
if(this.order == 8) this.order = 0;
}
this.move = function(){
if(Math.abs(srcObj.nowPoint.x - srcObj.endPoint.x) <=1 && Math.abs(srcObj.nowPoint.y-srcObj.endPoint.y)<=1){
window.clearInterval(srcObj.timer);
srcObj.shiftImage(true);
}else if(Math.abs(srcObj.nowPoint.x-srcObj.endPoint.x)<=1){
if(srcObj.nowPoint.y < srcObj.endPoint.y) srcObj.direction = 0;
else srcObj.direction = 3;
}else if(Math.abs(srcObj.nowPoint.y-srcObj.endPoint.y)<=1){
if(srcObj.nowPoint.x < srcObj.endPoint.x) srcObj.direction = 2;
else srcObj.direction = 1;
}else{
if(srcObj.nowPoint.x > srcObj.endPoint.x){
if(srcObj.nowPoint.y < srcObj.endPoint.y) srcObj.direction = 4;
else srcObj.direction = 6;
}else{
if(srcObj.nowPoint.y < srcObj.endPoint.y) srcObj.direction = 5;
else srcObj.direction = 7;
}
}
switch (srcObj.direction){
case 0:{
srcObj.nowPoint.y ++; break;
}
case 1:{
srcObj.nowPoint.x --; break;
}
case 2:{
srcObj.nowPoint.x ++; break;
}
case 3:{
srcObj.nowPoint.y --; break;
}
case 4:{
srcObj.nowPoint.x --; srcObj.nowPoint.y ++; break;
}
case 5:{
srcObj.nowPoint.x ++; srcObj.nowPoint.y ++; break;
}
case 6:{
srcObj.nowPoint.x --; srcObj.nowPoint.y --; break;
}
case 7:{
srcObj.nowPoint.x ++; srcObj.nowPoint.y --; break;
}
}
srcObj.container.style.left = srcObj.nowPoint.x - 20 + "px";
srcObj.container.style.top = srcObj.nowPoint.y - 70 + "px";
srcObj.count++;
if(srcObj.count % 10 == 0){
srcObj.shiftImage();
srcObj.count = 0;
}
}
this.Go = function(p){
this.endPoint = p;
window.clearInterval(this.timer);
this.timer = window.setInterval(srcObj.move, 10);
}
this.shiftImage(true);
}
function Point(){
this.x = arguments[0]?arguments[0]:0;
this.y = arguments[1]?arguments[1]:0;
}
function doMove(e){
//if(event.srcElement.id == "container" || event.srcElement.tagName == "IMG") return;
if(event.srcElement.className != "0") return;
actor.Go(new Point(event.x, event.y));
}
function $(){
return document.getElementById?document.getElementById(arguments[0]):eval(arguments[0]);
}
document.onclick = doMove;
document.onkeydown = function(){
alert(actor.direction + " " + actor.order)
}
var _left = (document.body.clientWidth - 40) / 2;
var _top = document.body.clientHeight / 2 - 70;
document.write("<div id='container' class='abs' style='overflow:hidden;width:40px;height:72px;top:" + _top + "px;left:" + _left + "px;background:transparent'><img src='" + mainImg.src +"'></div>");
var actor = new movingMan(new Point(_left+20, _top+70));
</scrīpt>
</body>看样子地图得用拼的 这样不行
[
本帖最后由 秦皇也爱JS 于 2007-6-16 16:33 编辑 ]
论坛模式
推荐
收藏
分享给好友
推荐到圈子
管理
TAG: