javascript - 为什么我的代码直接进入结束条件?

标签 javascript processing processing.js

所以,我正在处理中制作 2D 射击视频游戏,我希望分数成为您如何进入下一个级别的原因。我将级别放入数组中,并使用几个 Int 元素来定义级别。

但是,一旦分数达到 200(进入下一级别所需的分数),它就会直接获胜,并且不会经过任何级别。

主要代码如下:

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;

final int LEVEL_ONE=0;
final int LEVEL_TWO=1;
final int LEVEL_THREE=2;
final int WON=3;
final int LOST=4;

final String WIN="You Beat the System!";
final String LOSE="Totes, Awkes...";

int gameState;


Level l;

ArrayList <Level> levels = new ArrayList<Level>();

void setup() {
  size(1000, 500);
  textAlign(CENTER);
  loadAssets();



  Level l1 = new Level("LEVEL 1", 5, 0, 3);
  Level l2 = new Level("LEVEL 2", 3, 3, 1);
  Level l3 = new Level("LEVEL 3", 5, 7, 0);

  levels.add(l1);
  levels.add(l2);
  levels.add(l3);


  l = levels.get(0);
}


void winCurrentLevel() {
  levels.remove(l);



  if (levels.size() > 0) l=levels.get(0);
  else winGame();
}



void draw() {
  switch(gameState) {
  case WON:
    showScreen(WIN);
    break;
  case LOST:
    showScreen(LOSE);
    break;
  default:
    l.playLevel();
  }
}   

这是关卡的代码:

class Level {


  final int GAMEPLAY=0;
  final int INTRO=-1;

  int lvlState;
  Background bg;

  ArrayList<PowerUps> powerups;
  ArrayList<Enemy> enemies;
  ArrayList<Bomb> bombs;

  //Score
  int score;
  //Intro
  int introTimer;
  String intro;

  //Enemies and PowerUps
  int numEnem;
  int enemySpawn;
  int numCoffees;


  // Bomb Enemies and Large Powerups
  int numBomb;
  int numLargeCoffees;



  //scrollforce
  int scrollrate;
  int scroll;
  PVector scrollForce;

  Level(String intromessage, int numenemies, int numbombs, int numcol) {

    powerups= new ArrayList<PowerUps>();
    enemies= new ArrayList<Enemy>();  
    bombs= new ArrayList<Bomb>();


    introTimer=60;
    lvlState=INTRO;


    intro=intromessage;

    numEnem=numenemies;
    numBomb=numbombs;

    numCoffees=numcol;
    scrollrate=1;
    scrollForce=new PVector(-4, 0);
    enemySpawn=100;


    initializePowerUps();
    spawnEnemies();
    spawnBombs();
  }


  void playLevel() {
    if (score==200) nextLevel();
    switch(lvlState) {
    case INTRO:
      if (introTimer>0) 
      {
        introTimer--;
        showScreen(intro);
      } else if (introTimer==0) lvlState=GAMEPLAY;
      break;
    case GAMEPLAY:
      gamePlay();
      break;
    }
  }



  void gamePlay() {
    background(255);

    if (scroll<8) scroll=scrollrate;
    if (frameCount%120==0) scrollrate*=1.5;

    p.update();



    for (int i=0; i<enemies.size(); i++) {
      Enemy e=enemies.get(i);
      e.update();
      if (e.pos.x<100) {
        enemies.remove(this);
        p.takeDamage(1);
      }
    }

    for (int i=0; i<bombs.size(); i++) {
      Bomb b=bombs.get(i);
      b.update();
      if (b.pos.x<100) {
        bombs.remove(this);
        b.takeDamage(1);
      }
    }


    if (frameCount%enemySpawn==60) spawnEnemies();
    if (frameCount%enemySpawn==60) spawnBombs();


    //Trying to change to level two
 


    for (int i=0; i<powerups.size(); i++) {
      PowerUps pu=powerups.get(i);
      pu.update();
    }

    drawHealthBar();
    drawScore();
    drawRayBar();
  }


  void spawnBombs() {
    while (bombs.size()<numBomb) {
      bombs.add(new Bomb(new PVector(random(800, 1000), random(50, 450)), new PVector (-random(7, 12), 0), Bomb));
    }
  }


  void spawnEnemies() {
    while (enemies.size()<numEnem) {
      enemies.add(new Enemy(new PVector(random(800, 1000), random(50, 450)), new PVector (-random(5, 10), 0), Grenade));
    }
  }

  void initializePowerUps() {
    spawnCoffee(numCoffees);
  }

  void spawnCoffee(int spawn) {
    for (int i=0; i<2; i++) {
      powerups.add(new Coffee(new PVector(-width, random(height-SmallCoffee.height)), new PVector (-random(5, 10), 0), SmallCoffee));
    }
  }
  void nextLevel() {
   if (gameState == LEVEL_ONE)
   {
   enemies.clear();
   bombs.clear();
   powerups.clear();
   
   p.health=p.MAX_HEALTH;
   gameState = LEVEL_TWO;
   } else if (gameState==LEVEL_TWO) {
   gameState=WON;
   }
   }
}

最佳答案

注意这一行:

if (score==200) nextLevel();

此检查由每个级别执行。因此,一旦达到 200,您就会进入下一个级别,在其中执行此检查,然后转到下一个级别,在其中执行此检查...

相反,您需要为每个级别设置一个不同值。您可以像这样进行检查:

if(level == 1 && score == 200){
   nextLevel();
}
else if(level == 2 && score == 500){
  nextLevel();
}
//...

或者更好的是,您应该将一个值传递到您的 Level 类中,该值告诉您进入下一个级别所需的值。

关于javascript - 为什么我的代码直接进入结束条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146906/

相关文章:

php - 如何将 javascript 变量分配给 session 值

processing.js - 如何将 [1] 添加到数组中?

javascript - 如何仅使用 CSS 修复网页中的第一行和第一列

javascript - 数据表 : disable first next previous last and show/search record when processing

java - 如何将像素转换为灰度?

java - Processing.org/最小 FFT 误差

java - 如何在 P2D 模式下绘制完全透明的像素/点?

javascript - 将图像从 Javascript 显示到Processing.js Canvas

javascript - 如何在processing.js中访问json数据

javascript - Webkit文档