0

currently the method I used is to detect whether touch position (Gdx.input.getX() & Y()) is in the area whether the object texture is. If so I setPosition of the object texture to the mouse position as center. While this work but it is not robust. Because if my finger move faster than the update, as soon as the touched position is outside texture bound. It won't update any more.

There must be a more reliable way and please advice. Essentially, I want to touch on the texture and drag the texture to wherever my touch is moving.

Many thanks.

My current approach is like this:

public class PlayScreen implements Screen {
    int scWidth, scHeight;
    int playerWidth, playerHeight;
    private SpriteBatch batch; // This is in the render.
    Player player;
    // Create a constructor
    Game game;
    public PlayScreen(Game game){
        this.game = game;
    }

    @Override
    public void show() {
        batch = new SpriteBatch();
        scWidth = Gdx.graphics.getWidth();
        scHeight = Gdx.graphics.getHeight();
        playerWidth = 180;
        playerHeight = 240;
        player = new Player("mario.png", new Vector2(250, 300),new Vector2(playerWidth, playerHeight)) ;
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();

        // Only draw if the mouse is hover on the image.
        if (Gdx.input.getX() > player.getPosition().x && Gdx.input.getX() < player.getPosition().x + playerWidth){
            if (scHeight - Gdx.input.getY() > player.getPosition().y && scHeight - Gdx.input.getY() < player.getPosition().y + playerHeight)
            {

                player.setPosition(new Vector2(Gdx.input.getX() - playerWidth/2,
                        scHeight - Gdx.input.getY() - playerHeight/2));
                player.draw(batch);

            } else{
                player.draw(batch);
            }

        } else{
            player.draw(batch);
        }

        batch.end();
        player.update(); // Update the bound


    }
}

Also the Player class is :

package com.jiajunyang.emosonicsgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Image;


public class Player extends Image {

    Vector2 position, size;
    Texture player;
    Rectangle bounds;

public Player(String fileName, Vector2 position, Vector2 size){
    super(new Texture(Gdx.files.internal(fileName)));
    this.position = position;
    this.size = size;
    bounds = new Rectangle(position.x, position.y, size.x, size.y);

// player = new Texture(Gdx.files.internal(fileName)); }

    public void update(){
        bounds.set(position.x, position.y, size.x, size.y);
    }

    public void draw(SpriteBatch batch){
        batch.draw(player, position.x, position.y, size.x, size.y);
    }

    public Vector2 getPosition() {
        return position;
    }

    public void setPosition(Vector2 position) {
        this.position = position;
    }

    public Vector2 getSize() {
        return size;
    }

    public void setSize(Vector2 size) {
        this.size = size;
    }

    public Rectangle getBounds() {
        return bounds;
    }

    public void setBounds(Rectangle bounds) {
        this.bounds = bounds;
    }
}
0

Jiajun, create a Stage and have your Player extend Image. Then call stage.addActor(player) in show. In render call stage.act() and stage.draw(). Use the Image's constructor to pass your Texture into. Finally, in Player's constructor, call this:

addListener(new DragListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchDragged(InputEvent event, float x, float y, int pointer) {
                moveBy(x - getWidth()/2, y - getHeight()/2);
            }
        });
|improve this answer|||||
  • Hi Thanks for your comment. I already have a stage I used for a button I use to randomise some trees display. So now I extend my Player class with Image (although I have idea what extend means, :() I addActor the player. But I dont know how to "Use the Image's constructor to pass your Texture into" . Can you explain how this work please? – J_yang Jun 7 '16 at 11:46
  • Hi again, I just edited the Player class to the question. Can you tell me whether any more modification required? Thanks – J_yang Jun 7 '16 at 11:50
  • player = new Player("mario.png", new Vector2(250, 300),new Vector2(playerWidth, playerHeight)); - this is a call to Player's constructor, extending a class is java terminology, it means Player's superclass will be Image, so Player will essentially be an Image class with your modifications. In your public Player() - the constructor - use the Image's constructor by calling super(texture). – WonderfulWorld Jun 7 '16 at 11:52
  • So i changed the constructor of Player to this:public Player(String fileName, Vector2 position, Vector2 size){ super(new Texture(Gdx.files.internal(fileName))); this.position = position; this.size = size; bounds = new Rectangle(position.x, position.y, size.x, size.y); And now the Image is always at the left conner even I passed the initial position to something else. – J_yang Jun 7 '16 at 12:04
  • player.setBounds(...) – WonderfulWorld Jun 7 '16 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.