You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Space Shooter游戏激光异常:仅1道玩家激光、无敌方激光

问题定位与修复方案

嘿,我仔细扒了你的代码,找到了几个直接导致激光显示异常的关键问题:

1. 最致命的错误:Laser构造函数参数顺序完全颠倒

你的Laser类构造方法签名是这样的:

public Laser(float movementSpeed, float xPosition, float yPosition, float width, float height, TextureRegion textureRegion)

参数顺序是 移动速度 → X坐标 → Y坐标 → 宽度 → 高度 → 纹理

但你在PlayerShip和EnemyShip的fireLaser方法里,完全搞反了参数顺序!

修复PlayerShip的fireLaser

原来的错误调用:

laser[0] = new Laser(xPosition + width * 0.07f, yPosition + height * 0.45f, laserHeight, laserWidth , laserMovementSpeed, laserTextureRegion);

修正后(同时修正了宽高传反的问题):

laser[0] = new Laser(laserMovementSpeed, 
                     xPosition + width * 0.07f, 
                     yPosition + height * 0.45f, 
                     laserWidth, 
                     laserHeight, 
                     laserTextureRegion);

laser[1]做同样的修正。

修复EnemyShip的fireLaser

原来的错误调用:

laser[0] = new Laser(xPosition + width * 0.18f, yPosition - laserHeight, laserWidth, laserHeight, laserMovementSpeed, laserTextureRegion);

修正后:

laser[0] = new Laser(laserMovementSpeed, 
                     xPosition + width * 0.18f, 
                     yPosition - laserHeight, 
                     laserWidth, 
                     laserHeight, 
                     laserTextureRegion);

laser[1]同理修正。

这个错误直接导致:

  • 玩家激光的移动速度被设成了X坐标值(超大数值),刚创建就瞬间飞出屏幕被移除,所以你只看到1道侥幸没飞太快的激光
  • 敌方激光的移动速度也是X坐标值,加上你在render里让敌方激光向下移动,直接瞬间飞出屏幕底部被移除,所以完全看不到

2. Laser构造里的多余Y偏移

看你的Laser构造函数里有一行:

this.yPosition = yPosition+33;

这个固定+33的偏移在你的128单位高度的世界里,会让激光的初始位置严重偏离预期(比如玩家激光直接飞到屏幕外),如果这不是你故意加的调试代码,赶紧删掉,改成:

this.yPosition = yPosition;

3. 敌方发射间隔的验证

你给EnemyShip设置的timeBetweenShoots是0.8f,意味着每0.8秒才能发射一次激光。测试的时候可以先把这个值调小(比如改成0.2f),确认敌方激光能正常显示后,再改回你想要的间隔。

修复后的预期效果

做完上面的修改后,玩家飞船应该能正常发射2道激光,敌方飞船也会按间隔发射2道激光,所有激光都会按你设定的速度移动,超出屏幕后被正确移除。

内容的提问来源于stack exchange,提问作者jaxon9911

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:13:14