如何停止Phaser 3版Slither.io克隆中Bot眼睛的旋转
停止Slither.io克隆项目中Bot眼睛旋转的解决方法
在使用Slither.io克隆项目时,Bot的眼睛出现持续旋转的问题,更换眼睛图片、移除左眼都无法解决,以下是针对提供的eyepair.js和eye.js文件的解决方法:
方法一:将眼睛改为静态精灵(彻底停止动态效果)
这种方法会完全移除眼睛的物理引擎驱动,让眼睛成为跟随头部移动的静态元素:
- 修改
eye.js中的initCircle方法,移除物理引擎启用逻辑:
initCircle: function(circle) { circle.scale.setTo(this.scale); // 移除物理引擎启用及相关代码 this.eyeGroup.add(circle); return circle; }
- 修改
eye.js中的update方法,清空动态逻辑:
update: function() { // 空方法,不再执行任何动态操作 }
- 修改
eyepair.js中的update方法,手动更新眼睛位置以跟随头部:
update: function() { var offset = this.getOffset(); // 更新左眼位置 this.leftEye.whiteCircle.x = this.head.body.x - offset.x; this.leftEye.whiteCircle.y = this.head.body.y - offset.y; this.leftEye.blackCircle.x = this.leftEye.whiteCircle.x; this.leftEye.blackCircle.y = this.leftEye.whiteCircle.y; // 更新右眼位置 this.rightEye.whiteCircle.x = this.head.body.x + offset.x; this.rightEye.whiteCircle.y = this.head.body.y - offset.y; this.rightEye.blackCircle.x = this.rightEye.whiteCircle.x; this.rightEye.blackCircle.y = this.rightEye.whiteCircle.y; }
- 可选:删除
eye.js中与物理约束相关的代码(如lock、dist变量及updateConstraints方法),简化代码结构。
方法二:保留物理引擎但固定眼球位置
如果希望保留物理引擎结构,仅停止眼睛的旋转/移动,可以:
- 修改
eye.js中的update方法,移除眼球的受力逻辑,固定黑眼球位置:
update: function() { // 将黑眼球固定在白眼球中心 this.blackCircle.body.x = this.whiteCircle.body.x; this.blackCircle.body.y = this.whiteCircle.body.y; }
- 修改
eye.js中的updateConstraints方法,将距离约束替换为锁定约束,彻底固定黑眼球:
updateConstraints: function(offset) { // 保留白眼球与头部的锁定约束 if (this.lock) { this.lock.localOffsetB = [ this.game.physics.p2.pxmi(offset[0]), this.game.physics.p2.pxmi(Math.abs(offset[1])) ]; } else { this.lock = this.game.physics.p2.createLockConstraint( this.whiteCircle.body, this.head.body, offset, 0 ); } // 替换距离约束为锁定约束,固定黑眼球在白眼球中心 if (this.dist) { this.game.physics.p2.removeConstraint(this.dist); } this.dist = this.game.physics.p2.createLockConstraint( this.blackCircle.body, this.whiteCircle.body, [0, 0], 0 ); }
内容的提问来源于stack exchange,提问作者rony sen
相关产品推荐
相关产品推荐

