4 Replies - 433 Views - Last Post: 03 April 2013 - 07:43 PM Rate Topic: -----

#1 game06  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 21-March 13

changing speed when u reach next level

Posted 03 April 2013 - 02:15 PM

1st for loop is looping though arraylist and if enemy is not dead than it will make enmey move, shoot etc... if its dead than it will remove it and make a new enemy. than add to arraylist.

bottom code check if its level =2 than use 2nd for loop. which loops though arraylist and increse the speed.

the problem is that when u go to level 2 than it will increse the speed of enemies but when they die than speed goes back to normal.

//actionPerformed Method in main
...
for(int i = enemy_store.size(); --i >= 0;){       //loop though arraylist<enemy>
	enemy_class = enemy_store.get(i);
	if(!enemy_class.getDead()){               //if enemy is not died
            //enemey move, shoot method
             ....
        }
	else{           //remove enemy and create another one
	      enemy_store.remove(i);
	      enemy_class = new Enemy(WINDOW_WIDTH+r.nextInt(100), r.nextInt(WINDOW_HEIGHT) ); //create enemy
	      enemy_store.add(enemy_class);                                               //store in array
	  }
}




...
if(tm.getLevel() == 2)  //if level is 2 than increase if all enemies        
{
	for(int i = 0; i < enemy_store.size(); i++){                 //loop though arraylist
	         enemy_class = enemy_store.get(i);
		enemy_class.setDx(enemy_class.getDx()+1); //increase speed of enemy by 1
	}
}
...

This post has been edited by game06: 03 April 2013 - 02:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: changing speed when u reach next level

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1414
  • View blog
  • Posts: 3,136
  • Joined: 05-April 11

Re: changing speed when u reach next level

Posted 03 April 2013 - 02:39 PM

You will need to set dx of the newly created enemeny
	else {           //remove enemy and create another one
	      enemy_store.remove(i);
	      enemy_class = new Enemy(WINDOW_WIDTH+r.nextInt(100), r.nextInt(WINDOW_HEIGHT) ); //create enemy
		  enemy_class.setDx(enemy_class.getDx() + tm.getLevel() - 1);
	      enemy_store.add(enemy_class);                                               //store in array
	  }


Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: changing speed when u reach next level

Posted 03 April 2013 - 02:46 PM

You're going about it the wrong way. Surely you have some sort of update method for Enemy objects. Each Enemy object should adjust its speed by itself in response to the current level either in its update method, or at creation time. i.e. You create an list of enemies at the beginning of each level, so you pass the value of level to the enemy so it can decide what it speed needs to be.

As for your code, we can't see what the enemy's dx is set to initially. Maybe you're taking it from 0 to 1. I don't know.
Was This Post Helpful? 0
  • +
  • -

#4 game06  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 21-March 13

Re: changing speed when u reach next level

Posted 03 April 2013 - 03:22 PM

ah i c. just to make sure i understant this. you are saying is that i should set/change the speed of enemies right after i create them. this seem to be working but i it seem i would have to keep testing for 1, 2, 3 in different classes. with same code. even if i send level variable in enemy calss i still have to test in constructor:

ex:
test for enemy speed. right after when i create enemy

 
enemy_class = new Enemy(WINDOW_WIDTH+r.nextInt(100), r.nextInt(WINDOW_HEIGHT) ); //create enemy
if level one
   change speed
if level two 
    change speed
add to arraylist



les say than i want to change background image in different levels. than i have to test again in level class
if level one
     change bg
if level two
    change bg
if level three
    change bg



etc...

is this normal or i shouldnt do this.

bc i was thinking of making a menthod that changes every thing. that way i only have to test onces.

This post has been edited by game06: 03 April 2013 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: changing speed when u reach next level

Posted 03 April 2013 - 07:43 PM

If you create a set of enemies per stage and make sure that they only exist on that specific stage, then there is only a need to check for the change at creation time, unless you want to change the speed of an ememy based on certain conditions e.g. The enemy gets faster as its life decreases, in which case you would need to continually monitor the value of its life in a method like update().

There are so many ways to go about it. If you have a game with only 10 levels, it might make sense to create your enemies before hand in in List of Lists. You then create a Level class that loads the appropriate List of enemies:

Level level0 = new Level(Player p, masteEnemyList.get(0)) //return the first List of enemies for level 1. 
Level level1 = new Level(Player p, masteEnemyList.get(1)) //return the first List of enemies for level 2. 



Then based on game state you display and update the appropriate Level.


enum GameLevel {LEVEL1, LEVEL2, LEVEL3, ETC....}

GameLevel gameLevel = GameLevel.LEVEL1;

public void gameLoop() {
    private void tick() {
        switch(gameLevel) {
        case LEVEL1:
            level1.update() //handles for level 1. 
            break;
        }
    }

    private void render() {
       switch(gameLevel) {
        case LEVEL1:
            level1.render(g); //paints everything in level 1 
            break;
        } 
    }
}



etc....

If you have a puzzle game with 100 or more levels that are randomly generated, it makes sense to use only one List of enemies and just re-use them for every level. For this case it might make more sense to have a separate method to call to change the speed of an enemy based upon the current level.

public void setLevelSpeed(enum e) {
    //pseudo code.
    if level 1, speed = 5;
    if level 2, speed = 7;
    etc...    
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1