为什么我的代码直接达到最终条件?

时间:2015-12-08 02:23:22

标签: javascript processing processing.js

所以,我正在加工中制作2D射击游戏,我希望得分成为你如何进入下一个级别的原因。我有一个数组的级别,并有几个Int元素来定义级别。

然而,一旦得分达到200(想要的分数移动到下一个级别)它直接进入WIN并且它不会通过任何级别。

这是主要代码:

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();
  }
}   

这是Level的代码:

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;
   }
   }
}

1 个答案:

答案 0 :(得分:1)

注意这一行:

az

此检查由每个级别执行。因此,只要您到达if (score==200) nextLevel(); ,就会进入下一级别,执行此项检查,然后转到下一级,执行此检查...

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

200

或者更好的是,您应该将值传递到if(level == 1 && score == 200){ nextLevel(); } else if(level == 2 && score == 500){ nextLevel(); } //... 类,告诉您进入下一级所需的值。