所以我正在制作一个游戏,我注意到当我尝试在玩家升级时播放音频时,它会滞后整个游戏一瞬间。public voidtick()
每秒调用 60 次。
而且我已经对其进行了测试,唯一落后于我的游戏的是这门课的音频部分
public void tick() {
if (Game.startPressed == true) {
if (HUD.HEALTH != 0) {
if (Game.gamePaused == false) {
playerScoreKeep++;
playerScoreKeepShield++;
if (playerScoreKeepShield >= 3000) {
playerScoreKeepShield = 0;
handler.addObject(new BasicShield(r.nextInt(Game.WIDTH - 17), r.nextInt(Game.HEIGHT - 17), ID.BasicShield, handler));
}
if (playerScoreKeep >= 1000) {
try { //This is the part that's causing the game to lag spike really bad
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource("/lvl_up.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
playerScoreKeep = 0;
HUD.playerLevel++;
handler.addObject(new BasicHP(r.nextInt(Game.WIDTH - 17), r.nextInt(Game.HEIGHT - 17), ID.BasicHP, handler));
for (int g = 0; g < 2; g++) handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 17), r.nextInt(Game.HEIGHT - 17), ID.BasicEnemy, handler));
if (HUD.playerLevel >= 5) {
advRandomDir = r.nextInt(3);
if (advRandomDir == 1) {
handler.addObject(new AdvancedEnemy(10, r.nextInt(Game.HEIGHT), ID.AdvancedEnemy, handler));
}
else if (advRandomDir == 2) {
handler.addObject(new AdvancedEnemy(r.nextInt(Game.WIDTH), 32, ID.Advancedenemy, handler));
} else {
handler.addObject(new AdvancedEnemy(r.nextInt(Game.WIDTH), 32, ID.AdvancedEnemy, handler));
}
handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 17), r.nextInt(Game.HEIGHT - 17), ID.BasicEnemy, handler));
if (HUD.playerLevel == 8) {
handler.addObject(new ChaoticEnemy(Game.WIDTH/2, Game.HEIGHT/2, ID.ChaoticEnemy, handler));
} else if (HUD.playerLevel == 9) {
ChaoticEnemy.ChaoticEnemy_Bullet_Speed = 25;
for (int i = 0; i < handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);
if (tempObject.getID() == ID.ChaoticEnemy) if (HUD.HEALTH != 0) {
tempObject.velX = 2;
tempObject.velY = 2;
}
}
} else if (HUD.playerLevel == 15) {
ChaoticEnemy.ChaoticEnemy_Bullet_Speed = 20;
for (int i = 0; i < handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);
if (tempObject.getID() == ID.ChaoticEnemy) if (HUD.HEALTH != 0) {
tempObject.velX = 3;
tempObject.velY = 3;
}
}
}
}
}
}
}
}
}