Anything libgdx related goes here!
by guavaman » Tue Nov 13, 2018 10:25 pm
Hello I am trying to get a tile in grid to change color but it just won't work no matter what I do. The main problem is with touch down, i'm not sure how to use it. Here is the code:
- Code: Select all
package ksmd.tiles.Screens;
import com.badlogic.gdx.Application;
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 java.util.ArrayList;
import java.util.List;
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
List<Tile> spriteMapList = new ArrayList<Tile>();
public PlayScreen(Tiles game) {
this.game = game;
}
@Override
public void show() {
Gdx.input.setInputProcessor(this);
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) {
if (Gdx.input.isTouched()) {
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;
}
}
-
guavaman
-
- Posts: 1
- Joined: Tue Nov 13, 2018 10:23 pm
by bmanmcfly » Wed Nov 14, 2018 4:57 am
I would just use the screen position to figure which tile directly.
if your tiles are 20x20 and the clicked position is (assuming tiles start at 0,0) 550, 234, then you take (int)550/20, (int)234/20 and that is your tile coordinates.
-
bmanmcfly
-
- Posts: 30
- Joined: Fri Oct 30, 2015 4:09 am
by evilentity » Wed Nov 14, 2018 10:13 am
touchDown looks resonable, however what exactly do you expect the 'selected = true' part to do exactly?
Looking for a freelancer? PM me!
-
evilentity
-
- Posts: 4646
- Joined: Wed Aug 24, 2011 11:37 am
-
Return to Libgdx
Who is online
Users browsing this forum: Exabot [Bot], Google [Bot], shatterblast and 1 guest