Estoy trabajando en un juego simple (siguiendo un tutorial sobre Zenva Academia) y, aunque he seguido las instrucciones a una T, me parece que no puede tener en mis formas de lona para mostrar en el navegador. Aquí está el código que tengo hasta ahora:
var canvas = document.getElementByID('myCanvas');
var ctx = canvas.getContext('2d');
let screenWidth = 1000;
let screenHeight = 500;
class GameCharacter {
constructor(x, y, width, height, color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
}
var blueSquare = new GameCharacter(
50, 50, 50, 50, "rgb(0, 0, 255)"
);
var rectangle = new GameCharacter(
75, 75, 100, 50, "rgb(0, 255, 0)"
);
var redSquare = new GameCharacter(
100, 50, 50, 50, "rgb(255, 0, 0)"
);
var draw = function() {
ctx.clearRect(0, 0, screenWidth, screenHeight);
ctx.fillStyle = "rgb(0, 0, 255)";
ctx.fillRect(blueSquare.x, blueSquare.y, blueSquare.width, blueSquare.height);
ctx.fillStyle = rectangle.color;
ctx.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
ctx.fillStyle = redSquare.color;
ctx.fillRect(redSquare.x, redSquare.y, redSquare.width, redSquare.height);
}
var step = function() {
draw();
window.requestAnimationFrame(step);
}
canvas {
border: 4px solid green;
background-color: yellow;
}
<canvas id='myCanvas' width='1000' height='500'></canvas>
Estoy todavía bastante nuevo en esto y esta es la primera pregunta que he hecho nunca en un foro. Déjame saber si estoy haciendo algo mal. LOL!
Estoy usando:
OS: Windows 10 Pro de 64 bits
Navegador: Probado tanto Chrome y Microsoft Edge
Editor de código: Sublime Text 3
Un simple error tipográfico - usted ha escrito getElementByID
, mientras que usted debe tener getElementById
. (Esto se muestra inmediatamente en la consola de herramientas de desarrollo de su navegador).
A continuación, tendrá que llamar step()
una vez a las cosas del encuentro.
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
let screenWidth = 1000;
let screenHeight = 500;
class GameCharacter {
constructor(x, y, width, height, color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
}
var blueSquare = new GameCharacter(
50, 50, 50, 50, "rgb(0, 0, 255)"
);
var rectangle = new GameCharacter(
75, 75, 100, 50, "rgb(0, 255, 0)"
);
var redSquare = new GameCharacter(
100, 50, 50, 50, "rgb(255, 0, 0)"
);
var draw = function() {
ctx.clearRect(0, 0, screenWidth, screenHeight);
ctx.fillStyle = "rgb(0, 0, 255)";
ctx.fillRect(blueSquare.x, blueSquare.y, blueSquare.width, blueSquare.height);
ctx.fillStyle = rectangle.color;
ctx.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
ctx.fillStyle = redSquare.color;
ctx.fillRect(redSquare.x, redSquare.y, redSquare.width, redSquare.height);
}
var step = function() {
draw();
window.requestAnimationFrame(step);
}
step();
canvas {
border: 4px solid green;
background-color: yellow;
}
<canvas id='myCanvas' width='1000' height='500'></canvas>