Do something ontouch in libgdx - java

I have written the following code. It is simple rectangle in middle of the screen. I want the rectangle to move -50 in y direction onClick (direction is just for illustration purposes only). Despite trying various methods. I cannot get the code to work. I am not sure if I am doing something fundamentally wrong. I would appreciate some direction from better learned minds.
package com.mygdx.gameobjects;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Hero extends Rectangle implements InputProcessor{
private Rectangle hero;
private Vector2 position;
public Hero(){
position = new Vector2(x, y);
hero = new Rectangle(position.x+68-10, position.y+204-30-20, 20, 20);
}
public void update (float delta) {
}
public void onClick() {
position.y = -50;
}
public Rectangle getHero() {
return hero;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
onClick();
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#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 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;
}
}

Related

How can I move the input processor with the camera in LibGDX?

I recently started programming an app in LibGDX. With this app, one can only now press on boxes which are then filled in blue.
In principle everything works. The problem is only if I move the orthographic camera, or start to zoom, then my input processor still remains in the same place.
In short. Because the camera is moved or zoomed, the input does not work properly.
I have two times here Schreenshots attached so you can see what I mean.
The red dots are always where I pressed.
Regards Timux ;D
Here it works correctly
No more
No problem when I move or zoom my camera, I've tested, you can check this.
May be you're not using unproject() method of camera that translate a point given in screen coordinates to world space.
public class GdxText extends ApplicationAdapter implements InputProcessor {
OrthographicCamera cam;
Texture texture;
Sprite firstSprite,secondSprite;
SpriteBatch spriteBatch;
Vector3 vector3;
#Override
public void create() {
vector3=new Vector3();
cam=new OrthographicCamera();
texture=new Texture("badlogic.jpg");
float w=Gdx.graphics.getWidth();
float h=Gdx.graphics.getHeight();
spriteBatch=new SpriteBatch();
firstSprite=new Sprite(texture);
firstSprite.setSize(100,100);
firstSprite.setPosition(w/2-150,h/2-50);
secondSprite=new Sprite(texture);
secondSprite.setSize(100,100);
secondSprite.setPosition(w/2+50,h/2-50);
Gdx.input.setInputProcessor(this);
}
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
firstSprite.draw(spriteBatch);
secondSprite.draw(spriteBatch);
spriteBatch.end();
}
#Override
public void resize(int width, int height) {
cam.setToOrtho(false,width,height);
cam.update();
}
#Override
public boolean keyDown(int keycode) {
if(keycode== Input.Keys.UP) {
cam.zoom -= cam.zoom * .1;
cam.update();
}else if(keycode==Input.Keys.DOWN) {
cam.zoom += cam.zoom * .1;
cam.update();
}
if(keycode== Input.Keys.LEFT) {
cam.position.add(2f,0,0);
cam.update();
}else if(keycode==Input.Keys.RIGHT) {
cam.position.add(-2f,0,0);
cam.update();
}
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.set(screenX,screenY,0);
Vector3 ori=cam.unproject(vector3);
if(firstSprite.getBoundingRectangle().contains(ori.x,ori.y))
System.out.println("Touch on First Sprite");
if(secondSprite.getBoundingRectangle().contains(ori.x,ori.y))
System.out.println("Touch on Second Sprite");
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;
}
#Override
public void dispose() {
spriteBatch.dispose();
texture.dispose();
}
}

Libgdx: pause without triggering justtouch

I recently asked this question, it works perfectly, the only problem I'm having is that the whole game is based on touch events, when a user touches the screen, an object gets created.
What's happening now is that when a user touches the pause button (texture packer), an object gets created and the game is paused. I want to prevent objects from being created if the pause is touched. I used to be able to do something like this:
private Vector3 touchPos;
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
if (Gdx.input.justTouched()) {
if (touchPos.x > pauseX && touchPos.x < pauseX + pauseX) {
if (touchPos.y > pauseY && touchPos.y < pauseX + pauseY) {
setGamePause(!getGamePause());
}}}
But doesn't seem to be working with texture packer, maybe my implementation is wrong, not sure, is there another approach?
private float pauseY = Gdx.graphics.getHeight() - 115;
private float pauseX = Gdx.graphics.getWidth() / 6;
button.setSize(150, 150)
It's on the top left of the screen
If pauseX and pauseY is left position and right position of a rectangle/button respectively then
you need width and height of button/rectangular area, like suppose that buttonWidth, buttonHeight.
if(Gdx.input.justTouched()) {
if (touchPos.x > pauseX && touchPos.x < pauseX + button.getWidth()) {
if (touchPos.y > pauseY && touchPos.y < pauseY + button.getHeight()) {
setGamePause(!getGamePause());
}
}
}
Please Check Test :
public class GdxTest extends Game implements InputProcessor{
private Stage stage;
Vector3 vector3;
TextButton button;
float pauseX,pauseY;
#Override
public void create() {
vector3=new Vector3();
ExtendViewport extendViewport=new ExtendViewport(700,1200,new OrthographicCamera());
stage=new Stage(extendViewport);
Skin skin=new Skin(Gdx.files.internal("skin/uiskin.json"));
skin.get("font-label", BitmapFont.class).getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
pauseX=300;
pauseY=500;
button=new TextButton("Pause",skin);
button.setPosition(pauseX,pauseY);
stage.addActor(button);
Gdx.input.setInputProcessor(this);
}
#Override
public void render() {
super.render();
Gdx.gl.glClearColor(0,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act();
////Touch Detection without processor
vector3.set(Gdx.input.getX(),Gdx.input.getY(),0);
stage.getCamera().unproject(vector3);
if(Gdx.input.justTouched()){
if(vector3.x>pauseX && vector3.x<pauseX+button.getWidth() && vector3.y>pauseY && vector3.y<pauseY+button.getHeight())
System.out.println("TOuched");
}
///////
}
#Override
public void resize(int width, int height) {
super.resize(width,height);
stage.getViewport().update(width,height);
}
#Override
public void dispose() {
stage.dispose();
}
#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 button1) {
vector3.set(screenX,screenY,0);
stage.getCamera().unproject(vector3);
if(vector3.x>pauseX && vector3.x<pauseX+button.getWidth() && vector3.y>pauseY && vector3.y<pauseY+button.getHeight())
System.out.println("TOuched");
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;
}
}

Can't render a Tiled Map in libgdx

