为什么这个对象会消失
Why does this object disappear?
>OK我是编码新手,并尝试制作一个小乒乓球克隆。不知何故,当这个对象碰到画布元素的底部时,它会消失。
这是我的代码:
var padle = {
x: 0,
y: 150,
w: 20,
h: 50,
// Add vx and vy properties here:
vx: 0,
vy: 100,
ax: 0,
ay: 0,
color: "#FAFAFA",
draw: function() {
ctx.beginPath();
ctx.fillRect(this.x, this.y, this.w, this.h );
ctx.fillStyle = this.color;
ctx.fill();
},
update: function() {
// Update the particle's position
this.vx += this.ax / FPS;
this.vy += this.ay / FPS;
this.x += this.vx / FPS;
this.y += this.vy / FPS;
if ( this.x < 0 ) {
this.x = 0;
this.vx = -this.vx;
}
if ( this.x > canvas.width ) {
this.x = canvas.width;
this.vx = -this.vx;
}
if (this.y < 0 ) {
this.y = 0;
this.vy = -this.vy;
}
if ( (this.y + this.h)> canvas.height ) {
this.y = canvas.height;
this.vy = -this.vy;
}
}
};`
怎么了?我真的不明白。
你的陈述
if ( (this.y + this.h)> canvas.height ) {
this.y = canvas.height;
this.vy = -this.vy;
}
将this.y
设置为 canvas.height
,而它应该真正将其设置为 canvas.height - this.h
。 正在发生的事情是,您告诉项目"如果您的下边缘在画布下方,则将上边缘设置为正好在画布的底部",并导致此代码在您的循环中每次都进行评估,这就是为什么它似乎"消失"的原因。 它卡在画布底部下方。
相关文章:
- 用鼠标单击对象,使画布对象消失
- 当对象从服务器发送到客户端时,JS原型方法消失了
- 为什么这个对象会消失
- Javascript:正在消失的对象属性
- 对象在Ember.js中保存时从视图中消失
- 如何使JavaScript对象在单击页面其余部分时消失
- 对象中的 JS 函数消失
- javascript生成的对象在出现后立即消失
- 如何单击未完全覆盖背景的对象,并在悬停时消失,同时通过窗口对象退出元素
- 在WebGL中渲染多网格对象时,背景(画布)颜色将消失
- 从一个对象悬停到另一个对象,而第二个DOM对象不会立即消失
- 对象上的流体和动态动画出现和消失(HTML/JS/CSS)
- 三.js中消失的线对象
- Javascript: in ff in history对象自定义函数消失
- 如何点击一个没有完全覆盖背景并在悬停时消失的对象
- 如何单击消失的对象
- 奇怪的javascript错误-对象消失了
- 使用jquery前置对象并消失
- 对象在angular模板重新编译时消失
- Javascript显示/消失对象