libGDX changing button's image - java

By following Kilbolt's Zombie Bird tutorial I created in my project a class to represent all buttons:
public class SimpleButton {
private float x, y, width, height;
private TextureRegion buttonUp;
private TextureRegion buttonDown;
private Rectangle bounds;
private boolean isPressed = false;
public SimpleButton(float x, float y, float width, float height,
TextureRegion buttonUp, TextureRegion buttonDown) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.buttonUp = buttonUp;
this.buttonDown = buttonDown;
bounds = new Rectangle(x, y, width, height);
}
public boolean isClicked(int screenX, int screenY) {
return bounds.contains(screenX, screenY);
}
public void draw(SpriteBatch batcher) {
if (isPressed) {
batcher.draw(buttonDown, x, y, width, height);
} else {
batcher.draw(buttonUp, x, y, width, height);
}
}
public boolean isTouchDown(int screenX, int screenY) {
if (bounds.contains(screenX, screenY)) {
isPressed = true;
return true;
}
return false;
}
public boolean isTouchUp(int screenX, int screenY) {
// It only counts as a touchUp if the button is in a pressed state.
if (bounds.contains(screenX, screenY) && isPressed) {
isPressed = false;
return true;
}
// Whenever a finger is released, we will cancel any presses.
isPressed = false;
return false;
}
}
Whenever I want to create a button I do it in my InputProcessor class by creating a SimpleButton object and after filling its parameters I add it to List list.
My current problem is that I'm trying to create a sound button, which I did create and it work fine, but what I'm struggling with is making the button change TextureRegion between ON and OFF states.
This is an example of how I create a button(actually I created the sound button) and put it in the list:
soundButton = new SimpleButton(
136 / 2 - (AssetLoader.soundOnNotClicked.getRegionWidth() / 0.25f),
midPointY - 102, 15, 15, AssetLoader.soundOnNotClicked,
AssetLoader.soundClicked);
gameButtons.add(backButton);
I tried using the same condition that when sound ON is on it will put inside the parameters "AssetLoader.soundOnNotClicked" and when it's OFF "AssetLoader.soundOFFNotClicked", but it just show the first TextureRegion in the condition. Can someone help me figure out how I can change TextureRigions when sound is ON and OFF?
EDIT WITH CHANGES:
First created a second constractor:
public SimpleButton(float x, float y, float width, float height,
TextureRegion buttonUp, TextureRegion buttonDown, TextureRegion buttonOn) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.buttonUp = buttonUp;
this.buttonDown = buttonDown;
this.buttonOn = buttonOn;
bounds = new Rectangle(x, y, width, height);
}
Then changed a bit the isClicked method:
public boolean isClicked(int screenX, int screenY) {
if (isOn == false) {
isOn = true;
}else {
isOn = false;
}
return bounds.contains(screenX, screenY);
}
Then changed the draw method:
public void draw(SpriteBatch batcher) {
if (isPressed) {
batcher.draw(buttonDown, x, y, width, height);
} else {
if (isOn == false) {
batcher.draw(buttonUp, x, y, width, height);
}else{
batcher.draw(buttonOn, x, y, width, height);
}
}
}
Lastly I changed the creation of the button by adding the textureRegion of the OFF sound:
soundButton = new SimpleButton(
136 / 2 - (AssetLoader.soundButtonOff.getRegionWidth() / 0.25f),
midPointY - 102, 15, 15, AssetLoader.soundButtonOff,
AssetLoader.soundButtonOn, AssetLoader.soundButtonOffX);
menuButtons.add(soundButton);

Make another boolean called isOn just like isPressed
Add another TextureRegion called buttonOn just like buttonDown
Add TextureRegion buttonDown to your constructor and set it there like you do with buttonDown
Flip the boolean whenever the button is clicked in isClicked(). (isOn = !isOn)
Add the condition in your draw method to draw the buttonOn texture if isOn is true.
That should pretty much do it.

Related

LIBGDX - How to make the paddle not jumping in pong game?

