简体   繁体   English

Libgdx,Android,如何拖动和移动纹理

[英]Libgdx, Android, how to drag and move a texture

currently the method I used is to detect whether touch position (Gdx.input.getX() & Y()) is in the area whether the object texture is. 目前,我使用的方法是检测触摸位置(Gdx.input.getX()和Y())是否在该区域中是否是对象纹理。 If so I setPosition of the object texture to the mouse position as center. 如果是这样,我将对象纹理的Position设置为以鼠标位置为中心。 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 : Player类也是:

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

Jiajun, create a Stage and have your Player extend Image. 贾俊,创建一个舞台,让您的播放器扩展图像。 Then call stage.addActor(player) in show. 然后在show中调用stage.addActor(player)。 In render call stage.act() and stage.draw(). 在渲染调用中,stage.act()和stage.draw()。 Use the Image's constructor to pass your Texture into. 使用Image的构造函数将您的Texture传入。 Finally, in Player's constructor, call this: 最后,在Player的构造函数中,调用此命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM