Search code examples
javalibgdx

Numbers in diagonally/historical incrementation sequence


I don't really know if this question was asked somewhere before (I can't really find it)

So, I'm learning how to create basic color switch game (random color ball goes down and you need to rotate wheel to collide with ball with the same color)

And with this rotating I have a really big problem. I need to rotate them in somewhat of "sequence".

Example of what I have to get: 0 1 2 3 1 2 3 0 2 3 0 1 3 0 1 2 0 1 2 3 -> and again It looks like they are put diagonally. I hope you guys can know what I need to get.

My code is quite bad, it works like this: enter image description here

Unfortunatelly it don't work as I mentioned above. I know why it doesn't work - but how to do it properly? That's the question.

I'm using a ColorType[4] array to store latest colors. ColorType is an enum with colors.

My code:

public ColorType next() {
    circles[0] = circles[1];
    circles[1] = circles[2];
    circles[2] = circles[3];
    circles[3] = circles[0];

    System.out.println(circles[0] + ", " + circles[1] + ", " + circles[2] + ", " + circles[3]);

    return circles[0];
}

public void updateTextures() {
    next();

    this.textureRegions[0] = Util.getTextureRegion(SpriteType.CIRCLE, circles[0]);
    this.textureRegions[1] = Util.getTextureRegion(SpriteType.CIRCLE, circles[1]);
    this.textureRegions[2] = Util.getTextureRegion(SpriteType.CIRCLE, circles[2]);
    this.textureRegions[3] = Util.getTextureRegion(SpriteType.CIRCLE, circles[3]);
}

I'm executing updateTextures on click and then render it.


Solution

  • It looks like you are overriding you circles[0] before using it. To fix it you need to save it in a temp variable:

    public ColorType next() {
       ColorType temp = circles[0];
       circles[0] = circles[1];
       circles[1] = circles[2];
       circles[2] = circles[3];
       circles[3] = temp;
    
       System.out.println(circles[0] + ", " + circles[1] + ", " + circles[2] + ", " + circles[3]);
    
       return circles[0]; 
    }