I am making a Pong game and when I click on the screen the paddle jump to my cursor point. I want that I need to drag the cursor to move him and without jumping like normal Pong game. how can I do this?
This is my Paddle class:
public class Paddle {
private Vector3 position;
private int width, height;
private Texture texture;
public Paddle(int x, int y, int width, int height){
this.width = width;
this.height = height;
createTexture(width,height);
position = new Vector3(x, y, 0);
}
private void createTexture(int width, int height) {
Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.BLACK);
pixmap.fillRectangle(0, 0, width, height);
texture = new Texture(pixmap);
pixmap.dispose();
}
public void update(int y){
position.add(0, y - position.y,0);
position.y = y;
position.set(position.x, HeadGuns.HEIGHT - position.y, position.z);
}
public void draw(SpriteBatch sb){
sb.draw(texture, position.x, position.y, width,height);
}
This is my PlayState class:
public class PlayState extends State {
private Paddle myPaddle;
public PlayState(GameStateManager gsm) {
super(gsm);
myPaddle = new Paddle(25, HeadGuns.HEIGHT/2, 25, 150);
}
#Override
public void handleInput() {
if (Gdx.input.isTouched()){
//when I touched the screen
myPaddle.update(Gdx.input.getY());
}
}
#Override
public void update(float dt) {
handleInput();
}
#Override
public void render(SpriteBatch sb) {
sb.begin();
myPaddle.draw(sb);
sb.end();
}
#Override
public void dispose() {
}
You are reading touch position:
Gdx.input.getY()
and using it directly to set pad position - you can't do that.
You should use InputLister to get events.
First you should listen to touchDown and see is user touching your pad or not (compare touch coordinates with pad coordinates)
Then, for dragging you should use touchDragged() event...to update pad position when dragging happen, but only if touchDown detected that touch:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/InputListener.html#touchDragged-com.badlogic.gdx.scenes.scene2d.InputEvent-float-float-int-

libgdx: wrong touch handling on phone

foreword: on desktop everything works just perfect.
Trouble appers when i'm testing my project on phone
I start the game. Here is some buttons on screen: button_1, button_2, button_3. For example i'm touching button_1: with very first touch after app start nothing happing at all. If i'm touching button_1 again, it works fine -> touchDown (button image goes down for 10px) then touchUp (button image goes up for 10px and button code run). But if i touch another button instead, for example button_2, only touchDown of button_1 occurs (button_1 image goes down for 10px and up) and nothing else. It's happens for every button, so i need to touch button twice to make it work.
Button class:
public class myButton {
private float x, y, width, height;
private Texture buttonUp;
public Rectangle bounds;
private boolean isPressed = false;
public myButton(float x, float y, float width, float height, Texture buttonUp) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.buttonUp = buttonUp;
bounds = new Rectangle(x, y, width, height);
}
public boolean isClicked(int screenX, int screenY) {
return bounds.contains(screenX, screenY);
}
public void draw(SpriteBatch batch) {
if (isPressed) {
batch.draw(buttonUp, x, y - 10, width, height);
} else {
batch.draw(buttonUp, x, y, width, height);
}
}
public boolean isTouchDown(int screenX, int screenY) {
if (bounds.contains(screenX, screenY)) {
isPressed = true;
return true;
}
return false;
}
public boolean isTouchUp(int screenX, int screenY) {
if (bounds.contains(screenX, screenY) && isPressed) {
isPressed = false;
return true;
}
isPressed = false;
return false;
}
}
InputHandlerer:
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
playButton.isTouchDown(screenX, screenY);
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
if (playButton.isTouchUp(screenX, screenY)) {
start();
return true;
}
}
in create() method i've got:
touchPos = new Vector3();
Gdx.input.setInputProcessor(new InputHandlerer());
camera = new OrthographicCamera();
viewport = new FitViewport(1080, 1920, camera);
in render() method i've got:
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
batch.setProjectionMatrix(camera.combined);
I repeat - on desktop every button works properly, but on phone not. What could be the problem?
Maybe on desktop is working because you can do only one click at a time, instead on phone you should manage the pointers, also if you don't want to use the multitouch.
On your InputHandlerer class you have to initilize:
private int button1pointer = -1;
private boolean button1isPressed = false;
Then you have to manage the pointers on your tochDown/Up methods:
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
for (int i = 0; i < 2; i++) { //here you can choose how many touches you can manage
if (Gdx.input.isTouched(i)) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
if (playButton.isTouchDown(screenX, screenY) && (button1pointer != i) && (!button1isPressed)) {
button1pointer = i;
button1isPressed = true;
}
return true;
}
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (!(Gdx.input.isTouched(pointer)) && (button1pointer == pointer) && (button1isPressed)) {
button1pointer = -1;
button1isPressed = false;
start();
return true;
}
}

