在Canvas标签中Game.draw()函数不会运行?

时间:2018-07-11 22:39:56

标签: javascript

percent = input("Enter percent:")
ofNumber = input("Enter number:")

percent2 = percent++0.

answer = percent2 * ofNumber

print(answer)
'use strict';

var Game = {
    canvas: undefined, 
    canvasContext: undefined
};

Game.start = function () {
    Game.canvas = document.getElementById('myCanvas');
    Game.canvasContext = Game.canvas.getContext('2d');
    Game.mainLoop();
    
};
document.addEventListener('DOMContentLoaded', Game.start);

Game.update = function () {
};

Game.draw = function () {
    Game.canvasContext.fillStyle = 'blue';
    Game.canvasContext.fillReact(0, 0, Game.canvas.width, Game.canvas.height);
};

Game.mainLoop = function () {
    Game.update();
    Game.draw();
    window.setTimeout(mainLoop, 1000 / 60);
};

以上代码应将'canvas'的颜色设置为蓝色,但该代码无法运行。函数“ Game.draw()”很可能是问题所在,如果有人愿意花时间帮助我,将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

问题是您有Game.canvasContext.fillReact而不是Game.canvasContext.fillRect

您还希望将Game.mainLoop传递给window.setTimeout而不是像mainLoop那样传递给window.setTimeout(Game.mainLoop, 1000 / 60);。话虽如此,您实际上可能是在寻找window.setInterval()而不是window.setTimeout()-区别在于 .setInterval() 将重复循环,而 {{ 3}} 仅会触发一次。

这是一个更新的示例:

'use strict';

var Game = {
    canvas: undefined, 
    canvasContext: undefined
};

Game.start = function () {
    Game.canvas = document.getElementById('myCanvas');
    Game.canvasContext = Game.canvas.getContext('2d');
    Game.mainLoop();
    
};
document.addEventListener('DOMContentLoaded', Game.start);

Game.update = function () {
};

Game.draw = function () {
    Game.canvasContext.fillStyle = 'blue';
    Game.canvasContext.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
};

Game.mainLoop = function () {
    Game.update();
    Game.draw();
    window.setInterval(Game.mainLoop, 1000 / 60);
};
<!DOCTYPE html>
<html>

<head>
    <title>FlyingSprite</title>
    <script src="FlyingSprite.js"></script>
</head>

<body>
    <div id="gameArea">
        <canvas id="myCanvas" width="800" height="480"></canvas>
    </div>
</body>

</html>