Skip to content

Instantly share code, notes, and snippets.

@cypherdare
Created March 13, 2016 19:28
Show Gist options
  • Save cypherdare/4aa45a300c46e12b1ce2 to your computer and use it in GitHub Desktop.
Save cypherdare/4aa45a300c46e12b1ce2 to your computer and use it in GitHub Desktop.
Example of enter and exit called on clicked button
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
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.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
public class MyGdxGame extends ApplicationAdapter {
private Stage stage;
private Pixmap pixmap;
Texture texture;
@Override
public void create () {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
pixmap = new Pixmap(2, 2, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
texture = new Texture(pixmap);
TextureRegionDrawable up = new TextureRegionDrawable(new TextureRegion(texture));
Drawable down = up.tint(Color.BLUE);
for (int i = 0; i < 2; i++) {
Button button = new Button(up, down);
button.setX(MathUtils.random(0, Gdx.graphics.getWidth()-100));
button.setY(MathUtils.random(0, Gdx.graphics.getHeight()-100));
button.setWidth(MathUtils.random(50, 100));
button.setHeight(MathUtils.random(50, 100));
button.addListener(enterExitListener);
stage.addActor(button);
}
}
@Override
public void render () {
Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void resize (int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void dispose () {
stage.dispose();
pixmap.dispose();
texture.dispose();
}
private ClickListener enterExitListener = new ClickListener(){
public void enter (InputEvent event, float x, float y, int pointer, Actor fromActor) {
super.enter(event, x, y, pointer, fromActor);
Gdx.app.log("enterExitListener", "entered ");
}
public void exit (InputEvent event, float x, float y, int pointer, Actor toActor) {
super.exit(event, x, y, pointer, toActor);
Gdx.app.log("enterExitListener", "exited ");
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment