How to select a tile in grid and change its color










0















I made a 5x5 grid by brute force, and now I want to change the tile's color upon click. The problem is in the touch down method which I can't seem to solve. I am a newbie at libgdx.



package ksmd.tiles.Screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

import ksmd.tiles.Tiles;
import ksmd.tiles.UI.Tile;

public class PlayScreen implements Screen, InputProcessor



private Tiles game;
private Tile tiles;
private Viewport viewport;
private OrthographicCamera camera;
private boolean selected;
private SpriteBatch batch;
private Texture tex;
private TextureRegion lit;
private TextureRegion dark;
private Vector3 mouse;

private float WORLD_WIDTH = 480, WORLD_HEIGHT = 800; //to see 50 x 50 units of the world
private float TILE_SIZE; //on tile is 10 units big

public PlayScreen(Tiles game)
this.game = game;


@Override
public void show()

camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); // center the camera
viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera); // create a viewport which sees 50 x 50 units of the game world

batch = new SpriteBatch();

tex = new Texture(Gdx.files.internal("badlogic.jpg"));

lit = Tiles.res.getAtlas("pack").findRegion("lit");
dark = Tiles.res.getAtlas("pack").findRegion("dark");
tiles = new Tile[5][5];
TILE_SIZE = WORLD_WIDTH / tiles[0].length; //on tile is 10 units big

//Create the tiles
for(int row = 0; row < tiles.length; row++)
for(int col = 0; col < tiles[0].length; col++)
if (selected)
tiles[row][col] = new Tile(col * TILE_SIZE, row * TILE_SIZE,TILE_SIZE, TILE_SIZE, lit);
else
tiles[row][col] = new Tile(col * TILE_SIZE, row * TILE_SIZE,TILE_SIZE, TILE_SIZE, dark);







public void setSelected (boolean b)
selected = b;


public boolean getSelected() return selected;

//render the Tiles
@Override
public void render(float delta)
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(viewport.getCamera().combined);

batch.begin(); //call batch.begin() (this is the only call of batch.begin() !!! )
for(int row = 0; row < tiles.length; row++)
for(int col = 0; col < tiles[0].length; col++)
tiles[row][col].render(batch, delta); // call the render method of each tile


batch.end();//call batch.end() (this is the only call of batch.end() !!! )


@Override
public void resize(int width, int height)
viewport.update(width, height);


@Override
public void dispose()
//dispose disposable objects
batch.dispose();


@Override
public void pause()


@Override
public void resume()


@Override
public void hide()


@Override
public boolean keyDown(int keycode)
return false;


@Override
public boolean keyUp(int keycode)
return false;


@Override
public boolean keyTyped(char character)
return false;


@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)

Vector3 clickPos = new Vector3(screenX, screenY,0);
camera.unproject(clickPos);

for(int row = 0; row < tiles.length; row++)
for(int col = 0; col < tiles[0].length; col++)
if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
//Tile tiles[row][col] was clicked
selected = true;




return false;


@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button)
return false;


@Override
public boolean touchDragged(int screenX, int screenY, int pointer)
return false;


@Override
public boolean mouseMoved(int screenX, int screenY)
return false;


@Override
public boolean scrolled(int amount)
return false;




The problem is in touch down. I am not sure how to get x and y of the tile and change its color upon click.



package ksmd.tiles.UI;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Tile

private Sprite texture;
private Rectangle bounding;

public Tile(float x, float y, float widht, float height, TextureRegion tex)
texture = new Sprite(tex);
texture.setBounds(x + 4, y *1.55f + 4, widht - 8, height* 1.55f - 8); // set bounds of Sprite
bounding = new Rectangle(x + 4, y *1.55f + 4, widht - 8, height* 1.55f - 8);



public void render(Batch batch, float delta)
//draw the sprite, but not call batch.begin() !!! because batch.begin() is already called!
texture.draw(batch);


public Rectangle getBounding() return bounding;




I have edited the code to include tile class. This is a grid with rectangular tiles.










