touch isn't detecting on libgdx button - java

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

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-

Add multiple InputListener to Actor libgdx

I have a problem with InputListener.
I have create class who extend TextButton to make TextButtons with borders and one default Inputlistener. And in my main code i want to add one more Inputlistener to set a new screen when it is pressed (in my code it's just a print to watch if that works) but only the touchDown works...
My TextButtonWithBorder class :
public class TextButtonWithBorder extends TextButton {
ShapeRenderer shapeRenderer = new ShapeRenderer();
public TextButtonWithBorder(String text, Skin skin) {
super(text, skin);
this.setTransform(true);
this.addReduceClickListener();
}
public TextButtonWithBorder(String text, Skin skin, String styleName) {
super(text, skin, styleName);
this.setTransform(true);
this.addReduceClickListener();
}
public TextButtonWithBorder(String text, TextButtonStyle style) {
super(text, style);
this.setTransform(true);
this.addReduceClickListener();
}
#Override
public void draw(Batch batch, float parentAlpha) {
batch.end();
shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.setColor(Color.WHITE);
shapeRenderer.rect(getX(),getY(),getWidth()*getScaleX(),getHeight()*getScaleY());
shapeRenderer.end();
batch.begin();
super.draw(batch, parentAlpha);
}
public void setCenter(float x,float y)
{
setPosition(x-getWidth()/2*getScaleX(),y-getHeight()/2*getScaleY());
}
public void setCenter(Vector2 center)
{
setPosition(center.x-getWidth()/2*getScaleX(),center.y-getHeight()/2*getScaleY());
}
public Vector2 getCenter()
{
return new Vector2(getX()+getWidth()/2*getScaleX(),getY()+getHeight()/2*getScaleY());
}
public void addReduceClickListener()
{
addListener((new InputListener(){
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
Vector2 center = getCenter();
setScale(0.9F);
setCenter(center);
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
Vector2 center = getCenter();
setScale(1F);
setCenter(center);
}
}));
}
public void dispose()
{
shapeRenderer.dispose();
}
}
And My main code :
public class MainMenuScreen implements Screen {
final PongAndroid game;
Stage stage;
BitmapFont font;
TextButtonWithBorder buttonOnePlayer;
TextButtonWithBorder buttonTwoPlayers;
TextButtonWithBorder buttonAbout;
TextButtonWithBorder buttonExit;
Label title;
ImageButton options;
public MainMenuScreen(final PongAndroid game) {
this.game=game;
stage = new Stage(game.viewport,game.batch);
Gdx.input.setInputProcessor(stage);
// Styles
font = new BitmapFont(Gdx.files.internal("MVboli50.fnt"));
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = font;
Label.LabelStyle labelStyle = new Label.LabelStyle();
labelStyle.font = font;
ImageButton.ImageButtonStyle imageButtonStyle = new ImageButton.ImageButtonStyle();
// Configure Actors
// title
title = new Label("Pong Android",labelStyle);
title.setFontScale(2f);
title.setPosition(game.WIDTH/2-title.getWidth()/2*title.getFontScaleX(),game.HEIGHT-title.getHeight()-(game.HEIGHT*0.15f));
// buttonOnePlayer
buttonOnePlayer = new TextButtonWithBorder("1 Player",textButtonStyle);
buttonOnePlayer.setWidth(game.WIDTH*0.70f);
buttonOnePlayer.setHeight(buttonOnePlayer.getHeight()*1.2f);
buttonOnePlayer.setPosition(game.WIDTH/2-buttonOnePlayer.getWidth()/2,title.getY()-title.getHeight()/2-buttonOnePlayer.getHeight()-game.HEIGHT*0.05f);
//buttonTwoPlayer
buttonTwoPlayers = new TextButtonWithBorder("2 Players",textButtonStyle);
buttonOnePlayer.setTransform(true);
buttonTwoPlayers.setWidth(buttonOnePlayer.getWidth());
buttonTwoPlayers.setHeight(buttonTwoPlayers.getHeight()*1.2f);
buttonTwoPlayers.setPosition(buttonOnePlayer.getX(),buttonOnePlayer.getY()-buttonOnePlayer.getHeight()-game.HEIGHT*0.05f);
//buttonAbout
buttonAbout = new TextButtonWithBorder("About",textButtonStyle);
buttonOnePlayer.setTransform(true);
buttonAbout.setWidth(buttonTwoPlayers.getWidth()/2-game.WIDTH*0.05f);
buttonAbout.setHeight(buttonAbout.getHeight()*1.2f);
buttonAbout.setPosition(buttonTwoPlayers.getX(),buttonTwoPlayers.getY()-buttonAbout.getHeight()-game.HEIGHT*0.05f);
//buttonExit
buttonExit = new TextButtonWithBorder("Exit",textButtonStyle);
buttonOnePlayer.setTransform(true);
buttonExit.setWidth(buttonAbout.getWidth());
buttonExit.setHeight(buttonExit.getHeight()*1.2f);
buttonExit.setPosition(buttonAbout.getX()+buttonAbout.getWidth()+game.WIDTH*0.1f, buttonAbout.getY());
// Add listeners to Actors
buttonExit.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return super.touchDown(event, x, y, pointer, button);
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up");
super.touchUp(event, x, y, pointer, button);
System.out.println("up");
}
});
// Add Actors to stage
stage.addActor(title);
stage.addActor(buttonOnePlayer);
stage.addActor(buttonTwoPlayers);
stage.addActor(buttonAbout);
stage.addActor(buttonExit);
//stage.addActor();
}
#Override
public void show() {
}
#Override
public void render(float delta) {
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
buttonOnePlayer.dispose();
buttonTwoPlayers.dispose();
buttonAbout.dispose();
buttonExit.dispose();
}
}
My question is how to add multiple InputListener to an Actor ?
Return true for both InputListeners to handle the touchUp event. This is my output when clicking once:
listener A: touchDown
listener B: touchDown
listener A: touchUp
listener B: touchUp
The code:
private SpriteBatch batch;
private Viewport viewport;
private Stage stage;
private Texture texture;
private BitmapFont font;
#Override
public void create() {
batch = new SpriteBatch();
viewport = new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
stage = new Stage( viewport, batch );
texture = new Texture( "badlogic.jpg" );
font = new BitmapFont();
Gdx.input.setInputProcessor( stage );
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(
new TextureRegionDrawable( new TextureRegion( texture ) ), null, null, font );
TextButton textButton = new TextButton( "text", textButtonStyle );
textButton.setPosition(
0.5f * Gdx.graphics.getWidth() - 0.5f * textButton.getWidth(),
0.5f * Gdx.graphics.getHeight() - 0.5f * textButton.getHeight()
);
textButton.addListener( new InputListener(){
#Override
public boolean touchDown( InputEvent e, float x, float y, int pointer, int button ) {
Gdx.app.log( "listener A", "touchDown" );
return true;
}
#Override
public void touchUp( InputEvent e, float x, float y, int pointer, int button ) {
Gdx.app.log( "listener A", "touchUp" );
}
} );
textButton.addListener( new InputListener(){
#Override
public boolean touchDown( InputEvent e, float x, float y, int pointer, int button ) {
Gdx.app.log( "listener B", "touchDown" );
return true;
}
#Override
public void touchUp( InputEvent e, float x, float y, int pointer, int button ) {
Gdx.app.log( "listener B", "touchUp" );
}
} );
stage.addActor( textButton );
}
#Override
public void dispose() {
batch.dispose();
stage.dispose();
texture.dispose();
font.dispose();
}
#Override
public void resize( int width, int height ) {
viewport.update( width, height, true );
}
#Override
public void render() {
Gdx.gl.glClearColor( 0, 0, 0, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
stage.act( Gdx.graphics.getDeltaTime() );
stage.draw();
}
Buttons in LibGDX already have built-in InputListeners that robustly handle different button and hover states. Instead of adding your own InputListener, you should add a ChangeListener to respond to button presses.

Java SWT Drawing Shapes on canvas with mouse events

I want to make an application in order to draw forms (rectangle, line, square, arrow) like in paint using Java SWT Canvas. I'm using mouse events (Up, Down and move) to get the canvas Y and X position. And i have a button for each form types that get canvas mouse position and draw the selected form using the mouse events. My problem is, when i draw the first form (Circle, square, line) everything works, but when draw the second, the first erase. How can I make the first form stay on drawn after redraw the canvas?
Variables:
private static boolean drag = false;
private Canvas compCanvas;
private Button btnSend, btnAdd,btnFreeHand,btnArrow,btnCircle,btnSquare,btnLine;
private Composite mainPanel;
compCanvas = new Canvas(mainPanel, SWT.NONE);
mouseEvents():
private void mouseEvents(){
compCanvas.addListener(SWT.MouseDown, new Listener(){
public void handleEvent(Event e){
System.out.println("Mouse event on canvas DOWN: X VALUE:"+e.x+"Y VALUE:"+e.y);
startY = e.y;
startX = e.x;
drag = true;
}
});
compCanvas.addListener(SWT.MouseUp, new Listener(){
public void handleEvent(Event e){
System.out.println("Mouse event on canvas UP: X VALUE:"+e.x+"Y VALUE:"+e.y);
endY = e.y;
endX = e.x;
drag = false;
//compCanvas.redraw();
}
});
compCanvas.addListener(SWT.MouseMove, new Listener(){
public void handleEvent(Event e){
System.out.println("Mouse event on canvas MOVE: X VALUE:"+e.x+"Y VALUE:"+e.y);
if(drag){
endY = e.y;
endX = e.x;
compCanvas.redraw();
}
}
});
};
btnSquare.selectionListener() and Declaration:
btnSquare = new Button(compSendAdd, SWT.NONE);
btnSquare.setLayoutData(new RowData(25, 25));
btnSquare.setImage(squareIcon);
btnSquare.addSelectionListener(new SelectionListener(){
private void btnSquare(){
mouseEvents();
//LightweightSystem lws = new LightweightSystem(compCanvas);
compCanvas.addListener(SWT.Paint, new Listener(){
public void handleEvent(Event e){
if(drag){
GC gc = e.gc;
//gc.setAlpha(128);
int minX = Math.min(startX,endX);
int minY = Math.min(startY,endY);
int maxX = Math.max(startX, endX);
int maxY = Math.max(startY, endY);
int width = maxX - minX;
int height = maxY - minY;
gc.fillRectangle(minX, minY,width,height);
}
}
});
}
public void widgetSelected(SelectionEvent event) {
btnSquare();
}
public void widgetDefaultSelected(SelectionEvent event) {
btnSquare();
}
});
By default controls are filled with current background color each time the SWT.Paint listener is called. You need to turn this off.
Do this by specifying the SWT.NO_BACKGROUND style on the Canvas
compCanvas = new Canvas(mainPanel, SWT.NO_BACKGROUND);
You will also need to fill the background the first time the canvas is drawn.
Create class shape with x, y, width, height fields
class Shape {
public int x; // coordiates
public int y;
public int width;
public int heigth;
String type; // "rect" for example
public Shape(int x, int y, int width, int height, String type) {
this.x = x;
this.y = y;
this.width = width;
this.heigth = height;
this.type = type;
}
}
After mouse up store your shape in list according to which button is selected
List<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Shape(x, y, width, height, getType()));
In PainListener You MUST redraw all shapes from your list
for(Shape s: shapes) {
//draw shape s
}

libGDX changing button's image

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.

libgdx drag and drop

Im trying to add drag and drop functionality to several images in Libgdx. I have looked at this example: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/DragAndDropTest.java but Its still not working. The images do not drag and drop. Would anyone be able to give me some pointers in why its not working?
Thanks
private void createButton() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
skin = new Skin();
skin.add("up", new Texture(Gdx.files.internal("assets/data/up.png")));
skin.add("def", new Texture(Gdx.files.internal("assets/data/Goal.png")));
final Image up = new Image(skin, "up");
up.setBounds(1090, 630, 40, 40);
stage.addActor(up);
Image def = new Image(skin, "def");
def.setBounds(1090, 585, 40, 40);
stage.addActor(def);
DragAndDrop dragAndDrop = new DragAndDrop();
dragAndDrop.addSource(new Source(up) {
public Payload dragStart (InputEvent event, float x, float y, int pointer) {
Payload payload = new Payload();
payload.setObject(payload);
payload.setDragActor(up);
payload.setDragActor(new Label("up", skin));
Label validLabel = new Label("up", skin);
validLabel.setColor(0, 1, 0, 1);
payload.setValidDragActor(validLabel);
return payload;
}
});
dragAndDrop.addTarget(new Target(def) {
public boolean drag (Source source, Payload payload, float x, float y, int pointer) {
getActor().setColor(Color.GREEN);
return true;
}
public void reset (Source source, Payload payload) {
getActor().setColor(Color.WHITE);
}
public void drop (Source source, Payload payload, float x, float y, int pointer) {
System.out.println("Accepted: " + payload.getObject() + " " + x + ", " + y);
}
});
render();
}
public void render () {
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
Table.drawDebug(stage);
}
I implemented your code on a project of mine.
I removed your render and used the one below. Also, you shouldn't need the assets/ prefix to your image import.
skin.add("up", new Texture(Gdx.files.internal("images/coin.png")));
skin.add("def", new Texture(Gdx.files.internal("images/coin.png")));
#Override
public void render(float delta) {
super.render(delta);
stage.draw();
stage.act(Gdx.graphics.getDeltaTime());
}
Another way to do drag in a better way...
public class CaveInterection implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture bgTexture;
private Sprite sprite;
private Stage stage;
private Texture mirrTexture;
private MyActor mirrorActor;
Sprite img1,img2;
#Override
public void create() {
camera = new OrthographicCamera(1024, 550);
camera.position.set(1024 / 2, 550 / 2, 0);
batch = new SpriteBatch();
stage = new Stage(1024, 550, false);
//bgTexture = new Texture(Gdx.files.internal("data/cave.jpg"));
//bgTexture = new Texture(Gdx.files.internal("data/bg.jpg"));
mirrTexture = new Texture(Gdx.files.internal("data/mirror.png"));
mirrTexture
.setFilter(TextureFilter.Linear, TextureFilter.Linear);
mirrorActor = new MyActor(new TextureRegion(mirrTexture));
mirrorActor.setPosition(700, 400);
mirrorActor.setOrigin(mirrorActor.getWidth()/2, mirrorActor.getHeight()/2);
stage.addActor(mirrorActor);
// finally stage as the input process
Gdx.input.setInputProcessor(stage);
}
#Override
public void dispose() {
batch.dispose();
//bgTexture.dispose();
}
#Override
public void render() {
// clear the screen, update the camera and make the sprite batch
// use its matrices.
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
//batch.draw(bgTexture, 0,0);
// Gdx.app.log("arvind","X :"+mirrorActor.getX()+ " Y :"+mirrorActor.getY());
//batch.draw(bgTexture, 0, 0);
batch.end();
// tell the stage to act and draw itself
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
public class MyActor extends Actor {
TextureRegion region;
float lastX;
float lastY;
public MyActor (TextureRegion region) {
this.region = region;
setWidth(region.getRegionWidth());
setHeight(region.getRegionHeight());
addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("arv", "pointer1+"+pointer);
// we only care for the first finger to make things easier
if (pointer != 0) return false;
// record the coordinates the finger went down on. they
// are given relative to the actor's corner (0, 0)
Gdx.app.log("arvind", "touchDown");
Gdx.app.log("arv", "pointer2+"+pointer+""+x+"::::"+y);
lastX = x;
lastY = y;
return true;
}
public void touchDragged (InputEvent event, float x, float y, int pointer) {
// we only care for the first finger to make things easier
if (pointer != 0) return;
Gdx.app.log("arv", "touchDragged");
// adjust the actor's position by (current mouse position - last mouse position)
// in the actor's coordinate system.
translate(x - lastX, y - lastY);
// rotate(2);
// save the current mouse position as the basis for the next drag event.
// we adjust by the same delta so next time drag is called, lastX/lastY
// are in the actor's local coordinate system automatically.
lastX = x - (x - lastX);
lastY = y - (y - lastY);
}
});
}
#Override
public void draw (SpriteBatch batch, float parentAlpha) {
//batch.draw(region, getX(), getY());
batch.draw(region, getX(), getY(), mirrorActor.getOriginX(), mirrorActor.getOriginY(), mirrorActor.getWidth(), mirrorActor.getHeight(), 1, 1,getRotation(), true);
}
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}

Categories

Resources