功能未出现在浏览器中

时间:2019-10-28 11:33:26

标签: javascript html

我正在尝试从w3网站复制此游戏。我的画布已显示,但是自从我添加了运动功能以来,我的对象(var myGamePiece)又称为红色正方形,就停止显示。随时检查。我检查了10次是否有错别字或其他内容,但找不到任何内容,代码未显示任何错误。严重不知道到底是怎么回事。我花了几个小时试图使其工作。

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style>
    canvas {
        border: 1px solid #d3d3d3;
        background-color: #f1f1f1;
    }
    </style>
</head>
<body onload="startGame()">

<script>
    
function startGame() {
    myGameArea.start();
}

var myGameArea = {
    canvas: document.createElement("canvas"),
    start : function() {
        this.canvas.width = 600;
        this.canvas.height = 600;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
    }
}

var myGamePiece;
function startGame () {
    myGameArea.start();
    myGamePiece = new component(30, 30, "red", 10, 120);
}


function component(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;
    this.update = function() {
      ctx = myGameArea.context;
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }

    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
}

function updateGameArea() {
    myGameArea.clear();
    myGamePiece.newPos();
    myGamePiece.update();
}

function moveup() {
    myGamePiece.speedY -= 1;
}

function movedown() {
    myGamePiece.speedY += 1;
}

function moveleft() {
    myGamePiece.speedX -= 1;
}

function moveright() {
    myGamePiece.speedX += 1;
}

</script>

<button onclick="moveup()">UP</button>
<button onclick="movedown()">DOWN</button>
<button onclick="moveleft()">LEFT</button>
<button onclick="moveright()">RIGHT</button>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

您已经编写了多个startGame函数。请删除最上面的一个。 同样,主要的问题是您在myGameArea对象中的间隔和清除功能后忘记更新游戏区域。因此代码应如下所示-

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style>
    canvas {
        border: 1px solid #d3d3d3;
        background-color: #f1f1f1;
    }
    </style>
</head>
<body onload="startGame()">

<script>

var myGameArea = {
    canvas: document.createElement("canvas"),
    start : function() {
        this.canvas.width = 600;
        this.canvas.height = 600;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
    },
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

var myGamePiece;
function startGame () {
    myGameArea.start();
    myGamePiece = new component(30, 30, "red", 10, 120);
}


function component(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;
    this.update = function() {
      ctx = myGameArea.context;
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }

    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
}

function updateGameArea() {
    myGameArea.clear();
    myGamePiece.newPos();
    myGamePiece.update();
}

function moveup() {
    myGamePiece.speedY -= 1;
}

function movedown() {
    myGamePiece.speedY += 1;
}

function moveleft() {
    myGamePiece.speedX -= 1;
}

function moveright() {
    myGamePiece.speedX += 1;
}

</script>

<button onclick="moveup()">UP</button>
<button onclick="movedown()">DOWN</button>
<button onclick="moveleft()">LEFT</button>
<button onclick="moveright()">RIGHT</button>

</body>
</html>

Codepen:https://codepen.io/ashfaq_haq/pen/MWWvKWq?editors=1000

答案 1 :(得分:1)

您需要在updateGameAreamoveupmoveDownmoveLeftmoveRight开始和之后致电<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <style> canvas { border: 1px solid #d3d3d3; background-color: #f1f1f1; } </style> </head> <body> <script> var myGameArea = { canvas: document.createElement("canvas"), start : function() { this.canvas.width = 600; this.canvas.height = 600; this.context = this.canvas.getContext("2d"); document.body.insertBefore(this.canvas, document.body.childNodes[0]); } } startGame(); var myGamePiece; function startGame () { myGameArea.start(); myGamePiece = new component(30, 30, "red", 10, 120); updateGameArea(); } function component(width, height, color, x, y) { this.width = width; this.height = height; this.speedX = 0; this.speedY = 0; this.x = x; this.y = y; this.update = function() { ctx = myGameArea.context; ctx.fillStyle = color; ctx.fillRect(this.x, this.y, this.width, this.height); } this.clear = function() { ctx = myGameArea.context; ctx.clearRect(0, 0, 600, 600); } this.newPos = function() { this.x += this.speedX; this.y += this.speedY; } } function updateGameArea() { myGamePiece.clear(); myGamePiece.newPos(); myGamePiece.update(); } function moveup() { myGamePiece.speedY -= 1; updateGameArea(); } function movedown() { myGamePiece.speedY += 1; updateGameArea(); } function moveleft() { myGamePiece.speedX -= 1; updateGameArea(); } function moveright() { myGamePiece.speedX += 1; updateGameArea(); } </script> <button onclick="moveup()">UP</button> <button onclick="movedown()">DOWN</button> <button onclick="moveleft()">LEFT</button> <button onclick="moveright()">RIGHT</button> </body> </html>

public class ApplicationUser : IdentityUser
{
    public string CustomName { get; set; }
}