I have been trying to render a tiled map to my LibGDX project but nothing shown on the screen, I followed this tutorial from here http://www.gamefromscratch.com/post/2014/04/16/LibGDX-Tutorial-11-Tiled-Maps-Part-1-Simple-Orthogonal-Maps.aspx.
Could the problem be that I didn't set the position of the camera right?
This is my code
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.tiled.AtlasTmxMapLoader;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class Level extends ApplicationAdapter implements InputProcessor {
ActorDemo jet;
Stage stage;
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
AtlasTmxMapLoader test;
#Override
public void create () {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
jet = new ActorDemo();
stage = new Stage();
stage.addActor(jet);
camera = new OrthographicCamera();
camera.setToOrtho(false,w/2,h/2);
camera.update();
tiledMap = new TmxMapLoader().load("Level.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
Gdx.input.setInputProcessor(this);
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera.combined,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
tiledMapRenderer.render();
stage.draw();
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
if(keycode == Input.Keys.LEFT)
camera.translate(-32,0);
if(Gdx.input.isKeyPressed(Input.Keys.A)){
camera.position.x -=2;
}
if(keycode == Input.Keys.RIGHT)
camera.translate(32,0);
if(keycode == Input.Keys.UP)
camera.translate(0,-32);
if(keycode == Input.Keys.DOWN)
camera.translate(0,32);
if(keycode == Input.Keys.NUM_1)
tiledMap.getLayers().get(0).setVisible(!tiledMap.getLayers().get(0).isVisible());
if(keycode == Input.Keys.NUM_2)
tiledMap.getLayers().get(1).setVisible(!tiledMap.getLayers().get(1).isVisible());
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
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;
}
}
Screenshot of the Desktop configuration
Here is where I put my tiled map
This is the tile set I used to create my tmx file
I added this tile set to to the tiled map editor and created a tile sheet and then put it in my project
It worked, the problem was that the program was not able to find the tile Set, so I changed the path(from the .tmx file) to where it actually located and it worked.

How to allow user to touch scroll using libgdx in a cross-plattform project?

I'm developing a cross-plattform application in java (focusing on android right now) and I'm mainly using libgdx to achieve this. Right now I have a screen that consists of rows of clickable buttons where the rows extend below the screen. I want to allow the user to touch/flick scroll up and down in order for them to view all the different buttons available.
Some relevant information: I'm using an Orthographic Camera and an InputProcessor.
Right now I'm thinking that the camera is supposed to move up and down based on the touchDragged method in the InputProcessor.
I'll attach the relevant code in the GameRenderer and InputHandler classes below. (with a lot of unrelevant code removed.) Any help or suggestions would be great, I've googled like a mad man and been stuck for a day now.
GameRenderer
public class GameRenderer {
private GameWorld myWorld;
private OrthographicCamera cam;
private SpriteBatch batcher;
private ArrayList<Element> elements = new ArrayList<Element>();
private int gameHeight;
public GameRenderer(GameWorld world, int gameHeight) {
myWorld = world;
elements = myWorld.getElement();
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
cam.setToOrtho(true, 136, gameHeight);
batcher = new SpriteBatch();
batcher.setProjectionMatrix(cam.combined);
}
public void render() {
Gdx.gl.glClearColor(0, 191, 255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batcher.begin();
for (int i = 0; i < elements.size(); i++) {
Element e = (Element) elements.get(i);
batcher.draw(AssetLoader.element, e.getLocationX(), e.getLocationY(), e.getElementWidth(), e.getElementHeight());
AssetLoader.font.draw(batcher, e.getFormula(), e.getLocationX() + 4, e.getLocationY() + 3);
}
batcher.end();
}
}
InputHandler
public class InputHandler implements InputProcessor
{
private Element myElement;
private float scaleX, scaleY;
private ArrayList<Element> elements;
GameWorld myWorld;
GameRenderer myRenderer;
public InputHandler(GameWorld game, GameRenderer renderer, ArrayList elementList, float x, float y) {
myWorld = game;
myRenderer = renderer;
elements = elementList;
scaleX = x;
scaleY = y;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return true;
}
#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 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;
}
}

Issues with touchDragged on libgdx

below is my code which I am trying to add touchDragged, I am trying to use the drag method to make an object move left and right by using the touchDragged feature.
Problem is that when i swipe on the right side of the screen be it left or right swipe the object moves right only. when i swipe on the left side be it left or right the object moves left only.
is there something I am missing?
input class and below that the object class.
also is the Tilt action same as this or is that something different?
it will be nice if that can be explained.
Input class is as
public class Input implements InputProcessor
{
private Kame myKame;
public Input(Kame kame) {
myKame = kame;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
myKame.onClick();
return true; // Return true to say we handled the touch.
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
myKame.onClick();
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
and Kame class is as :
public class Kame
{
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
private int width;
private int height;
public float x;
public Kame(float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
velocity = new Vector2(0, 0);
acceleration = new Vector2(0, 460);
}
public void update(float delta) {
}
public boolean onSwipe(int velocityX, int velocityY, int delta) {
position.add(position.cpy().scl(delta));
// if (Math.abs(velocityX) < Math.abs(velocityY)) {
if (velocityX > 136) {
position.x += 3;//x cordinate
velocity.y += 10;
} else if (velocityX < 136) {
position.x -= 3;
velocity.y += 10;
} else {
// Do nothing.
}
// } else {
// Ignore the input, because we don't care about up/down swipes.
// }
return true;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}
This call to onSwipe:
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
does not seem to match this implementation of onSwipe:
public boolean onSwipe(int velocityX, int velocityY, int delta) {
There are a couple problems here.
The screenX and screenY parameters passed to touchDragged are screen coordinates. They range between 0 and the width (or height) of the screen. onSwipe says it expects a "velocity" (though the algorithm inside doesn't seem to treat it as such).
The pointer in the touchDragged signature is the id number of the cursor (think about using 4 fingers on a touchpad, the "pointer" identifies which finger is which). You're using is as a 'delta'.
You can probably just compare the screenX to pos.x, and add or subtract 3 from pos.x depending on if screenX is less than or greater than pos.x.
Computing an actual velocity will require tracking the change in location (which means keep track of the previous touch location), and then scaling it by the frame rate (the delta passed to render).
change the code from
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int button) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
try that

Categories

Resources