LibGDX: How to get the same coordinates in tiled map for tile with and without zoom

I am a beginner with LibGDX. I am trying to create an android game based fixed size tiled map (33x21 tiles). Tiles I am using are 32x32px. So far I managed to load the map created with Tiled and add touch gestures like zoom in/out and panning. The game character movement will be turn based tile by tile, so I need to have a possibility to select specific tile in order to perform some action. I tried solution from here: LibGDX: How to make tiled map tiles clickable? and it works perfectly fine, until the screen is not zoomed in.
Without zoom, the left-bottom corner coordinates shows that it is (0,0), which is correct. When I zoom in, the left-bottom corner of the visible part of the map instead of keeping it's correct coordinates (i.e. (480, 320)) it becomes (0,0) again. I tried to use camera.unproject(Vector3), but I was unsuccessful. Probably used it wrong. I am also not really convinced, that the way in which I try to get tiles is the most appropriate one. Can you help me get tiles coordinates properly? Below is the code that I am using:
public class TiledTest extends ApplicationAdapter {
public TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
GestureDetector gesture;
InputMultiplexer myInputMultiplexer;
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
public static final float PAN_RATE = (float) 0.01;
private static final float ZOOM_SPEED = (float) 0.009;
int columns, rows;
TiledMapTileLayer grid_layer;
#Override
public void create () {
gesture =new GestureDetector(new MyGestureListener());
myInputMultiplexer = new InputMultiplexer();
float unitScale = 1 / 32f;
camera = new OrthographicCamera();
camera.setToOrtho(true, 33,21);
tiledMap = new TmxMapLoader().load("tiled_map.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap, unitScale);
grid_layer = (TiledMapTileLayer)tiledMap.getLayers().get(0);
Stage stage = new TiledMapStage(tiledMap);
myInputMultiplexer.addProcessor(gesture);
myInputMultiplexer.addProcessor(stage);
Gdx.input.setInputProcessor(myInputMultiplexer);
}
#Override
public void render () {
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
camera.update();
}
public class TiledMapActor extends Actor {
private TiledMapTileLayer.Cell cell;
public TiledMapActor(TiledMap tileMap, TiledMapTileLayer tiledLayer, TiledMapTileLayer.Cell cell) {
tiledMap = tileMap;
grid_layer = tiledLayer;
this.cell = cell;
}
}
public class TiledMapClickListener extends ClickListener {
private TiledMapActor actor;
public TiledMapClickListener(TiledMapActor actor) {
this.actor = actor;
}
#Override
public void clicked(InputEvent event, float x, float y) {
int a = (int) actor.getX();
int b = (int) actor.getY();
System.out.println(actor.cell + " has been clicked.");
System.out.println("x = " + a + "y = " + b );
}
}
public class TiledMapStage extends Stage {
private TiledMap tiledMap;
public TiledMapStage(TiledMap tiledMap) {
this.tiledMap = tiledMap;
for (MapLayer layer : tiledMap.getLayers()) {
TiledMapTileLayer tiledLayer = (TiledMapTileLayer)layer;
createActorsForLayer(tiledLayer);
}
}
private void createActorsForLayer(TiledMapTileLayer tiledLayer) {
for (int x = 0; x <= tiledLayer.getWidth(); x++) {
for (int y = 0; y < tiledLayer.getHeight(); y++) {
TiledMapTileLayer.Cell cell = tiledLayer.getCell(x, y);
TiledMapActor actor = new TiledMapActor(tiledMap, tiledLayer, cell);
actor.setBounds(x * tiledLayer.getTileWidth(), y * tiledLayer.getTileHeight(), tiledLayer.getTileWidth(),
tiledLayer.getTileHeight());
addActor(actor);
EventListener eventListener = new TiledMapClickListener(actor);
actor.addListener(eventListener);
}
}
}
}
public void resize(float width, float height) {
}
public class MyGestureListener implements GestureListener{
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
return false;
}
#Override
public boolean tap(float x, float y, int count, int button) {
return false;
}
#Override
public boolean longPress(float x, float y) {
return false;
}
#Override
public boolean fling(float velocityX, float velocityY, int button) {
return false;
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
float effectiveViewportHeight = camera.viewportHeight * camera.zoom;
camera.position.x = MathUtils.clamp(camera.position.x, effectiveViewportWidth / 2f, 33 - effectiveViewportWidth / 2f);
camera.position.y = MathUtils.clamp(camera.position.y, effectiveViewportHeight / 2f, 21 - effectiveViewportHeight / 2f);
if (camera.zoom < 1) {
camera.translate(-deltaX * PAN_RATE, -deltaY * PAN_RATE, 0);
camera.update();
}
return false;
}
#Override
public boolean panStop(float x, float y, int pointer, int button) {
//Gdx.app.log("Text", "panstop");
return false;
}
#Override
public boolean zoom (float originalDistance, float currentDistance){
float ratio = originalDistance/currentDistance;
camera.zoom += ZOOM_SPEED * ratio;
if (camera.zoom < 0.3)
{
camera.zoom = (float) 0.3;
}
else if (camera.zoom > 1)
{
camera.zoom = 1;
}
System.out.println(camera.zoom);
return false;
}
#Override
public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer){
camera.zoom -= .01;
camera.update();
return false;
}
}
}
So you want to transfer the screen coordinates to world coordinates.
For example:
#Override
public void clicked(InputEvent event, float x, float y) {
Vector3 touch = new Vector3(x, y, 0);
camera.unproject(touch);
System.out.println("Screen coordinates translated to world coordinates: "
+ "X: " + touch.x + " Y: " + touch.y);
}
}
Above clicked function does not worked for me, but you led me to the answer. I dumped whole assigning actors for tiles and used camera.unproject in MyGestureListener in touchDown.
If someone is interested I just leave it here :)
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
Vector3 temp_coord = new Vector3(x,y,0);
Vector3 coords = camera.unproject(temp_coord);
x =(int) coords.x;
y =(int) coords.y;
writerX = x;
writerY = y;
System.out.println("Screen coordinates translated to world coordinates: "
+ "X: " + x + " Y: " + y);
return false;
}

