请帮忙,当我运行Window.onload()=dank();
函数时,我收到错误消息,意外的令牌非法。
<html>
<style>
</style>
<body background="background.jpg">
<canvas id="canvas" width="1300" height="630"></canvas>
<script>
var canvas=document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var H=canvas.height;
var W=canvas.width;
var x=10;
var y=15;
var rightPressed=false;
var leftPressed=false;
var upPressed=false;
var downPressed=false;
var heroX=20;
var heroY=20;
//this is where the error is
功能无法正常工作
Window.onload()=dank();
function dank(){
alert("How to play: move your hero using the arrow keys, and collect colored balls for powerups and points!
Get the highest score you can without touching a monster, which makes you start over!
Collect the colored balls for more and better special powerups.");
}
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
这是主要的游戏功能
function draw(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
var heroImage=new Image();
heroImage.onload=function(){
ctx.drawImage(heroImage , heroX, heroY);
}
heroImage.src="hero.jpg";
dope();
}
function keyDownHandler(e) {
if(e.keyCode == 39) {
rightPressed = true;
}
else if(e.keyCode == 37) {
leftPressed = true;
}
else if(e.keyCode == 40) {
downPressed = true;
}
else if(e.keyCode == 38) {
upPressed = true;
}
}
function keyUpHandler(e) {
if(e.keyCode == 39) {
rightPressed = false;
}
else if(e.keyCode == 37) {
leftPressed = false;
}
else if(e.keyCode == 40) {
downPressed = false;
}
else if(e.keyCode == 38) {
upPressed = false;
}
}
function dope(){
if(rightPressed) {
heroX += 7;
}
else if(leftPressed) {
heroX -= 7;
}
else if(upPressed){
heroY -= 7
}
else if(downPressed){
heroY +=7;
}
}
setInterval(draw, 10);
</script>
</body>
</html>
最佳答案
JavaScript字符串不能跨越多行。
关于javascript - 当我运行window.onload函数时,出现意外的非法 token ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34446516/