以下是游戏:link
当您点击其他图块时,角色应该慢慢走到那里。但是如果你在行走时点击另一个瓷砖,那么角色就会变形到那个地方。我无法弄清楚为什么。
if(humanSelected && !moving)
{
moving=true;
var tileSpeed = 200;//ms
var x = $that.attr('x');//destination point
var y = $that.attr('y');
var diffx = humanSelected.x-x;//distance to cover in x-axis
var diffy = humanSelected.y-y;
var dist = Math.sqrt((diffx *diffx )+(diffy *diffy ));//the distance in diagonal(straight line)
$('.man').animate({
right: diffx*20,
bottom: diffy*20
}, tileSpeed*dist, function() {
var mytile = tile(humanSelected.x,humanSelected.y);//get source tile object
$('img.man').remove('');//remove character from previous tile
humanSelected.x=x;humanSelected.y=y;
$that.html('<img src="man.gif" class="man"/>').attr('contain', 'man').css({right:0,bottom:0});//put character on new tile
moving=false;
});
$('.div').mouseup(function(){
var rx = $(this).attr('x');
var ry = $(this).attr('y');
humanSelected = new Object();
humanSelected={
x:rx,
y:ry
};
});
答案 0 :(得分:0)
我解决了。
原来,回调函数中的$that = $(this);
行不再相同。每个mouseup事件都会更改变量。我创建了一个特殊变量,用于在移动期间保存目标tile对象。