Libgdx, Android, how to drag and move a texture

好久不见. 提交于 2019-12-25 07:14:38

问题


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;
    }
}

回答1:


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);
            }
        });


来源:https://stackoverflow.com/questions/37674054/libgdx-android-how-to-drag-and-move-a-texture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!