How to rescue a variable from an Actor in Libgdx?

Asked

Viewed 84 times

3

I have the following Actor:

public class Carta extends Actor {
Sprite sprite;
public static int val, pos, peso;
Texture texture;

public Carta(int valor, int posicao) {
    val = valor;
    pos = posicao;

    switch (val) {
        case 1:
            texture = new Texture("espada.png");
            peso = 3;
            break;
        case 2:
            texture = new Texture("basto.jpg");
            peso = 2;
            break;
        case 3:
            texture = new Texture("tres.jpg");
            peso = 1;
            break;

    }


    sprite = new Sprite(texture);

}


public void draw(Batch batch, float pAlfa) {
    final Color c = getColor();
    batch.setColor(c.r, c.g, c.b, c.a * pAlfa);

    batch.draw(sprite, getX(), getY(), this.getWidth() / 2, 0, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
    batch.setColor(c.r, c.g, c.b, 1f);
    //this.setOrigin(this.getWidth() / 2, this.getHeight());

}

public void act(float delta) {

    super.act(delta);
}

}

And I call this actor:

...
carta1 = new Carta(1,2);
carta1.setBounds(100,200, 100, 250);

carta2 = new Carta(2,3);
carta2.setBounds(100,300, 100, 250);

stage.addActor(carta1);
stage.addActor(carta2);
...

Then I rescue the variable int peso with carta1.peso or carta2.peso, and always returns to me the weight of the actor carta2, someone knows why this happens?

  • Amigo @ramaral will you help me in this matter?

1 answer

7


Hello, Emerson

Wouldn’t it be because you put the variable weight as Static? Static makes its variable unique to any instance of the Letter class, because the weight attribute of all instances points to the same memory address. The card1 and card2 objects have their weight attribute pointing to the same memory address and, therefore, if you put the value 1 at that address through card1, card2.weight will also read this value 1 in memory.

The first instance carta1 = new Carta(1,2) makes the weight equal to 3, so every instance of Charter will have the weight attribute equal to 3. The second instantiation carta2 = new Carta(2,3) changes the value of the weight, makes the weight equal to 2, which will make the weight attribute of any instance of card, including card 1, also equal to 2.

Do a test with this code below:

public class Teste {
    static int variavelEstatica = 1;

    public Teste() {

    }

    public void setVariavelEstatica(int valor) {
       variavelEstatica = valor;
    }

    public int getVariavelEstatica() {
      return variavelEstatica ;
    }

}

public class Principal {

    public static void main(String[] args) {
         Teste teste1 = new Teste();
         Teste teste2 = new Teste();
         Teste teste3 = new Teste();

         System.out.println("Teste1: " + teste1.getVariavelEstatica());
         System.out.println("Teste2: " + teste2.getVariavelEstatica());
         System.out.println("Teste3: " + teste3.getVariavelEstatica());

         teste1.setVariavelEstatica(55);

         System.out.println("Teste1: " + teste1.getVariavelEstatica());
         System.out.println("Teste2: " + teste2.getVariavelEstatica());
         System.out.println("Teste3: " + teste3.getVariavelEstatica());


         teste2.setVariavelEstatica(66);

         System.out.println("Teste1: " + teste1.getVariavelEstatica());
         System.out.println("Teste2: " + teste2.getVariavelEstatica());
         System.out.println("Teste3: " + teste3.getVariavelEstatica());

         teste3.setVariavelEstatica(77);

         System.out.println("Teste1: " + teste1.getVariavelEstatica());
         System.out.println("Teste2: " + teste2.getVariavelEstatica());
         System.out.println("Teste3: " + teste3.getVariavelEstatica());
    }

}

I hope you were able to explain.

  • Thank you very much, besides certain your reply was very enlightening, congratulations for the knowledge acquired friend.

  • I’m glad I helped, @Emersonbarcellos! I know how hard it is to not understand what might be happening. New eyes always help! Here hoping to get the reward!

  • please see this link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.