java - LibGDX: Can't seem to draw the objects in an array -


enemy class

public class enemy extends sprite{  private vector2 velocity = new vector2(0,0); private float speed = 30, gravity = 30 * 1.8f;   public enemy(sprite sprite){     super(sprite); }  public void draw(spritebatch spritebatch){     update(gdx.graphics.getdeltatime());     super.draw(spritebatch); }  public void update(float delta) {      velocity.y -= gravity * delta;     sety(velocity.y + speed * delta); }  } 

playscreen class

public class playscreen implements screen {     private player player;     private orthographiccamera camera;     private orthogonaltiledmaprenderer renderer;     private tiledmap map;     private rectangle rightrectangle, leftrectangle, playerrectangle;      @override     public void render(float delta) {         gdx.gl.glclearcolor(0, 0, 0, 1);         gdx.gl.glclear(gl20.gl_color_buffer_bit);         renderer.render();         renderer.getspritebatch().begin();         player.draw(renderer.getspritebatch());          boolean wallleft = leftrectangle.overlaps(player.rectangle);         boolean wallright = rightrectangle.overlaps(player.rectangle);          if(wallleft){             system.out.println("wallleft overlap");             player.velocity.x = 0;         }         else if(wallright){             system.out.println("wallright overlap");             player.velocity.x = 0;         }          enemies = new arraylist<enemy>();         enemy = new enemy(new sprite(new texture("img/player.png"))));          enemies.add(new enemy(new sprite(new texture("img/player.png"))));         enemies.add(new enemy(new sprite(new texture("img/player.png"))));           for(it = enemies.iterator(); it.hasnext();){          enemy.draw(renderer.getspritebatch());         enemy.setorigin(500, 500);          }           renderer.getspritebatch().end();      }     @override     public void resize(int width, int height) {         camera.viewportwidth = width;         camera.viewportheight = height;         camera.update();     }     @override     public void show() {         camera = new orthographiccamera();         map = new tiledmap();         renderer = new orthogonaltiledmaprenderer(map);          player = new player(new sprite(new texture("img/player.png")));         rightrectangle = new rectangle(1280,0,0,720);         leftrectangle = new rectangle(0,0,0,720);          player.setposition(             gdx.graphics.getwidth()/2f - player.getwidth()/2f,             gdx.graphics.getheight()/2f - player.getheight()/2f                  - gdx.graphics.getheight()/5f);     } } 

so if run this, application freeze , not respond anymore. believe problem when try adding 'enemy' playscreen class. ran fine before, when didn't put 'enemy' class project. i'm trying is, draw each enemy within array of enemies set position.

yes, missing it.next() in here advance loop.

for(it = enemies.iterator(); it.hasnext();){      enemy.draw(renderer.getspritebatch());     enemy.setorigin(500, 500);   } 

i think dont want drawing same enemy each time either, should set enemy = it.next() @ beginning of loop

also, im not %100 sure on how backend of works, i'm pretty sure don't want load 3 copies of same sprite in following snippet. should load sprite once, , use same 1 each enemy

enemy = new enemy(new sprite(new texture("img/player.png")))); enemies.add(new enemy(new sprite(new texture("img/player.png")))); enemies.add(new enemy(new sprite(new texture("img/player.png")))); 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -