<html>
<body>
<div id="debug"> </div>
<SPAN id=spn1 style="position:absolute;left=0;top=0;OVERFLOW: hidden; WIDTH: 32px; HEIGHT: 64px">
<IMG src="http://www.jiuyue.org/dreamland/rpg/unit/u_boy1.gif">
</SPAN>
<script>
//document.onmousemove=new Function("window.status=event.x+','+event.y;");
var target_posx=1,target_posy=1,theTimeout=0,theTimeout2=0;
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 - 64);
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;

这篇博客通过JavaScript代码展示了如何创建一个人物行走的移动效果。通过监听鼠标点击和移动事件,确定目标位置,并使用数学计算调整角色朝向和步长,使角色沿直线向目标移动。同时,代码还包括了角色自动随机移动的实现。
最低0.47元/天 解锁文章
455

被折叠的 条评论
为什么被折叠?