touch isn't detecting on libgdx button

I'm new to libgdx and learning libgdx scene2d.
In menu screen constructor I had created a stage with the device's resolution
and added stage to input processor:
stage = new Stage(800, 480, false);
stage.getCamera().position.set(400, 240, 0);
Gdx.input.setInputProcessor(stage);
Now, I created a button and added it to stage:
PButton start = new PButton(Assets.btn, Assets.btn_p, Assets.font50,
"START");
start.setPosition(600, 400);
start.addListener(new ActorGestureListener() {
#Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
getGame().setScreen(new GameScreen1(getGame()));
System.out.println("set game");
}
});
stage.addActor(start);
where PButton extends the Button class as:
public class PButton extends Button {
private BitmapFont font;
private String text;
private float sizex;
private float sizey;
private float posx;
private float posy;
public PButton(TextureRegion up, TextureRegion down, BitmapFont font,
String text) {
super(new TextureRegionDrawable(up), new TextureRegionDrawable(down));
this.font = font;
this.text = text;
this.sizex = up.getRegionWidth();
this.sizey = down.getRegionHeight();
setSize(sizex, sizey);
font.setScale(Initiate.getM_ratio());
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
font.draw(batch, text, posx + sizex / 2 - font.getBounds(text).width
/ 2, posy + sizey / 2 + font.getBounds(text).height / 2);
}
#Override
public void setPosition(float x, float y) {
this.posx = x;
this.posy = y;
super.setPosition(posx, posy);
}
}
but when I click on the button, it doesn't get input.
UPDATE: problem solved after a deep debugging I found that my stage.dispose() method of previous screen if not called properly
anyway thanks for your help
Actually you do not act to Gesture on a Button. It's a click or a touch but not a gesture. Change this to a ClickListenerand the onClick action and it should work.
Regards

touch scrolling with libgdx

