我创建了一个函数名称isMoving(),我使用了一个带有方向作为参数的动画Jquery函数。在下面的代码中,我使用isMoving()函数并键入“right / left / top / bottom”以使animate有效。但它没有,它只是忽略它。但是,如果我尝试在isMoving()中的animate函数中键入任何方向,它可以正常工作。关于这里发生了什么的任何想法?
以下是代码:
Player.prototype.isMoving = function(direction, x, y) {
//I want to use animate on player in 4 differents cases. Top, Bottom, Left, Right. So I try to use direction as parameter but and I execute the function (below on the code) it just doesn't work. However If I try to write "left" on the animate just below, it will work.
player.animate({direction: "60px"}, 500, function (){
*Code...*
});
}
Player.prototype.isGoingTo = function (direction, onMove) {
*Code...*
switch (direction) {
case 'left':
newX -= 1;
**this.isMoving('right', newX, newY);**
break;
case 'right' :
newX += 1;
**this.isMoving('left', newX, newY);**
break;
case 'top':
newY -= 1;
**this.isMoving('bottom', newX, newY);**
break;
case 'bottom' :
newY += 1;
**this.isMoving('top', newX, newY);**
break;
}
}
感谢。
答案 0 :(得分:1)
这是因为当{direction: "60px"}
是一个变量时,JS无法以那种方式direction
创建对象。您必须首先使用正确的密钥创建一个对象,并在player.animate
函数中使用它,如下所示:
Player.prototype.isMoving = function(direction, x, y) {
var obj = {};
obj[direction] = '60px';
player.animate(obj, 500, function (){
*Code...*
});
}