How to redeem an Actor variable in Libgdx?

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);
...

So I redeem the variable int peso with carta1.peso or carta2.peso , and I always return the weight of the actor carta2 , does anyone know why that happens?     

asked by anonymous 20.06.2016 / 16:37

1 answer

7

Hello, Emerson

Would not it be because you put the weight variable as static? Static makes its variable unique to any instance of the Charter class, since the weight attribute of all instances now points to the same memory address. The letter1 and letter2 objects have their weight attribute pointing to the same memory address and so if you put the value 1 at that address through letter1, letter2.weight will also read that value 1 in memory.

The first instantiation carta1 = new Carta(1,2) causes the weight to be equal to 3, then, every instance of Charter will have the weight attribute equal to 3. The second instantiation carta2 = new Carta(2,3) changes the weight value, causes the weight is equal to 2, which will make the weight attribute of any letter instance, including letter1, also equal 2.

Try 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've been able to explain.

    
23.06.2016 / 20:59