switch case
in
Programming Questions
•
1 year ago
Hi @ all,
my problem is that my switch/case condition add everytime the conditions of case 1.
In my sktech i write down this code (extract):
- int lvl = 1;
- void draw() {
- //spawn enemies - why spawn in case 2 enemies from case 1?
- switch(lvl) {
- case 1:
- enemies.add(new cEnemies(saPx1, saPy1, 35, eL, eLbL));
- enemies.add(new cEnemies(saPx2, saPy2, 35, eL, eLbL));
- enemies.add(new cEnemies(saPx1, saPy2, 35, eL, eLbL));
- enemies.add(new cEnemies(saPx2, saPy1, 35, eL, eLbL));
- break;
- case 2:
- println("case2: " + lvl);
- enemies.add(new cEnemiesLvl2(saPx1, saPy1, 50, 10, 30));
- enemies.add(new cEnemiesLvl2(saPx2, saPy2, 50, 10, 30));
- enemies.add(new cEnemiesLvl2(saPx1, saPy2, 50, 10, 30));
- enemies.add(new cEnemiesLvl2(saPx2, saPy1, 50, 10, 30));
- break;
- }
- }
The variable lvl will count up in the class at line 21:
- //eliminate the enemies
- for (int i = 0; i < shots.size(); i++) {
- //create new vector to store the position x and y of the shots
- cShots shotsTemp = (cShots) shots.get(i);
- posShot = new PVector(shotsTemp.posS.x, shotsTemp.posS.y);
- //from the beginning of the shot to the end of the shot
- if (posShot.x >= posE.x && posShot.x <= posE.x + eS && posShot.y >= posE.y && posShot.y <= posE.y + eS) {
- eL--;
- //enemie livebar length
- eLbL -= 3;
- //remove shot if it hits a target
- shots.remove(shots.get(i));
- if (eL <= 0) {
- enemies.remove(this);
- //stop adding more enemies
- eC--;
- }
- if (eC <= 0) {
- lvl++;
- }
- }
- }
So if the variable lvl is = 1 case 1 run.
If the variable lvl is = 2 println (line 14 - first codesnippet) appears and the result in the console is 2. But instead of running case 2 the case 1 is running.
Do i use the switch/case condition in a wrong way?
Thanks.
1