本文介绍了在拖动的libgdx SpriteBatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个游戏,我要拖屏幕的对象。我正在用在这里我使用这样只使用一个纹理区域旋转SpriteBatch.draw(....)方法Screen对象。我要实现对物体拖,但我无法启动拖动的一部分。


解决方案

 包com.dance.utils;进口com.badlogic.gdx.input.GestureDetector;
进口com.badlogic.gdx.input.GestureDetector.GestureListener;
进口com.badlogic.gdx.math.Vector2;
进口com.badlogic.gdx.math.Vector3;
进口com.dance.screen.GameScreen;
公共类GameTouchProcessor
{
私人GameScreen gameScreen;
接触点的Vector3 =新的Vector3();
的Vector3 relativeTouchPoint =新的Vector3();Vector2 lastPosition =新Vector2();GestureListener听者=新GestureListener()
{    @覆盖
    公共布尔变焦(浮动为arg0,ARG1浮动){
        // TODO自动生成方法存根
        返回false;
    }
    @覆盖
    公共布尔一扔(浮点为arg0,ARG1浮球,诠释ARG2){
        // TODO自动生成方法存根
        返回false;
    }    @覆盖
    公共布尔长preSS(浮动为arg0,ARG1浮动){
        // TODO自动生成方法存根
        返回false;
    }    @覆盖
    公共布尔锅(浮动为arg0,ARG1浮球,浮球ARG2,浮ARG3){
        如果(gameScreen.isPlayerDraged)
        {
        gameScreen.cam.unproject(relativeTouchPoint.set(为arg0,ARG1,0));
        GameScreen.position.set(lastPosition.x + relativeTouchPoint.x - touchPoint.x,lastPosition.y + relativeTouchPoint.y - touchPoint.y);
        }
        返回false;
    }    @覆盖
    公共布尔panStop(浮点为arg0,ARG1浮动,诠释ARG2,诠释ARG3){
        // TODO自动生成方法存根
        返回false;
    }    @覆盖
    公共布尔龙头(浮动为arg0,ARG1浮动,诠释ARG2,诠释ARG3){
        // TODO自动生成方法存根
        返回false;
    }    @覆盖
    公共布尔触地(浮动为arg0,ARG1浮动,诠释ARG2,诠释ARG3){
        gameScreen.cam.unproject(touchPoint.set(为arg0,ARG1,0));
        lastPosition.set(GameScreen.position); //这里的位置是一个VECTOR2我在画我的对象,所以当位置改变物体运动这个位置
        返回false;
    }
    @覆盖
    公共布尔捏(Vector2为arg0,ARG1 Vector2,Vector2 ARG2,
            Vector2 ARG3){
        // TODO自动生成方法存根
        返回false;
    }
};公共GestureDetector探测器=新GestureDetector(听者)
{    公共布尔润色(INT为arg0,ARG1 INT,INT ARG2,诠释ARG3)
    {
        gameScreen.isPlayerDraged = FALSE;
        返回false;
    }
};
公共GameTouchProcessor(GameScreen gameScreen)
{
    this.gameScreen = gameScreen;
}

}

在您的游戏画面(或u想拖为准屏)把这个

  inputMultiplexer =新InputMultiplexer();
    inputMultiplexer.addProcessor(游戏);
    inputMultiplexer.addProcessor(新GameTouchProcessor(本).detector);
    Gdx.input.setInputProcessor(inputMultiplexer);

不要混淆与我的code的片段

在gamescreen我在画上vector2的现在的位置是我的目标,我对矫正在PAN方法vector2

有关输入处理器更好的信息

实际上Libgdx所有输入上一个单独的线程运行,并且一个输入处理器被分配给它。但是,当你想使自己的proceesor像你的情况u想一个泛回调拖动对象。

因此​​,如果您添加proccesor比GDX默认proceesor不会工作,你不会得到许多重要的输入呼叫回来,你必须明确地处理它们。

因此​​,为了避免我们创建一个多路复用器和两个输入proceesor和自定义添加到它,所以它与所有的回调作品

在这里我的游戏类实现inputproceesor(默认的)。我已经合并的默认输入处理器与我的自定义的

I am making a game where I have to drag objects of a Screen. I am making Screen objects using SpriteBatch.draw(....) method where I am using rotation so using only one Texture Region. I have to implement drag on object but I am unable to start the dragging part.

解决方案
package com.dance.utils;

import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.dance.screen.GameScreen;


public class GameTouchProcessor
{
private GameScreen gameScreen;
Vector3 touchPoint=new Vector3();
Vector3 relativeTouchPoint=new Vector3();

Vector2 lastPosition = new Vector2();

GestureListener listner=new GestureListener()
{

    @Override
    public boolean zoom(float arg0, float arg1) {
        // TODO Auto-generated method stub
        return false;
    }


    @Override
    public boolean fling(float arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean longPress(float arg0, float arg1) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean pan(float arg0, float arg1, float arg2, float arg3) {
        if(gameScreen.isPlayerDraged)
        {
        gameScreen.cam.unproject(relativeTouchPoint.set(arg0, arg1, 0));
        GameScreen.position.set(lastPosition.x + relativeTouchPoint.x - touchPoint.x, lastPosition.y + relativeTouchPoint.y - touchPoint.y);
        }
        return false;
    }

    @Override
    public boolean panStop(float arg0, float arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean tap(float arg0, float arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(float arg0, float arg1, int arg2, int arg3) {
        gameScreen.cam.unproject(touchPoint.set(arg0, arg1, 0));
        lastPosition.set(GameScreen.position); //HERE POSITION IS A VECTOR2 I AM DRAWING MY OBJECT ON THIS POSITION SO WHEN POSITION IS CHANGED OBJECT MOVES
        return false;
    }


    @Override
    public boolean pinch(Vector2 arg0, Vector2 arg1, Vector2 arg2,
            Vector2 arg3) {
        // TODO Auto-generated method stub
        return false;
    }
};

public GestureDetector  detector=new GestureDetector(listner)
{

    public boolean touchUp(int arg0, int arg1, int arg2, int arg3)
    {
        gameScreen.isPlayerDraged=false;
        return false;
    }
};


public GameTouchProcessor(GameScreen gameScreen)
{
    this.gameScreen=gameScreen;
}

}

IN YOUR GAME SCREEN (or whichever screen u want to drag)PUT THIS

    inputMultiplexer=new InputMultiplexer();
    inputMultiplexer.addProcessor(game);
    inputMultiplexer.addProcessor(new GameTouchProcessor(this).detector);
    Gdx.input.setInputProcessor(inputMultiplexer);

Dont get confused with snippets of my code

in gamescreen i am drawing my object on postion of a vector2 and i am modifing the vector2 in PAN method

for a better info on input processor

input processor

actually all the input in Libgdx is running on a seperate thread and a input processor is assigned to it. But when you want to make your own proceesor like in your case u wanted a pan callback to drag object.

So if you add your proccesor than Gdx default proceesor wont work and you will not get many of the important input call back so you will have to handle them explicitly.

So to avoid that we create a multiplexor and add both the input proceesor and your custom to it so it works with all the call backs

here my game class is implementing the inputproceesor (default one). i have merged the default input processor with my custom one

这篇关于在拖动的libgdx SpriteBatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:03