share|improve this question




























    0















    I made a 5x5 grid by brute force, and now I want to change the tile's color upon click. The problem is in the touch down method which I can't seem to solve. I am a newbie at libgdx.



    package ksmd.tiles.Screens;

    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.InputProcessor;
    import com.badlogic.gdx.Screen;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.OrthographicCamera;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    import com.badlogic.gdx.graphics.g2d.TextureRegion;
    import com.badlogic.gdx.math.Vector3;
    import com.badlogic.gdx.utils.viewport.FitViewport;
    import com.badlogic.gdx.utils.viewport.Viewport;

    import ksmd.tiles.Tiles;
    import ksmd.tiles.UI.Tile;

    public class PlayScreen implements Screen, InputProcessor



    private Tiles game;
    private Tile tiles;
    private Viewport viewport;
    private OrthographicCamera camera;
    private boolean selected;
    private SpriteBatch batch;
    private Texture tex;
    private TextureRegion lit;
    private TextureRegion dark;
    private Vector3 mouse;

    private float WORLD_WIDTH = 480, WORLD_HEIGHT = 800; //to see 50 x 50 units of the world
    private float TILE_SIZE; //on tile is 10 units big

    public PlayScreen(Tiles game)
    this.game = game;


    @Override
    public void show()

    camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT);
    camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); // center the camera
    viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera); // create a viewport which sees 50 x 50 units of the game world

    batch = new SpriteBatch();

    tex = new Texture(Gdx.files.internal("badlogic.jpg"));

    lit = Tiles.res.getAtlas("pack").findRegion("lit");
    dark = Tiles.res.getAtlas("pack").findRegion("dark");
    tiles = new Tile[5][5];
    TILE_SIZE = WORLD_WIDTH / tiles[0].length; //on tile is 10 units big

    //Create the tiles
    for(int row = 0; row < tiles.length; row++)
    for(int col = 0; col < tiles[0].length; col++)
    if (selected)
    tiles[row][col] = new Tile(col * TILE_SIZE, row * TILE_SIZE,TILE_SIZE, TILE_SIZE, lit);
    else
    tiles[row][col] = new Tile(col * TILE_SIZE, row * TILE_SIZE,TILE_SIZE, TILE_SIZE, dark);







    public void setSelected (boolean b)
    selected = b;


    public boolean getSelected() return selected;

    //render the Tiles
    @Override
    public void render(float delta)
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(viewport.getCamera().combined);

    batch.begin(); //call batch.begin() (this is the only call of batch.begin() !!! )
    for(int row = 0; row < tiles.length; row++)
    for(int col = 0; col < tiles[0].length; col++)
    tiles[row][col].render(batch, delta); // call the render method of each tile


    batch.end();//call batch.end() (this is the only call of batch.end() !!! )


    @Override
    public void resize(int width, int height)
    viewport.update(width, height);


    @Override
    public void dispose()
    //dispose disposable objects
    batch.dispose();


    @Override
    public void pause()


    @Override
    public void resume()


    @Override
    public void hide()


    @Override
    public boolean keyDown(int keycode)
    return false;


    @Override
    public boolean keyUp(int keycode)
    return false;


    @Override
    public boolean keyTyped(char character)
    return false;


    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button)

    Vector3 clickPos = new Vector3(screenX, screenY,0);
    camera.unproject(clickPos);

    for(int row = 0; row < tiles.length; row++)
    for(int col = 0; col < tiles[0].length; col++)
    if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
    //Tile tiles[row][col] was clicked
    selected = true;




    return false;


    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button)
    return false;


    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer)
    return false;


    @Override
    public boolean mouseMoved(int screenX, int screenY)
    return false;


    @Override
    public boolean scrolled(int amount)
    return false;




    The problem is in touch down. I am not sure how to get x and y of the tile and change its color upon click.



    package ksmd.tiles.UI;

    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.Batch;
    import com.badlogic.gdx.graphics.g2d.Sprite;
    import com.badlogic.gdx.graphics.g2d.TextureRegion;

    public class Tile

    private Sprite texture;
    private Rectangle bounding;

    public Tile(float x, float y, float widht, float height, TextureRegion tex)
    texture = new Sprite(tex);
    texture.setBounds(x + 4, y *1.55f + 4, widht - 8, height* 1.55f - 8); // set bounds of Sprite
    bounding = new Rectangle(x + 4, y *1.55f + 4, widht - 8, height* 1.55f - 8);



    public void render(Batch batch, float delta)
    //draw the sprite, but not call batch.begin() !!! because batch.begin() is already called!
    texture.draw(batch);


    public Rectangle getBounding() return bounding;




    I have edited the code to include tile class. This is a grid with rectangular tiles.










    share|improve this question


























      0












      0








      0








      I made a 5x5 grid by brute force, and now I want to change the tile's color upon click. The problem is in the touch down method which I can't seem to solve. I am a newbie at libgdx.



      package ksmd.tiles.Screens;

      import com.badlogic.gdx.Gdx;
      import com.badlogic.gdx.InputProcessor;
      import com.badlogic.gdx.Screen;
      import com.badlogic.gdx.graphics.GL20;
      import com.badlogic.gdx.graphics.OrthographicCamera;
      import com.badlogic.gdx.graphics.Texture;
      import com.badlogic.gdx.graphics.g2d.SpriteBatch;
      import com.badlogic.gdx.graphics.g2d.TextureRegion;
      import com.badlogic.gdx.math.Vector3;
      import com.badlogic.gdx.utils.viewport.FitViewport;
      import com.badlogic.gdx.utils.viewport.Viewport;

      import ksmd.tiles.Tiles;
      import ksmd.tiles.UI.Tile;

      public class PlayScreen implements Screen, InputProcessor



      private Tiles game;
      private Tile tiles;
      private Viewport viewport;
      private OrthographicCamera camera;
      private boolean selected;
      private SpriteBatch batch;
      private Texture tex;
      private TextureRegion lit;
      private TextureRegion dark;
      private Vector3 mouse;

      private float WORLD_WIDTH = 480, WORLD_HEIGHT = 800; //to see 50 x 50 units of the world
      private float TILE_SIZE; //on tile is 10 units big

      public PlayScreen(Tiles game)
      this.game = game;


      @Override
      public void show()

      camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT);
      camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); // center the camera
      viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera); // create a viewport which sees 50 x 50 units of the game world

      batch = new SpriteBatch();

      tex = new Texture(Gdx.files.internal("badlogic.jpg"));

      lit = Tiles.res.getAtlas("pack").findRegion("lit");
      dark = Tiles.res.getAtlas("pack").findRegion("dark");
      tiles = new Tile[5][5];
      TILE_SIZE = WORLD_WIDTH / tiles[0].length; //on tile is 10 units big

      //Create the tiles
      for(int row = 0; row < tiles.length; row++)
      for(int col = 0; col < tiles[0].length; col++)
      if (selected)
      tiles[row][col] = new Tile(col * TILE_SIZE, row * TILE_SIZE,TILE_SIZE, TILE_SIZE, lit);
      else
      tiles[row][col] = new Tile(col * TILE_SIZE, row * TILE_SIZE,TILE_SIZE, TILE_SIZE, dark);







      public void setSelected (boolean b)
      selected = b;


      public boolean getSelected() return selected;

      //render the Tiles
      @Override
      public void render(float delta)
      Gdx.gl.glClearColor(0, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      batch.setProjectionMatrix(viewport.getCamera().combined);

      batch.begin(); //call batch.begin() (this is the only call of batch.begin() !!! )
      for(int row = 0; row < tiles.length; row++)
      for(int col = 0; col < tiles[0].length; col++)
      tiles[row][col].render(batch, delta); // call the render method of each tile


      batch.end();//call batch.end() (this is the only call of batch.end() !!! )


      @Override
      public void resize(int width, int height)
      viewport.update(width, height);


      @Override
      public void dispose()
      //dispose disposable objects
      batch.dispose();


      @Override
      public void pause()


      @Override
      public void resume()


      @Override
      public void hide()


      @Override
      public boolean keyDown(int keycode)
      return false;


      @Override
      public boolean keyUp(int keycode)
      return false;


      @Override
      public boolean keyTyped(char character)
      return false;


      @Override
      public boolean touchDown(int screenX, int screenY, int pointer, int button)

      Vector3 clickPos = new Vector3(screenX, screenY,0);
      camera.unproject(clickPos);

      for(int row = 0; row < tiles.length; row++)
      for(int col = 0; col < tiles[0].length; col++)
      if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
      //Tile tiles[row][col] was clicked
      selected = true;




      return false;


      @Override
      public boolean touchUp(int screenX, int screenY, int pointer, int button)
      return false;


      @Override
      public boolean touchDragged(int screenX, int screenY, int pointer)
      return false;


      @Override
      public boolean mouseMoved(int screenX, int screenY)
      return false;


      @Override
      public boolean scrolled(int amount)
      return false;




      The problem is in touch down. I am not sure how to get x and y of the tile and change its color upon click.



      package ksmd.tiles.UI;

      import com.badlogic.gdx.graphics.Texture;
      import com.badlogic.gdx.graphics.g2d.Batch;
      import com.badlogic.gdx.graphics.g2d.Sprite;
      import com.badlogic.gdx.graphics.g2d.TextureRegion;

      public class Tile

      private Sprite texture;
      private Rectangle bounding;

      public Tile(float x, float y, float widht, float height, TextureRegion tex)
      texture = new Sprite(tex);
      texture.setBounds(x + 4, y *1.55f + 4, widht - 8, height* 1.55f - 8); // set bounds of Sprite
      bounding = new Rectangle(x + 4, y *1.55f + 4, widht - 8, height* 1.55f - 8);



      public void render(Batch batch, float delta)
      //draw the sprite, but not call batch.begin() !!! because batch.begin() is already called!
      texture.draw(batch);


      public Rectangle getBounding() return bounding;




      I have edited the code to include tile class. This is a grid with rectangular tiles.










      share|improve this question
















      I made a 5x5 grid by brute force, and now I want to change the tile's color upon click. The problem is in the touch down method which I can't seem to solve. I am a newbie at libgdx.



      package ksmd.tiles.Screens;

      import com.badlogic.gdx.Gdx;
      import com.badlogic.gdx.InputProcessor;
      import com.badlogic.gdx.Screen;
      import com.badlogic.gdx.graphics.GL20;
      import com.badlogic.gdx.graphics.OrthographicCamera;
      import com.badlogic.gdx.graphics.Texture;
      import com.badlogic.gdx.graphics.g2d.SpriteBatch;
      import com.badlogic.gdx.graphics.g2d.TextureRegion;
      import com.badlogic.gdx.math.Vector3;
      import com.badlogic.gdx.utils.viewport.FitViewport;
      import com.badlogic.gdx.utils.viewport.Viewport;

      import ksmd.tiles.Tiles;
      import ksmd.tiles.UI.Tile;

      public class PlayScreen implements Screen, InputProcessor



      private Tiles game;
      private Tile tiles;
      private Viewport viewport;
      private OrthographicCamera camera;
      private boolean selected;
      private SpriteBatch batch;
      private Texture tex;
      private TextureRegion lit;
      private TextureRegion dark;
      private Vector3 mouse;

      private float WORLD_WIDTH = 480, WORLD_HEIGHT = 800; //to see 50 x 50 units of the world
      private float TILE_SIZE; //on tile is 10 units big

      public PlayScreen(Tiles game)
      this.game = game;


      @Override
      public void show()

      camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT);
      camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); // center the camera
      viewport = new FitViewport(WORLD_WIDTH, WORLD_HEIGHT, camera); // create a viewport which sees 50 x 50 units of the game world

      batch = new SpriteBatch();

      tex = new Texture(Gdx.files.internal("badlogic.jpg"));

      lit = Tiles.res.getAtlas("pack").findRegion("lit");
      dark = Tiles.res.getAtlas("pack").findRegion("dark");
      tiles = new Tile[5][5];
      TILE_SIZE = WORLD_WIDTH / tiles[0].length; //on tile is 10 units big

      //Create the tiles
      for(int row = 0; row < tiles.length; row++)
      for(int col = 0; col < tiles[0].length; col++)
      if (selected)
      tiles[row][col] = new Tile(col * TILE_SIZE, row * TILE_SIZE,TILE_SIZE, TILE_SIZE, lit);
      else
      tiles[row][col] = new Tile(col * TILE_SIZE, row * TILE_SIZE,TILE_SIZE, TILE_SIZE, dark);







      public void setSelected (boolean b)
      selected = b;


      public boolean getSelected() return selected;

      //render the Tiles
      @Override
      public void render(float delta)
      Gdx.gl.glClearColor(0, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      batch.setProjectionMatrix(viewport.getCamera().combined);

      batch.begin(); //call batch.begin() (this is the only call of batch.begin() !!! )
      for(int row = 0; row < tiles.length; row++)
      for(int col = 0; col < tiles[0].length; col++)
      tiles[row][col].render(batch, delta); // call the render method of each tile


      batch.end();//call batch.end() (this is the only call of batch.end() !!! )


      @Override
      public void resize(int width, int height)
      viewport.update(width, height);


      @Override
      public void dispose()
      //dispose disposable objects
      batch.dispose();


      @Override
      public void pause()


      @Override
      public void resume()


      @Override
      public void hide()


      @Override
      public boolean keyDown(int keycode)
      return false;


      @Override
      public boolean keyUp(int keycode)
      return false;


      @Override
      public boolean keyTyped(char character)
      return false;


      @Override
      public boolean touchDown(int screenX, int screenY, int pointer, int button)

      Vector3 clickPos = new Vector3(screenX, screenY,0);
      camera.unproject(clickPos);

      for(int row = 0; row < tiles.length; row++)
      for(int col = 0; col < tiles[0].length; col++)
      if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
      //Tile tiles[row][col] was clicked
      selected = true;




      return false;


      @Override
      public boolean touchUp(int screenX, int screenY, int pointer, int button)
      return false;


      @Override
      public boolean touchDragged(int screenX, int screenY, int pointer)
      return false;


      @Override
      public boolean mouseMoved(int screenX, int screenY)
      return false;


      @Override
      public boolean scrolled(int amount)
      return false;




      The problem is in touch down. I am not sure how to get x and y of the tile and change its color upon click.



      package ksmd.tiles.UI;

      import com.badlogic.gdx.graphics.Texture;
      import com.badlogic.gdx.graphics.g2d.Batch;
      import com.badlogic.gdx.graphics.g2d.Sprite;
      import com.badlogic.gdx.graphics.g2d.TextureRegion;

      public class Tile

      private Sprite texture;
      private Rectangle bounding;

      public Tile(float x, float y, float widht, float height, TextureRegion tex)
      texture = new Sprite(tex);
      texture.setBounds(x + 4, y *1.55f + 4, widht - 8, height* 1.55f - 8); // set bounds of Sprite
      bounding = new Rectangle(x + 4, y *1.55f + 4, widht - 8, height* 1.55f - 8);



      public void render(Batch batch, float delta)
      //draw the sprite, but not call batch.begin() !!! because batch.begin() is already called!
      texture.draw(batch);


      public Rectangle getBounding() return bounding;




      I have edited the code to include tile class. This is a grid with rectangular tiles.







      android libgdx






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 18:13







      Guavaman

















      asked Nov 12 '18 at 22:06









      GuavamanGuavaman

      104




      104






















          1 Answer
          1






          active

          oldest

          votes


















          0














          In your Tile class create a com.badlogic.gdx.math.Rectangle object which can check if a point is in this Rectangle:



          public class Tile 
          ...
          private Rectangle bounding;

          public Tile(float x, float y, float width, float height)
          ...
          bounding = new Rectangle(x, y, width, height);



          public Rectangle getBounding() return bounding;



          And then in your touchDown method:



          @Override
          public boolean touchDown(int screenX, int screenY, int pointer, int button)
          Vector3 clickPos = camera.unproject(screenX, screenY, 0);

          for(int row = 0; row < tiles.length; row++)
          for(int col = 0; col < tiles[0].length; col++)
          if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
          //Tile tiles[row][col] was clicked
          return true;




          return false;






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270782%2fhow-to-select-a-tile-in-grid-and-change-its-color%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            In your Tile class create a com.badlogic.gdx.math.Rectangle object which can check if a point is in this Rectangle:



            public class Tile 
            ...
            private Rectangle bounding;

            public Tile(float x, float y, float width, float height)
            ...
            bounding = new Rectangle(x, y, width, height);



            public Rectangle getBounding() return bounding;



            And then in your touchDown method:



            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button)
            Vector3 clickPos = camera.unproject(screenX, screenY, 0);

            for(int row = 0; row < tiles.length; row++)
            for(int col = 0; col < tiles[0].length; col++)
            if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
            //Tile tiles[row][col] was clicked
            return true;




            return false;






            share|improve this answer





























              0














              In your Tile class create a com.badlogic.gdx.math.Rectangle object which can check if a point is in this Rectangle:



              public class Tile 
              ...
              private Rectangle bounding;

              public Tile(float x, float y, float width, float height)
              ...
              bounding = new Rectangle(x, y, width, height);



              public Rectangle getBounding() return bounding;



              And then in your touchDown method:



              @Override
              public boolean touchDown(int screenX, int screenY, int pointer, int button)
              Vector3 clickPos = camera.unproject(screenX, screenY, 0);

              for(int row = 0; row < tiles.length; row++)
              for(int col = 0; col < tiles[0].length; col++)
              if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
              //Tile tiles[row][col] was clicked
              return true;




              return false;






              share|improve this answer



























                0












                0








                0







                In your Tile class create a com.badlogic.gdx.math.Rectangle object which can check if a point is in this Rectangle:



                public class Tile 
                ...
                private Rectangle bounding;

                public Tile(float x, float y, float width, float height)
                ...
                bounding = new Rectangle(x, y, width, height);



                public Rectangle getBounding() return bounding;



                And then in your touchDown method:



                @Override
                public boolean touchDown(int screenX, int screenY, int pointer, int button)
                Vector3 clickPos = camera.unproject(screenX, screenY, 0);

                for(int row = 0; row < tiles.length; row++)
                for(int col = 0; col < tiles[0].length; col++)
                if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
                //Tile tiles[row][col] was clicked
                return true;




                return false;






                share|improve this answer















                In your Tile class create a com.badlogic.gdx.math.Rectangle object which can check if a point is in this Rectangle:



                public class Tile 
                ...
                private Rectangle bounding;

                public Tile(float x, float y, float width, float height)
                ...
                bounding = new Rectangle(x, y, width, height);



                public Rectangle getBounding() return bounding;



                And then in your touchDown method:



                @Override
                public boolean touchDown(int screenX, int screenY, int pointer, int button)
                Vector3 clickPos = camera.unproject(screenX, screenY, 0);

                for(int row = 0; row < tiles.length; row++)
                for(int col = 0; col < tiles[0].length; col++)
                if(tiles[row][col].getBounding().contains(clickPos.x, clickPos.y))
                //Tile tiles[row][col] was clicked
                return true;




                return false;







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 13:25

























                answered Nov 13 '18 at 10:10









                MorchulMorchul

                1,103113




                1,103113



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270782%2fhow-to-select-a-tile-in-grid-and-change-its-color%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto