libGDX Stage input handling - java

I have an Stage class that handles touch input.
In the Screen class I set the stage as InputProcessor:
stageTest = new StageTest(new ScreenViewport());
Gdx.input.setInputProcessor(stageHUD);
But now I want to add a force to an Box2d object always a gesture input happens.
public class ActSwipe extends Actor {
private int tmpPointer;
private float
tmpX,
tmpY,
deltaX,
deltaY,
rad;
protected float
forceX,
forceY;
public ActSwipe() {
this.setName("SwipeAction");
this.setTouchable(Touchable.enabled);
this.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if(tmpPointer == 0) {
tmpPointer = pointer;
tmpX = x;
tmpY = y;
}
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
if (tmpPointer == pointer) {
tmpPointer = 0;
deltaX = x - tmpX;
deltaY = y - tmpY;
rad = (float) Math.atan2(deltaY, deltaX);
forceX = (float) Math.cos(rad);
forceY = (float) Math.sin(rad);
}
}
});
}
}

You can implement InputProcessor (or extend InputAdapter) in your screen and override its methods with your code.
Then use InputMultiplexer like this:
InputMultiplexer multiplexer = new InputMultiplexer();
Gdx.input.setInputProcessor(multiplexer);
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage)
In the overridden methods you need to make sure that an object that was hit is an object that should be handled by your input processor and not the one from the scene. If so, return true from the method, so the event won't be passed to the latter.

Related

Actor not moving in screen2d

I want to use moveToAction and move the actor. But its not moving if i use moveToAction. If change x,y in draw method it works, but not with movetoaction
public class Aks extends Actor {
private State state;
private MainGame game;
private TextureAtlas movingAtlas;
private Animation movingAnimation;
private float elapsedTime = 0f;
public Aks(MainGame game) {
this.game = game;
movingAtlas = new TextureAtlas(Gdx.files.internal("atlas/myaltas/atlas.atlas"));
movingAnimation = new Animation(1f/15f, movingAtlas.getRegions());
TextureRegion texture = (TextureRegion) movingAnimation.getKeyFrame(elapsedTime, true);
setBounds(getX(),getY(),texture.getRegionWidth(),texture.getRegionHeight());
MoveToAction moveAction = new MoveToAction();
moveAction.setPosition(300f, 300f);
moveAction.setDuration(10f);
this.addAction(moveAction);
addListener(new ActorGestureListener(){
#Override
public void tap(InputEvent event, float x, float y, int count, int button) {
Gdx.app.log("Tag", "Actor touched x = " );
super.tap(event, x, y, count, button);
}
});
}
#Override
public void draw(Batch batch, float alpha){
drawFlying(batch);
}
void drawFlying(Batch batch){
TextureRegion texture = (TextureRegion) movingAnimation.getKeyFrame(elapsedTime, true);
setBounds(getX(),getY(),texture.getRegionWidth(),texture.getRegionHeight());
Gdx.app.log("Tag", "x =" + getX() + " y =" + getY() );
batch.draw(texture, getX(),getY(),50,50);
}
#Override
public void act(float delta){
elapsedTime = elapsedTime+delta;
}
}
You needs to call super.act(delta), in the actor class, the act method control all the actions so if you don't call it, the actions wont happen, on the contrary you don't need to call super.draw() that's because actor class was created just for logical part of a entity, you must define what it will draw in case you need it.
I hope it helps!
Your draw function needs to call super.draw(batch, alpha) for it to affect child actions.

Scroll up and down by drag the table

table1.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
scroll=y;
System.out.print(scroll);
return true;
}
#Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
table1.setPosition((scroll+(y)));
}});
I want to scroll my table up and down by dragging my table (like Facebook on phone) I can't make this math thing on the Y I don't know why when I drag it it goes crazy.
Parameters x and y in the methods of InputListener are local coordinates (i.e. relative to your table), so you need to convert them to table's parent coordinate system:
table.addListener(new InputListener() {
Vector2 vec = new Vector2();
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
table.localToParentCoordinates(vec.set(x, y));
return true;
}
#Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
float oldTouchY = vec.y;
table.localToParentCoordinates(vec.set(x, y));
table.moveBy(0f, vec.y - oldTouchY);
}
});
By the way, using ScrollPane will probably be much better and convenient solution in your case: https://github.com/libgdx/libgdx/wiki/Scene2d.ui#scrollpane
Upd:
Actually, since you only need deltaY you can do this without converting coordinates:
table.addListener(new InputListener() {
float touchY;
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
touchY = y;
return true;
}
#Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
// since you move your table along with pointer, your touchY will be the same in table's local coordinates
float deltaY = y - touchY;
table.moveBy(0f, deltaY);
}
});