I'm trying to implement touch scrolling in a libgdx game. I have a wide image that is a panorama of a room. I want to be able to scroll the image so the user can see around the room. I have it so that I can scroll a certain distance but when a new touchDragged event is registered the image is moved back to the original position.
This is how I'm implementing it
public class AttackGame implements ApplicationListener {
AttackInputProcessor inputProcessor;
Texture backgroundTexture;
TextureRegion region;
OrthographicCamera cam;
SpriteBatch batch;
float width;
float height;
float posX;
float posY;
#Override
public void create() {
posX = 0;
posY = 0;
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
backgroundTexture = new Texture("data/pancellar.jpg");
region = new TextureRegion(backgroundTexture, 0, 0, width, height);
batch = new SpriteBatch();
}
#Override
public void resize(int width, int height) {
cam = new OrthographicCamera();
cam.setToOrtho(false, width, height);
cam.translate(width / 2, height / 2, 0);
inputProcessor = new AttackInputProcessor(width, height, cam);
Gdx.input.setInputProcessor(inputProcessor);
}
#Override
public void render() {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0, 2400, 460);
batch.end();
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
backgroundTexture.dispose();
}
}
And in the InputProcessor
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
cam.position.set(screenX, posY / 2, 0);
cam.update();
return false;
}
I got this far with help from this question LibGdx How to Scroll using OrthographicCamera?. However it doesn't really solve my problem.
I think the problem is with the touchDragged corodinates not being world coordinates but I have tried unprojecting the camera with no effect.
I have been struggling with this for a few weeks and I would really appreciate some help on this.
Thanks in advance.
I recently did something as what you want. This is my Input class that I use for move the map, you only need to change my 'stage.getCamera()' for your 'cam':
public class MapInputProcessor implements InputProcessor {
Vector3 last_touch_down = new Vector3();
...
public boolean touchDragged(int x, int y, int pointer) {
moveCamera( x, y );
return false;
}
private void moveCamera( int touch_x, int touch_y ) {
Vector3 new_position = getNewCameraPosition( touch_x, touch_y );
if( !cameraOutOfLimit( new_position ) )
stage.getCamera().translate( new_position.sub( stage.getCamera().position ) );
last_touch_down.set( touch_x, touch_y, 0);
}
private Vector3 getNewCameraPosition( int x, int y ) {
Vector3 new_position = last_touch_down;
new_position.sub(x, y, 0);
new_position.y = -new_position.y;
new_position.add( stage.getCamera().position );
return new_position;
}
private boolean cameraOutOfLimit( Vector3 position ) {
int x_left_limit = WINDOW_WIDHT / 2;
int x_right_limit = terrain.getWidth() - WINDOW_WIDTH / 2;
int y_bottom_limit = WINDOW_HEIGHT / 2;
int y_top_limit = terrain.getHeight() - WINDOW_HEIGHT / 2;
if( position.x < x_left_limit || position.x > x_right_limit )
return true;
else if( position.y < y_bottom_limit || position.y > y_top_limit )
return true;
else
return false;
}
...
}
This is the result: http://www.youtube.com/watch?feature=player_embedded&v=g1od3YLZpww
You need to do a lot more computation in the touchDragged callback, you can't just pass whatever screen coordinates were touched on to the camera. You need to figure out how far the user has dragged their finger, and in what direction. The absolute coordinates are not immediately useful.
Consider dragging down from the top-right or dragging down from the top-left. In both cases you want (I presume) to move the camera the same distance, but the absolute values of the screen coordinates will be very different in the two cases.
I think the simplest thing is to just track previousX and previousY (initialize them in the touchDown method. Then invoke cam.translate() with the delta (deltaX = screenX - previousX, for example), during touchDragged. And also update the previous* in touchDragged.
Alternatively, you can look at some of the fancier InputProcessor wrappers libgdx provides (see https://code.google.com/p/libgdx/wiki/InputGestureDetection).
Simple answer:
Declare 2 fields to hold the new and old drag location:
Vector2 dragOld, dragNew;
When just touched you set both of these equal to the touched location or your cam will jump.
if (Gdx.input.justTouched())
{
dragNew = new Vector2(Gdx.input.getX(), Gdx.input.getY());
dragOld = dragNew;
}
Update dragNew each frame and simply subtract the vectors from each other to get the x and y for translating the camera.
if (Gdx.input.isTouched())
{
dragNew = new Vector2(Gdx.input.getX(), Gdx.input.getY());
if (!dragNew.equals(dragOld))
{
cam.translate(dragOld.x - dragNew.x, dragNew.y - dragOld.y); //Translate by subtracting the vectors
cam.update();
dragOld = dragNew; //Drag old becomes drag new.
}
}
This is all I use to drag my ortho cam around, simple and effective.
Used drinor's answer but added the line on "touchDown) function so it doesn't reset the camera every time you start dragging again:
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
last_touch_down.set( screenX, screenY, 0);
return false;
}
I have not used it myself, but I would start by looking at the code of:
Have you looked at the code of http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/scenes/scene2d/ui/FlickScrollPane.html
See: touchDragged

Categories

Resources