为什么我的代码不工作时,t类似于MDN?(OOP)
Why is my code not working when t is similar to MDN? (OOP)
这是MDN的球速度代码:
var ball = {
x: 100,
y: 100,
vx: 5,
vy: 2,
radius: 25,
color: 'blue',
draw: function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, true);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
};
function draw() {
ctx.clearRect(0,0, canvas.width, canvas.height);
ball.draw();
ball.x += ball.vx;
ball.y += ball.vy;
raf = window.requestAnimationFrame(draw);
};
canvas.addEventListener('mouseover', function(e){
raf = window.requestAnimationFrame(draw);
});
ball.draw();
我的代码在这里:
var Ball = function(x, y, vx, vy, r, color) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.radius = r;
this.color = color;
this.draw = function() {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
}
};
function movement() {
pingPong.draw();
pingPong.x += pingPong.vx;
pingPong.y += pingPong.vy;
raf = window.requestAnimationFrame(movement);
};
canvas.addEventListener('mouseover', function(e){
raf = window.requestAnimationFrame(movement);
});
var pingPong = new Ball(100, 100, 5, 1, 15, 'black');
pingPong.draw();
我不明白为什么我的球没有被重画。对我来说,它们看起来完全一样,我已经console.log了我的x和y坐标,它们正在更新。谁能告诉我为什么我的坏了?
MDN's
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, true);
ctx.closePath();
ctx.fillStyle = this.color;
你ctx.arc(x, y, r, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fillStyle = color;
在你的代码中,你没有加上this.
,这可能会使你的对象没有颜色,并被放置在屏幕之外。
相关文章:
- Javascript日期格式类似于ISO,但本地
- toBoolean方法类似于toString
- 州和城市选择框类似于国家细分页面
- 通过传递图像的URL通过javascript读取/处理图像-类似于PHP中的file_get_contents
- 在BootStrap菜单栏中为所选项目设置背景,类似于BootStrap中的父导航选项
- 检测演示者工具的事件,类似于onmousedown
- 类似于Prism的Knockout js框架
- 创建类似于Google analytics的分析地图
- 敲除js变量设置类似于调用函数
- 如何处理javascript中的循环引用,类似于Excel提供迭代限制的方式
- 用null填充稀疏数组(类似于压缩两个数组)
- jQuery模态弹出的行为类似于确认和警报对话框
- 如何垂直淡出文本,类似于Amazon'的产品说明
- 如何在JavaScript中创建类似于something.function()的函数
- 使Dockerfile VOLUME的行为类似于docker compose volumes
- TypeScript代码类似于揭示模块模式结构
- E4X中是否有一种方法可以将基元数据类型的XMLList转换为类似于Array.join()的Array
- 有可能对对象中的所有键进行原子更新吗?类似于阵列拼接的东西
- tinyMCE删除背景图像:“"类似于magento的url的内容
- 为什么我的代码不工作时,t类似于MDN?(OOP)