Libgdx Input don't working on Android

I'm new in Libgdx and I'm trying to make a map that can be explored using Camera. Fo that I implements GestureListener in my own Map class.
public class Map extends Stage implements GestureListener {
public String mapName;
private Sprite background;
public LocationPoint points[];
private OrthographicCamera camera;
private Batch batch;
public Music anbientSound;
public int numOfPoints;
public int locationsX[];
public int locationsY[];
public Map(Sprite background) {
this.background = background;
}
public Sprite getBackground() {
return background;
}
public void activate() {
InputMultiplexer inputChain = new InputMultiplexer();
if(points==null) {
points = new LocationPoint[numOfPoints];
for(int i = 0; i < numOfPoints; i++) {
points[i] = new LocationPoint(locationsX[i], locationsY[i]);
addActor(points[i]);
}
}
batch = GameUtils.batch;
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0);
camera.update();
Music music = GameUtils.addMusic(anbientSound);
music.setLooping(true);
music.play();
inputChain.addProcessor(this);
inputChain.addProcessor(new GestureDetector(this));
Gdx.input.setInputProcessor(inputChain);
}
public void draw() {
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(Gdx.gl20.GL_COLOR_BUFFER_BIT);
Batch batch = this.batch;
batch.setProjectionMatrix(camera.combined);
batch.begin();
background.draw(batch);
batch.end();
batch.begin();
for(int i = 0; i < numOfPoints; i++) {
points[i].draw(batch, 1);
addActor(points[i]);
}
batch.end();
}
public void dispose() {
GameUtils.stopMusic();
background.getTexture().dispose();
anbientSound.dispose();
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
camera.translate(-deltaX, deltaY);
float initialX = camera.viewportWidth / 2;
float initialY = camera.viewportHeight / 2;
GameUtils.limitBound(camera, initialX, initialY, background.getWidth(), background.getHeight());
camera.update();
return true;
}
#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 panStop(float x, float y, int pointer, int button) {
return false;
}
#Override
public boolean zoom(float initialDistance, float distance) {
return false;
}
#Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
Vector2 pointer1, Vector2 pointer2) {
return false;
}
#Override
public void pinchStop() {}
}
The method activate() is used to activate the resources of the Map class. The class Map also have a ImageButtons called LocationPoints.
public class LocationPoint extends ImageButton {
private Monster monster;
private Trap trap;
public boolean occuped;
public boolean isTrap;
public int f = 20;
public int k = 20;
public LocationPoint(float x, float y) {
super(GameUtils.getLocationDrawable());
this.setSize(46, 46);
setPosition(x, y);
addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
setPosition(f, k);
f += 10;
k += 10;
return super.touchDown(event, x, y, pointer, button);
}
});
}
public void occup(Monster monster) {
this.monster = monster;
occuped = true;
if(isTrap)
captured();
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
}
public void empty() {
monster = null;
occuped = false;
}
public void captured() {
monster.capture();
}
public Monster getMonster() {
return monster;
}
}
In LocationPoint class I add a InputListener to make a thing when that LocationPoint is touched.
When a play the game on android both LocationPoints event and the Map pan event. But after I move the camera, when I touch on LocationPoint it don't fires that point event.
But when I return the camera to initial position the LocationPoints events works fine! Can you help me? (And sorry for my broken english...)
Actor is already a child so remove redundant call from draw() method.
for(int i = 0; i < numOfPoints; i++) {
points[i].draw(batch, 1);
addActor(points[i]); // <-- Not Should be in draw() call
}
Stage having own SpriteBatch that created by default constructor, use that one or pass own batch in Stage constructor.
Use getBatch() method of Stage if you want to draw something by yourself.
Creating camera in Map class also redundant, use getViewport().getCamera() that return camera of stage, you can typecast to OrthographicCamera
call super.dispose(); inside your dispose() method
Drawing all your Actor by yourself ? If you're not doing something beyond the scope, no need to override draw() method of Stage.

