白桦的天空

第一次的心动,永远的心痛!
  首页  :: 新随笔  :: 联系 :: 管理

js_RPG游戏中人物的移动

Posted on 2005-05-17 10:42  白桦的天空  阅读(1176)  评论(1编辑  收藏  举报

<html>
<body>
<div id="debug"> </div>
<SPAN id=spn1 style="position:absolute;left=0;top=0;OVERFLOW: hidden; WIDTH: 32px; HEIGHT: 48px">
<IMG src="char_10_student_woman.png">
</SPAN>
<script>
//document.onmousemove=new Function("window.status=event.x+','+event.y;");
var target_posx,target_posy,theTimeout,theTimeout2;

document.onmousemove = view_pos;
function view_pos(){
    window.status = event.x +","+event.y;
}

document.onclick = set_pos;
function set_pos(){

    target_posx = parseInt(event.x - 16);
    target_posy = parseInt(event.y - 48);
   
    clearTimeout(theTimeout);
    move();
}

function move(){
    var bc = 4;
    var a_bc = 0;
    var b_bc = 0;
    var a = 0;
    var b = 0;
    var c = 0;
   
    now_posx = parseInt((spn1.style.left).substr(0,(spn1.style.left).length-2));
    now_posy = parseInt((spn1.style.top).substr(0,(spn1.style.top).length-2));

    a = Math.abs(target_posx-now_posx);
    b = Math.abs(target_posy-now_posy);
   
    c = Math.ceil(Math.sqrt(a*a+b*b))
   // document.write("a is"+a+"<br>b is"+b+"<br>c is "+c);
    a_bc = (a/c)*bc;
    b_bc = (b/c)*bc;
   

   
   
   


    //document.write(a_bc+"<br>"+b_bc);
    //调定角色方向
    var dir = set_dir(now_posx,now_posy,target_posx,target_posy);
    spn1.scrollTop = (dir-1) * 48;


    debug.innerHTML = "角色移动,两点走直线<br>x边长:"+a+"<br>y边长"+b+"<br>斜边长:"+c+"<br>X步长 is"+a_bc+"<br>Y步长 is"+b_bc+"<br>方向:"+dir+"<br>now_posx:"+now_posx+"<br>now_posy"+now_posy+"<br>target_posx"+target_posx+"<br>target_posy"+target_posy;
   
    if(now_posx != target_posx){
       
        if(now_posx > target_posx){
            now_posx -= a_bc;
            if(now_posx <= target_posx)
                now_posx = target_posx;
            spn1.style.left = now_posx;
        }else{
            now_posx += a_bc;
            if(now_posx >= target_posx)
                now_posx = target_posx;
            spn1.style.left = now_posx;
        }
    }

    if(now_posy != target_posy){
        if(now_posy > target_posy){
            now_posy -= b_bc;
            if(now_posy <= target_posy)
                now_posy = target_posy;
            spn1.style.top = now_posy;
        }else{
            now_posy += b_bc;
            if(now_posy >= target_posy)
                now_posy = target_posy;
            spn1.style.top = now_posy;
        }
    }

   
    if(isNaN(a_bc) && isNaN(b_bc)){
        clearTimeout(theTimeout2);
        theTimeout2 = 0;
        spn1.scrollLeft = 0;
        return;
    }else{
        if(theTimeout2 == 0){
            theTimeout2 = setTimeout("show_a();",100);
        }
    }

    theTimeout = setTimeout("move();",100);

}


//调定角色方向
function set_dir(now_posx,now_posy,target_posx,target_posy){

   
   
    if(target_posx > now_posx){
       
        if(target_posy > now_posy){
            dir = 2;
        }
        if(target_posy == now_posy){
            dir = 1;
        }
        if(target_posy < now_posy){
            dir = 8;
        }
    }
    if(target_posx < now_posx){
       
        if(target_posy > now_posy){
            dir = 4;
        }
        if(target_posy == now_posy){
            dir = 5;
        }
        if(target_posy < now_posy){
            dir = 6;
        }
    }
    if(target_posx == now_posx){
       
        if(target_posy > now_posy){
            dir = 3;
        }
        if(target_posy < now_posy){
            dir = 7;
        }
    }

    return dir;
}

 


i = 0;
function show_a(){
   
    if(i<160){
        spn1.scrollLeft = i;
    }else{
        spn1.scrollLeft=1;
  }
  }
  </script>
           
        
           

</body>
</html>