1
\$\begingroup\$

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 com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.ui.Table;

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.addActor(new Bomb());

        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();
        }
    }
}

Please help me and sorry if I made some mistakes in my post as I'm a newbie

| improve this question | | | | |
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – dfour Oct 18 '17 at 23:31
  • \$\begingroup\$ Thanks for the reply. I didn't quite understand your first point, could you explain it a little more in depth? Thanks \$\endgroup\$ – Joel Jacob Oct 19 '17 at 0:09
1
\$\begingroup\$

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.

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

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.addActor(new Bomb());
        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);
    }
}
| improve this answer | | | | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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