画布清理问题。选择 Canvasteroids 中未清除的对象
我正在使用 html5 画布创建一个新的小行星游戏。一切进展顺利,直到我必须将激光动态地绘制到舞台上。它们绘制不正确(它们应该只有 10 像素长),并且当您在 10 秒内拍摄两次时,会出现旧的激光轨迹。这是网址,因为代码太多,我不想让每个人都通过。
http://marccannon.com/canvasteroids/
理想情况下,激光长度为 10 像素,一旦出现就会消失生命周期结束(1 秒或 33 帧)。它们从不应再在绘制循环中运行它们的数组中获取 shift()
。激光对象类似乎有某种记忆。到目前为止,我已经花了几个小时用激光轨迹制作太空艺术,而不是添加实际的小行星来撞击。请有人帮忙。我正在失去理智。
预先感谢您的帮助。
I am creating a new asteroids game with html5 canvas. It's been going well up to the point where I have to dynamically draw lasers onto the stage. They don't draw correctly (they should only be 10pixels long) and when you shoot twice over 10 seconds apart the old laser trail shows up. Here's the url because there's more code than I care to put everyone through.
http://marccannon.com/canvasteroids/
Ideally the lasers will be 10px long and go away once they're out of life (1sec or 33 frames). They get shift()
out of an array that should no longer be running them in the draw loop. It seems as though there's some kind of memory with the Laser object class. So far I've spent hours making space art with laser trails instead of adding the actual asteroids to hit. Someone please help. I'm losing my sanity.
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,当您在画布上绘图时,您必须始终记住调用
beginPath()
,否则所有moveTo
和lineTo
命令将保留添加并添加到当前路径。The problem is that when you draw on a canvas you must must always remember to call
beginPath()
, otherwise allmoveTo
andlineTo
commands will keep adding and adding to the current path.