javascript - 画布中的“OOP”正确方式
问题描述
我正在尝试使用我们不久前收到的糖代码在画布上绘制一些东西。我当然在使用 babel。我有两个问题。首先你可以在下面找到我的代码:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
class Rectangle {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw() {
ctx.beginPath();
ctx.rect(this.x, this.y, 50, 50);
ctx.fillStyle = "#000";
ctx.fill();
ctx.closePath();
};
move() {
document.addEventListener('keydown', event => {
if (event.keyCode === 37) {
ctx.clearRect(0, 0, canvas.width, canvas.height)
this.x--
this.draw();
}
if (event.keyCode === 39) {
ctx.clearRect(0, 0, canvas.width, canvas.height)
this.x++
this.draw();
}
})
}
}
class Ball {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, 10, 0, Math.PI*2);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
};
}
const rect = new Rectangle(130, 50);
const ball = new Ball(60, 40)
setInterval(() => {
rect.draw();
rect.move();
ball.draw();
}, 100);
我做了两节课。一个是长方形,第二个是球。矩形是可以用箭头移动的。当我移动时,球会消失一秒钟,然后再次出现在屏幕上。在这种情况下我做错了什么?游戏流程应该如何正确显示?第二个问题是:我怎样才能让这两个类相互交互?例如,我希望当矩形触摸球时,会出现简单的 console.log。谢谢
解决方案
大部分代码都是你的。因为我更喜欢使用requestAnimationFrame
,所以我为请求添加了一个标识符: let requestId = null;
。
矩形的move()
方法只处理 的值x
,并以key
为属性。
我还编写了frame
逐帧构建动画的函数。
function frame() {
requestId = window.requestAnimationFrame(frame);
ctx.clearRect(0, 0, canvas.width, canvas.height);
rect.move(key);
rect.draw();
ball.draw();
}
更改变量keydown
的值后key
,取消当前动画,并调用frame
函数开始新动画。
我还添加了一个keyup
事件来停止按键上的动画。
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
let key;
let requestId = null;
class Rectangle {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw() {
ctx.beginPath();
ctx.rect(this.x, this.y, 50, 50);
ctx.fillStyle = "#000";
ctx.fill();
//ctx.closePath();
}
move(key) {
if (key == 37) {
this.x--;
}
if (key == 39) {
this.x++;
}
}
}
class Ball {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, 10, 0, Math.PI * 2);
ctx.fillStyle = "#0095DD";
ctx.fill();
//ctx.closePath();
}
}
const rect = new Rectangle(130, 50);
const ball = new Ball(60, 40);
rect.draw();
ball.draw();
function frame() {
requestId = window.requestAnimationFrame(frame);
ctx.clearRect(0, 0, canvas.width, canvas.height);
rect.move(key);
rect.draw();
ball.draw();
}
document.addEventListener("keydown", event => {
if (requestId) {
window.cancelAnimationFrame(requestId);
}
key = event.keyCode;
frame();
});
document.addEventListener("keyup", event => {
if (requestId) {
window.cancelAnimationFrame(requestId);
}
});
canvas{border:1px solid;}
<canvas id="myCanvas"></canvas>
推荐阅读
- python - 使用 Python 检索 MacOS 日历事件?
- java - com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 com.example.myapplication.Orders 类型
- ios - Swift 错误域=NSURLErrorDomain 代码=-999 “已取消”
- angular - 和运算符( && )在 Angular 中不起作用
- c++ - 为什么当我尝试打印地图内容时,我得到的是地址而不是值?在 C++ 中
- php - 如何使用 PHP 将 html 复选框值保存到 MS-SQL 数据库?
- python - 递归下实例变量和实例对象的行为
- java - 无法从下拉列表中选择一个值并单击 java Selenium
- python - 在字典列表/数组中添加元素
- r - ggplot中的轴标签规范