为什么 keydown() 会出现这个错误?
Why am I getting this error for the keydown()?
$(document).ready(function(){
$("#promptButton").click(function(){
$("#openPrompt").hide();
startGame();
});
});
function startGame(){
$(document).ready(function(){
$("#gameboard").append("<div class='snakeHead' id='head'> hi </div>");
var direction;
moveTimer = setInterval(function() {snakeMove()},1000);
function snakeMove(){
function snakeDirection(){
$(".snakeHead").keydown(function(key){
if (key.which === 38){
direction = "up";
return {"margin-top": "-=20px"}
} else if(key.which === 40){
direction = "down"
return {"margin-top": "+=20px"}
} else if(key.which === 37){
direction = "left"
return {"margin-left": "-=20px"}
} else if(key.which === 39){
direction = "right"
return {"margin-left": "+=20px"}
} else {
direction = "right"
return {"margin-left": "+=20px}"
};
});
};
$('.snakeHead').animate({'margin-left':'+=20px'}, 'slow');
}
});
};
我不断收到
SyntaxError: expected expression, got ')' on line 32 col 11.
我的印象是 keydown()
只需要调用事件触发时将调用的函数。我需要另一个表达式来调用它吗?
感谢您的帮助,这是我正在尝试的第一个真正的项目,而且我是自学成才的,所以请尽量让我清楚。谢谢!
您有语法错误:
function startGame(){
$(document).ready(function(){
$("#gameboard").append("<div class='snakeHead' id='head'> hi </div>");
var direction;
moveTimer = setInterval(function() {snakeMove()},1000);
function snakeMove(){
function snakeDirection(){
$(".snakeHead").keydown(function(key){
if (key.which === 38){
direction = "up";
return {"margin-top": "-=20px"}
} else if(key.which === 40){
direction = "down"
return {"margin-top": "+=20px"}
} else if(key.which === 37){
direction = "left"
return {"margin-left": "-=20px"}
} else if(key.which === 39){
direction = "right"
return {"margin-left": "+=20px"}
} else {
direction = "right"
return {"margin-left": "+=20px}" <--- Here --->
};
});
};
$('.snakeHead').animate({'margin-left':'+=20px'}, 'slow');
}
});
};
$(document).ready(function(){
$("#promptButton").click(function(){
$("#openPrompt").hide();
startGame();
});
});
function startGame(){
$(document).ready(function(){
$("#gameboard").append("<div class='snakeHead' id='head'> hi </div>");
var direction;
moveTimer = setInterval(function() {snakeMove()},1000);
function snakeMove(){
function snakeDirection(){
$(".snakeHead").keydown(function(key){
if (key.which === 38){
direction = "up";
return {"margin-top": "-=20px"}
} else if(key.which === 40){
direction = "down"
return {"margin-top": "+=20px"}
} else if(key.which === 37){
direction = "left"
return {"margin-left": "-=20px"}
} else if(key.which === 39){
direction = "right"
return {"margin-left": "+=20px"}
} else {
direction = "right"
return {"margin-left": "+=20px}"
};
});
};
$('.snakeHead').animate({'margin-left':'+=20px'}, 'slow');
}
});
};
我不断收到
SyntaxError: expected expression, got ')' on line 32 col 11.
我的印象是 keydown()
只需要调用事件触发时将调用的函数。我需要另一个表达式来调用它吗?
感谢您的帮助,这是我正在尝试的第一个真正的项目,而且我是自学成才的,所以请尽量让我清楚。谢谢!
您有语法错误:
function startGame(){
$(document).ready(function(){
$("#gameboard").append("<div class='snakeHead' id='head'> hi </div>");
var direction;
moveTimer = setInterval(function() {snakeMove()},1000);
function snakeMove(){
function snakeDirection(){
$(".snakeHead").keydown(function(key){
if (key.which === 38){
direction = "up";
return {"margin-top": "-=20px"}
} else if(key.which === 40){
direction = "down"
return {"margin-top": "+=20px"}
} else if(key.which === 37){
direction = "left"
return {"margin-left": "-=20px"}
} else if(key.which === 39){
direction = "right"
return {"margin-left": "+=20px"}
} else {
direction = "right"
return {"margin-left": "+=20px}" <--- Here --->
};
});
};
$('.snakeHead').animate({'margin-left':'+=20px'}, 'slow');
}
});
};