0

I have twelve enemies in an ArrayList and I want to delay the batch.draw for each enemy. I tried the following code in the render method but it just makes the sprites flicker. I am using libgdx. Please help ;(

for (int i = 0; i < list.size(); i++)
{
    fireDelay -= Gdx.graphics.getDeltaTime();
    if (fireDelay <= 0)
    {
        batch.draw(list.get(i)//..etc)                    
        fireDelay += 0.2;
    }
}

3 Answers 3

0

The following code will definitely work:

boolean spawnTime=0;

void spawnEnemy(){
  Rectangle rect = new Rectangle();
  rect.x = Gdx.graphics.getWidth()/2;
  rect.y = Gdx.graphics.getHeight();
  rect.setSize(50,50);
  myList.add(rect);
}

spawnTime+=Gdx.graphics.getDeltaTime();

//call spawnEnemy function every second
if(spawnTime>=1){
  spawnEnemy();
  spawnTime=0;
}

//draw all the rectangles to the batch you added in the list
for(Rectangle rect: myList){
  batch.draw(rect,rect.x,rect.y,rect.w,rect.h);
}

Note: Declare spawnTime globally.

3
  • The following code will definitely work: and if it does not you can always yell "it should have worked!"
    – Madmenyo
    May 6, 2017 at 18:43
  • If you want to avoid error building up, change the if to a while and change spawnTime=0; to spawnTime-=1;.
    – Tenfour04
    May 6, 2017 at 21:21
  • @Madmenyo We are programmers, thats what we do , right? ;) May 7, 2017 at 6:00
0

Remember that in a game, your render method queues up all drawing commands. Then they are drawn to the screen. Then on the next call to render(), it clears the screen and starts over. Your code has a number of issues that don't make sense, like counting down the delta time inside a for loop.

There are a million ways to do this. Here's a simple one. What it does is on each call to render(), it updates the elapsed time and uses that to recalculate how many items should be showing now. Then the for loop draws all of them up to that number.

private static final float APPEARANCE_RATE = 5f; // 5 per second
private float elapsedTime = 0;

// in render():

elapsedTime += Gdx.graphics.getDeltaTime();

int itemCount = Math.min(list.size(), (int)(elapsedTime * APPEARANCE_RATE));
for (int i = 0; i < itemCount; i++)
{
    batch.draw(list.get(i), /*..etc*/);
}
1
  • I tried to run this but I can not get it working. All of my enemies in the ArrayList have the same position/behavior but must be drawned in the order of their index. I want to wait a second, spawn enemy 1, wait a second, spawn enemy 2, etc.
    – retchers
    May 6, 2017 at 3:05
0

You can try this snippet code as I've written using ModelBatch but the method works on spriteBatch too.

private int idx = 1;
private float elapsedTime = 0;
private int everySec = 1;
private Array<ModelInstance> storedInstance = new Array<>(); //extra array added each model every second to render in model batch.

And in render( ) method

modelBatch.begin(cam);
elapsedTime += Gdx.graphics.getDeltaTime();

int time = (int) Math.floor(elapsedTime);

if (idx <= instance.size) {

if (time == idx * everySec) {
     storedInstance.add(instance.get(idx - 1));
     idx++;
    }
}
modelBatch.render(storedInstance);
modelBatch.end();

The idx checks if all models have been rendered in every second respectively. If you want to change the time span of model appearance you should just alter "everySec" to for instance 5 for 5 seconds delay. Also "instance" is the list of my models to be rendered( in your case, the enemies).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.