# For loop not detecting Actors in draw method. (libGDX)

Question: In my code, I want to draw my actors through a For Loop. However the for loop doesn't seem to detect the addActor() call or even other libGDX calls.

Code:

package com.puzzlepieces.jjstudios;

import java.util.Random;

public class Box extends Table

{
Texture box = new Texture(Gdx.files.internal("box.png"));
Random rand = new Random();
Texture test = new Texture("bomb.png");

@Override
public void draw(Batch batch, float parentAlpha)
{
super.draw(batch, parentAlpha);

batch.draw(box, 400, 90, 900, 600);

this.setBounds(400 , 90, 900, 600);

this.setDebug(true);

for (int i = 0; i>5; i++)
{
for (int a = 0; a > 9; a++)
{
int randomNo = rand.nextInt(2);  // Sets random value
int est = 0;

if (randomNo == 0)
{
this.addActor(new Bomb());   //Spawns bomb if value 0
}
else if (randomNo == 1)
{
this.addActor(new FireCracker());  //Spawns FireCracker if value 1
}
else if (randomNo == 2)
{
this.addActor(new SteeringWheel());  //Spawns SteeringWheel if value is 2
}
else if (est == 1)
{
batch.draw(test, 400, 90);
}
}
this.row();
}
}
}


• The render method is called every frame, so about 60 times per second. which means you're adding 45 actors every frame unless you've disabled continuous rendering. Another point relating to the question is try using this.add() instead of this.addActor(). Then make sure you use this.pack() once all the actors have been added to re-evaluate display properties. – dfour Oct 18 '17 at 23:31
• Thanks for the reply. I didn't quite understand your first point, could you explain it a little more in depth? Thanks – Joel Jacob Oct 19 '17 at 0:09

Looking at the code you do this:

int est = 0;

...

else if (est == 1) {
batch.draw(test, 400, 90);
}


It doesn't seem like you change the est variable to 1. Because of this the batch will never draw.

Another remark: When using the Table class you want to use add() not addActor(). Otherwise the call to row() won't actually create a new row and space the actors properly.

I have updated your code so the Box will only get set to bomb cracker or wheel once when it is made by moving the loop into the constructor.

public class Box extends Table{

// variable fields for this Box class
Texture box = new Texture(Gdx.files.internal("box.png"));
Random rand = new Random();
Texture test = new Texture("bomb.png");

// the constructore, called once when you make a new Box
// e.g. something = new Box();
public Box(){

this.setDebug(true);

for (int i = 0; i>5; i++)
{
for (int a = 0; a > 9; a++)
{
int randomNo = rand.nextInt(2);  // Sets random value
int est = 0; // <- this is 0 so will never be 0 to match the else if check below

if (randomNo == 0){
this.add(new Bomb());   //Spawns bomb if value 0
}else if (randomNo == 1){
this.add(new FireCracker());  //Spawns FireCracker if value 1
}else if (randomNo == 2){
this.add(new SteeringWheel());  //Spawns SteeringWheel if value is 2
}else if (est == 1){// this will never be 1 as it is set earlier to 0
batch.draw(test, 400, 90);
}
}
this.row();
}
this.pack();
}

// this methos is the draw method and will run once every frame.
// this will be about 60 times per second depending on computer performance
@Override
public void draw(Batch batch, float parentAlpha)
{
super.draw(batch, parentAlpha);
batch.draw(box, 400, 90, 900, 600);
this.setBounds(400 , 90, 900, 600);
batch.draw(test, 400, 90);
}
}