即使已被调用,函数也不做任何事情

时间:2014-06-06 22:45:15

标签: javascript jquery function

我在这里调用了一个函数

function gameLoop(){

 xpos = Math.min(Math.max(xpos + xspeed,minx),maxx);
 ypos = Math.min(Math.max(ypos + yspeed,miny),maxy);

 gaurd();

 document.getElementById('character').style.left = xpos;
 document.getElementById('character').style.top = ypos;

 if (upPressed == 1)
   yspeed = Math.max(yspeed - 1,-1*maxSpeed);
 if (downPressed == 1)
   yspeed = Math.min(yspeed + 1,1*maxSpeed)
 if (rightPressed == 1)
   xspeed = Math.min(xspeed + 1,1*maxSpeed);
 if (leftPressed == 1)
   xspeed = Math.max(xspeed - 1,-1*maxSpeed);

 if (upPressed == 0 && downPressed == 0)
   slowDownY();
 if (leftPressed == 0 && rightPressed == 0)
   slowDownX();

 setTimeout("gameLoop()",10);

}

这是gaurd()位,但是当我运行代码时,它并没有调用它。

gaurd是

function gaurd() {


alert("getPricing just got called");
$( "#gaurd" ).hide;
$( "#gaurd1" ).hide;
$( "#gaurd2" ).hide;
var gaurdPos = Math.floor(Math.random()*4);

if  (gaurdPos = 1 ) 
    $( "#gaurd" ).show;


if  (gaurdPos = 2) 
    $( "#gaurd1" ).show;


if  (gaurdPos = 3) 
    $( "#gaurd2" ).show;

}

有人知道它为什么不打电话吗?

我是一个完全白痴,谢谢ballabin建议。我曾在其他地方写过一个gaurd函数,当我移动它时它没有删除它。

2 个答案:

答案 0 :(得分:1)

gameLoop()首先引用一堆未定义的变量:

xpos = Math.min(Math.max(xpos + xspeed,minx),maxx);
ypos = Math.min(Math.max(ypos + yspeed,miny),maxy);

即xpos,xspeed,minx,maxx,ypos,yspeed,miny,maxy。

答案 1 :(得分:0)

这可能是因为您没有使用()执行您的功能。

更改

foo.show

foo.show()

看看它是否有效。与hide相同。