Search code examples
javalibgdxgame-physics

Enemy object doesn't move LibGDX


I have a .txt file

Block 128 0
Block 192 0
Block 256 0
Block 320 0
...
...
...
Enemy 1000 64 500 500
Enemy 1474 64 290 250

and here is my reading method (enemies and list are global variables)

    ArrayList<GameObject> enemies = ArrayList<GameObject>();
    ArrayList<GameObject> list= ArrayList<GameObject>();
        FileHandle file = Gdx.files.internal("data/" + level + ".txt");
                StringTokenizer tokens = new StringTokenizer(file.readString());
                while(tokens.hasMoreTokens()){
                      if(type.equals("Block")){
                         list.add(new Brick(Integer.parseInt(tokens.nextToken()), Integer.parseInt(tokens.nextToken())));
                }else if(type.equals("Enemy")){
                        enemies.add(new Enemy(Float.parseFloat(tokens.nextToken()), Float.parseFloat(tokens.nextToken()), Integer.parseInt(tokens.nextToken()), Integer.parseInt(tokens.nextToken())));
                      }
                 }

here how I draw my enemies (in my draw method)

for(GameObject t : enemies){
            t.draw(batch);
}

and here I set my enemies sprites moving direction

for(GameObject t : enemies){
        int direction = 0;
        for(GameObject t2 : list){
            if(t2 instanceof Brick){
                if(t.hits2(t2.getHitBox()) == 2){
                    direction = 2;
                }else if(t.hits2(t2.getHitBox()) == 3){
                    direction = 1;
                }
            }
        }
        t.update(Gdx.graphics.getDeltaTime(), direction);
        switch(t.hits(player1.getHitBox())){
        case 4:
            SoundManager.kill.play(.03f);
            deleteEnemy.add(t);
            break;
        case 5:
            player1.setPosition(0, 400);
            dead++;
            SoundManager.dead.play(0.06f);
            gameState = 4;
            break;
        }
      break;
    }

Here's my update method

public class Enemy extends GameObject{
       private Rectangle bottom, left, right, top, full;
       ...
       ...
       ...
       ...
      public void update(float delta, int number){
            moveEnemy(delta, number);
      }
       public void moveEnemy(float delta, int number){
              if(sk1 > 0 && test && (number == 1 || number == 0)){
                  if(letRotate){
                      sprite.flip(true, false);
                      letRotate = false;
                  }
                  sk1 -= (speed * delta);
                  full.x -= (speed * delta);
                  top.x -= (speed * delta);
                  left.x -= (speed * delta);
                  right.x -= (speed * delta);
                  sprite.setPosition(full.x, full.y);
               }else{
                    test = false;
                    if((full.x + x) < sk2 && !test && (number == 2 || number == 0)){
                     if(!letRotate){
                           sprite.flip(true, false);
                           letRotate = true;
                     }
                     sk1 += (speed * delta);
                     full.x += (speed * delta);
                     top.x += (speed * delta);
                     left.x += (speed * delta);
                     right.x += (speed * delta);
                     sprite.setPosition(full.x, full.y);
             }else{
                test = true;
          }
    }
}
}

But when I run this code only ONE of my enemies is moving (enemy with rectangles)! What's wrong with my code?enter image description here


Solution

  • I know what's wrong. I just need to remove break after switch(t.hits(player1.getHitBox()))

    for(GameObject t : enemies){
            int direction = 0;
            for(GameObject t2 : list){
                if(t2 instanceof Brick){
                    if(t.hits2(t2.getHitBox()) == 2){
                        direction = 2;
                    }else if(t.hits2(t2.getHitBox()) == 3){
                        direction = 1;
                    }
                }
            }
            t.update(Gdx.graphics.getDeltaTime(), direction);
            switch(t.hits(player1.getHitBox())){
            case 4:
                SoundManager.kill.play(.03f);
                deleteEnemy.add(t);
                break;
            case 5:
                player1.setPosition(0, 400);
                dead++;
                SoundManager.dead.play(0.06f);
                gameState = 4;
                break;
            }
          //break; Remove this
         }