Libgdx,Android,如何拖动和移动纹理 [英] Libgdx, Android, how to drag and move a texture

查看:115
本文介绍了Libgdx,Android,如何拖动和移动纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我使用的方法是检测触摸位置(Gdx.input.getX()和Y())是否在对象纹理是否存在的区域中.如果是这样,我将对象纹理的Position设置为以鼠标位置为中心.虽然这项工作但是并不强大.因为如果我的手指移动的速度快于更新的速度,那么一旦触摸的位置超出了纹理范围,就可以了.它不会再更新了.

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.

非常感谢.

我当前的方法是这样的:

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


    }
}

Player类也是:

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

// 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,创建一个Stage并让您的Player扩展Image.然后在show中调用stage.addActor(player).在渲染调用中,stage.act()和stage.draw().使用Image的构造函数将您的Texture传递到其中.最后,在Player的构造函数中,调用此命令:

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

这篇关于Libgdx,Android,如何拖动和移动纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