$(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');
}
});
};