3
\$\begingroup\$

I want to make a small one-screen game in which the player has to slide pieces of an image until the image is completed. I used texture regions to create the pieces from an image

int numOfTiles_Horizontal = 5;
int numOfTiles_Vertical = 5;

float tile_width = img.getWidth() / numOfTiles_Horizontal;
float tile_height = img.getHeight() / numOfTiles_Vertical;

TextureRegion[][] tiles = new TextureRegion[numOfTiles_Horizontal][numOfTiles_Vertical];
for(int col = 0; col < numOfTiles_Horizontal; col++) {
    for(int row = 0; row < numOfTiles_Vertical; row++){
        tiles[col][row] = new TextureRegion(img, col * tile_width, row * tile_height, tile_width, tile_height);
    }
}

, but I'm unsure about how to allow the player to move the tiles into a blank space as well as making a "blank space" to allow tiles to move.

The game would look like this:

enter image description here

I followed some tutorials for libgdx but cannot find much on achieving this.

I am new to the engine and Android development so any help is appreciated.

\$\endgroup\$
1
\$\begingroup\$

Add an InputProcessor using Gdx.input.setInputProcesser(...) and check if where the mouse was clicked is in bounds of the grid map. If it is, try moving the piece in the spot to the empty spot near it. I didn't wanna add code but who cares. Here's an example in

public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
    label1:
    for(int i = 0; i < widthInTiles; i++)
    {
        for(int j = 0; j < heightInTiles; j++)
        {
            int tx = i * tileSize;
            int ty = j * tileSize;
            if(screenX >= tx && screenX <= tx + tileSize && screenY >= ty && screenY <= ty + tileSize )
            {
                tryMovingTile(i, j);
                break label1;
            }
        }
    }
    return true;
}
\$\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.