gpt4 book ai didi

javascript - 功能没有出现在浏览器中

转载 作者:行者123 更新时间:2023-11-30 10:59:14 24 4
gpt4 key购买 nike

我正在尝试从 w3 网站复制该游戏。我的 Canvas 显示,但自从我添加了移动功能后,我的对象 (var myGamePiece) 又名红色方 block ,就停止显示了。请随时检查。我检查了 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>

最佳答案

您编写了多个 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>

代码笔:https://codepen.io/ashfaq_haq/pen/MWWvKWq?editors=1000

关于javascript - 功能没有出现在浏览器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58590198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com