Creating a playButton in libGdx with InputHandler

I've created a game that uses 4 different GameStates: READY,RUNNING,GAMEOVER,and HIGHSCORE(variation of GAMEOVER except this one notifies the player that a highscore has been reached). My issue is that the way I've set up my InputHandler is that in the GameState.READY form the user can touch anywhere in the screen to advance into the GameState.RUNNING form. I've tried multiple tactics into creating a playButton, however nothing seems to be working my way. I created a PlayButton class as such:
public class PlayButton {
private Vector2 position;
private Rectangle boundingRectangle;
private int width;
private int height;
private PlayScreen playScreen;
public PlayButton (float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
boundingRectangle = new Rectangle();
}
public void update(float delta) {
boundingRectangle.set(position.x,(position.y),width,height);
}
public float getTheX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
public Rectangle getBoundingRectangle(){
return boundingRectangle;
}
}
and in my InputHandler i tried this:
public InputHandler(GameWrold myWorld, float scaleFactorX, float scaleFactorY){
this.myWorld = myWorld;
mySam = myWorld.getSam();
playButton = new PlayButton(45,gameHeight-75,50,-35);
buttonPlay = myWorld.getPlayButton();
int midPointY = myWorld.getMidPointY();
this.scaleFactorX = scaleFactorX;
this.scaleFactorY = scaleFactorY;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector2 touchPos = new Vector2();
touchPos.set(Gdx.input.getX(), Gdx.input.getY());
Vector2 tuch = new Vector2(screenX,screenY);
buttonPlay.getBoundingRectangle();
touch = new Rectangle(touchPos.x,touchPos.y,5,5);
if (myWorld.isReady()) {
if(playButton.getBoundingRectangle().contains(touchPos)){
myWorld.start();
}
}
mySam.onClick();
if (myWorld.isGameOver() || myWorld.isHighScore()) {
// Reset all variables, go to GameState.READ
myWorld.restart();
}
return true;
}
}
As you can see I tried creating a Rectangle with the touch variable and I tried checking if touch collided with the playButton boundingRectangle like this:
if(Intersector.overlaps(touch,playButton.getBoundingRectangle())){ or
(playButton.getBoundingRectangle().overlaps(playButton.getBoundingRectangle()))
Do not invent a circle and don't break open doors.
Use Scene2d :)
It is a simple UI framework fully compatible with libGDX that allows you to create Buttons, Sliders, Windows and another widgets in about one line of code.
You will find a nice description in a link I've attached above but TLDR version is:
Create Skin and pack a texture atlas for this using TexturePacker (free version is enough usually)
Create a Stage with appropriate Viewport
Stage stage = new Stage(new FitViewport(WIDTH, HEIGHT));
Set the stage as input processor (it means that all event on the stage will be catched)
Gdx.input.setInputProcessor(stage);
Create a Button with appropriate style (defined in Skin)
Button button = new Button(skin, "style");
Attach a ClickListener with appropriate action to the Button
button.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y)
{
//some action
}
});
Set it's position and add it to the stage
button.setPosition(x, y);
stage.addActor(button);
Fire act() and draw() stage's methods in your render()
//render()
stage.act();
stage.draw();
You've earned few hours ;)

LIBGDX background for main menu is showing up white

