2

I'm trying to spawn a sprite or object every second, but what happens is that the sprite is only rendered for less than a second every second

public class Player {
private Sprite playerSpr,enemy,enemy1;
public Vector2 position,size;
public Rectangle bounds;
private SpriteBatch batch;
private float deltaTime;
private float timer,elapsedTime;

private ArrayList<Sprite> enemies;
private Iterator<Sprite> enemyIterator;

public void create(){
    position=new Vector2(200,910);
    size=new Vector2(82,80);
    batch=new SpriteBatch();
    timer=0;

    playerSpr=new Sprite(new Texture(Gdx.files.internal("spr_player.png"))); 
    enemy=new Sprite(new Texture(Gdx.files.internal("spr_player.png"))); 
    enemy1=new Sprite(new Texture(Gdx.files.internal("spr_player.png"))); 
    bounds=new Rectangle(position.x,position.y,size.x,size.y);

    enemies=new ArrayList<Sprite>();
    enemies.add(playerSpr);
    enemies.add(enemy);

}

public void update(){
    deltaTime=Gdx.graphics.getDeltaTime();
    enemyIterator=enemies.iterator();

    timer+=1*deltaTime;
    elapsedTime+=1*deltaTime;   

    playerSpr.setPosition(position.x,position.y);
    playerSpr.setSize(size.x,size.y);

    bounds.set(position.x,position.y,size.x,size.y);
}

public void movement(){
    if(position.y<=910){
        position.y-=600*deltaTime;

        }

    }

/* wan't to draw the next sprite every second, for now I only have to stored in the iterator */
public void draw(){
    if(timer>=1){
        Sprite cur=enemyIterator.next();
        batch.begin();
        cur.draw(batch);
        batch.end();timer=0;
    }  

only the playerspr is being rendered for less that as second every second,I know be cause the timer is being reset every second but how do you spawn or draw the next one?

Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45
  • For future reference, there's no need to delete your old question and ask a new one. You can edit the original question, and that bumps it up the queue just like a new question. And the advantage of editing is that you don't lose the attention of people that were already trying to help you before. – Tenfour04 May 03 '15 at 18:06

1 Answers1

3

You are conflating your game logic with your drawing. All you should be doing in your draw method is drawing all your stuff, so you should just go through your list of enemies and draw each one:

public void draw(SpriteBatch batch){
    for (Sprite enemy : enemies)
        enemy.draw(batch);
}

Note that this class should not own or even reference a sprite batch, and should not call begin or end. The Game or Screen class that owns this class should call begin and end so they are only called once for the whole game.

And you want to spawn an enemy each second, which is something that should be done in your update method. Right after you increment your timer (the timer+=1*deltaTime; line), you can spawn an enemy by creating one and adding it to your list:

while (timer >= 1f) {
    createNewEnemy();
    timer -= 1f;  //don't simply reset to 0, or you will be rounding off every time a new one is spawned, so the rate won't be consistent over time.
}

(The reason for using while instead of if is because theoretically, you might want to spawn enemies more often than your max frame time. This prevents your game logic from falling behind if there is a slow frame somewhere.)

The createNewEnemy method should get a sprite, set it up, and add it to the enemies list. It could simply instantiate a new sprite new Sprite(enemy);, which creates a new Sprite and seeds it with the existing enemy prototype sprite you have. But if you use pooling, you can avoid triggering the GC, which will cause stuttering in your game:

private void newEnemy(){
    Sprite newEnemy = Pools.obtain(Sprite.class);
    newEnemy.set(enemy); //reference same Texture from your prototype enemy sprite
    //set up enemy position and other parameters here
    enemies.add(newEnemy);
}

If you use pooling, then later when you want to remove an enemy, you should do it like this:

private void removeEnemy(Enemy removedEnemy){
    Pools.free(removedEnemy);
    enemies.remove(removedEnemy);
}

Hopefully this gets you on the right track, although your game structure seems a bit confused (like why is this class called Player if it contains both the player and all the enemies?).

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • one last question, how do I make move the newEnemy if every second a new position for the newEnemy is created. – Kevin Bryan May 05 '15 at 14:55
  • I don't understand your question. Can you explain in more detail? – Tenfour04 May 05 '15 at 14:57
  • I want every sprite that is created every second to move downwards(decrease the y coordinate).Basically once the sprite is created it will move downwards until it goes outside the screen. Sorry for my english and thank you for your answer. – Kevin Bryan May 05 '15 at 16:09
  • That's quite an involved question. How about starting a new one? – Tenfour04 May 05 '15 at 17:37