Dragging in libgdx on SpriteBatch

六眼飞鱼酱① 提交于 2019-12-12 02:53:41

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/20392323/dragging-in-libgdx-on-spritebatch

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