My background image is not showing up, it shows up as a white square in the corner like this.
https://dl.dropboxusercontent.com/u/45938379/menu.png
I need to know how to fix this, my actors are showing behind it as you can see.
Here is my code
public class MainMenu implements Screen {
CrazyZombies game;
Stage stage;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
Button play, option, quit, custom, store;
TextureRegion backGround;
public MainMenu(CrazyZombies game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
batch.begin();
stage.draw();
drawBackGround();
batch.end();
}
#Override
public void resize(int width, int height) {
if (stage == null)
stage = new Stage(width, height, true);
stage.clear();
Gdx.input.setInputProcessor(stage);
/**
* quit Button
*/
TextButtonStyle styleQuit = new TextButtonStyle();
styleQuit.up = skin.getDrawable("8layer");
styleQuit.down = skin.getDrawable("8layer");
quit = new Button(styleQuit);
quit.setWidth(854);
quit.setHeight(480);
quit.setX(Gdx.graphics.getWidth() / 2 - quit.getWidth() / 2);
quit.setY(Gdx.graphics.getHeight() / 2 - quit.getHeight() / 2);
quit.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
}
});
/**
* End quit Button
*/
/**
* store Button
*/
TextButtonStyle styleStore = new TextButtonStyle();
styleStore.up = skin.getDrawable("9layer");
styleStore.down = skin.getDrawable("9layer");
store = new Button(styleStore);
store.setWidth(854);
store.setHeight(480);
store.setX(Gdx.graphics.getWidth() / 2 - store.getWidth() / 2);
store.setY(Gdx.graphics.getHeight() / 2 - store.getHeight() / 2);
store.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new StoreScreen(game));
}
});
/**
* End store Button
*/
/**
* customs Button
*/
TextButtonStyle styleCustom = new TextButtonStyle();
styleCustom.up = skin.getDrawable("10layer");
styleCustom.down = skin.getDrawable("10layer");
custom = new Button(styleCustom);
custom.setWidth(854);
custom.setHeight(480);
custom.setX(Gdx.graphics.getWidth() / 2 - custom.getWidth() / 2);
custom.setY(Gdx.graphics.getHeight() / 2 - custom.getHeight() / 2);
custom.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new CustomScreen(game));
}
});
/**
* End customs Button
*/
/**
* Options Button
*/
TextButtonStyle styleOptions = new TextButtonStyle();
styleOptions.up = skin.getDrawable("11layer");
styleOptions.down = skin.getDrawable("11layer");
option = new Button(styleOptions);
option.setWidth(854);
option.setHeight(480);
custom.setX(Gdx.graphics.getWidth() / 2 - custom.getWidth() / 2);
custom.setY(Gdx.graphics.getHeight() / 2 - custom.getHeight() / 2);
option.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new OptionScreen(game));
}
});
/**
* End Options Button
*/
/**
* Play Button
*/
TextButtonStyle stylePlay = new TextButtonStyle();
stylePlay.up = skin.getDrawable("7layer");
stylePlay.down = skin.getDrawable("7layer");
play = new Button(stylePlay);
play.setWidth(854);
play.setHeight(480);
play.setX(Gdx.graphics.getWidth() / 2 - play.getWidth() / 2);
play.setY(Gdx.graphics.getHeight() / 2 - play.getHeight() / 2);
play.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new GameScreen(game));
}
});
/**
* End Play Button
*/
stage.addActor(play);
stage.addActor(option);
stage.addActor(store);
stage.addActor(custom);
stage.addActor(quit);
}
#Override
public void show() {
Audio.playMusic(true);
batch = new SpriteBatch();
atlas = new TextureAtlas("data/mainmenu/mainmenu.pack");
skin = new Skin();
skin.addRegions(atlas);
backGround = atlas.findRegion("background");
backGround.getRegionHeight();
backGround.getRegionWidth();
}
public void drawBackGround() {
float w = 854;
float h = 480;
float y = 0;
float x = 0;
batch.draw(backGround, x, y, w, h);
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
}
}
What i have also noticed is that if i get rid of
stage.draw();
The image shows up.
Take out your stage.draw() out of the batch.begin() and batch.end(). The stage does have it's own Spritebatchso you do have concurenting ones at the moment. I think that does cause the troubles. So best way would be this:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
batch.begin();
drawBackGround();
batch.end();
}
I would recommend that you put your background inside the stage. Image is also an Actor so you can add it to your stage and than call the .toBack() (Back) to have it in the background.
Libgdx Image

Categories