【问题标题】:Libgdx multiTouch not workingLibgdx multiTouch 不工作
【发布时间】:2016-05-15 06:21:12
【问题描述】:

我正在开发一个游戏,我在这个游戏中使用了 Libgdx 库。它将在 Android 上运行,并且必须在一个屏幕上与两个玩家一起玩。但我无法获得其他玩家的输入。我可以和底线或顶线球员一起玩,但不能同时玩。

这是获取输入代码:

public class PlayStateInput implements InputProcessor {

private PlayState playState;
private Vector2 touchPos;
private Vector2 bodyCord;


public PlayStateInput(PlayState playState){

    this.playState = playState;
    touchPos = new Vector2();
    bodyCord = new Vector2();

    touchPos.x=Gdx.input.getX();
    touchPos.y=Gdx.input.getY();
    bodyCord.x=playState.getGameWorld().getPaddle().getBody().getPosition().x;
    bodyCord.y=playState.getGameWorld().getPaddle().getBody().getPosition().y;

}

@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    if(playState.getGameWorld().getPuck().getCircleRect().contains(screenX,screenY)){

        System.out.println("collision");
    }

    if(screenY > Gdx.graphics.getHeight()/2 && (pointer <= 2)){
        playState.getGameWorld().getPaddle2().setBottomPaddle(true);

    }

    if(screenY < Gdx.graphics.getHeight()/2 && (pointer <= 2)){
        playState.getGameWorld().getPaddle().setTopPaddle(true);
    }

    return false;
}

并在此处使用此输入:

public class Paddle implements GameObject {


private World world;
private Body body;
private Body body2;
private BodyDef bodyDef;
private BodyDef bodyDef2;
private Fixture fixture;
private Fixture fixture2;
private FixtureDef fixtureDef;
private FixtureDef fixtureDef2;


private Circle circleRect;
private Circle circleRect2;
boolean TopPaddle = false;
boolean BottomPaddle = false;

private float PPM=100f;
private float power=100f;

private Vector2 touchPos;

private Sprite sprite;

String koordinatlar;


public Paddle(World world){


    this.world = world;

    bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set((Gdx.graphics.getWidth()/2)/PPM,(Gdx.graphics.getHeight()/3)/PPM);

    body = world.createBody(bodyDef);



    fixtureDef = new FixtureDef();
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 1.0f;
    fixtureDef.restitution=0.3f;


    CircleShape circleShape = new CircleShape();
    circleShape.setRadius((Gdx.graphics.getWidth()/16)/PPM);
    fixtureDef.shape = circleShape;

    fixture = body.createFixture(fixtureDef);

    circleRect = new Circle(body.getPosition().x,body.getPosition().y,(Gdx.graphics.getWidth()/16));

    Sprite.split(ImageLoader.playButtonRegion.getTexture(),20,20);



    sprite = new Sprite(ImageLoader.paddle);
    sprite.setSize((Gdx.graphics.getWidth()/8),(Gdx.graphics.getWidth()/8));
    sprite.setPosition((Gdx.graphics.getWidth()/2)-30f,(Gdx.graphics.getHeight()/3)-30f);


    touchPos = new Vector2();

}

@Override
public void render(SpriteBatch sb) {


    sprite.draw(sb);
    sprite.setPosition(body.getPosition().x*PPM-30f,body.getPosition().y*PPM-30f);
}

@Override
public void update(float delta) {

    touchPos.x=Gdx.input.getX()/PPM;
    touchPos.y=Gdx.input.getY()/PPM;

    System.out.println(touchPos);


    if (TopPaddle) {

        body.setLinearVelocity(power*(touchPos.x-body.getPosition().x),power*(touchPos.y-body.getPosition().y));
        body.setAngularVelocity(0.0f);


        if(Gdx.input.getY()>Gdx.graphics.getHeight()/2){

            body.setLinearVelocity(0f,0f);
        }
        //System.out.println(Gdx.input.getX()+" "+Gdx.input.getY());
    }

}

我希望,我说清楚了。

【问题讨论】:

  • 要正确支持多点触控,您需要实现一个 InputListener 并跟踪指针索引以区分向下的手指。

标签: java libgdx box2d 2d-games


【解决方案1】:

我想我看到了你的问题,你依赖于一次只接受一个输入的事实。因此,如果两个玩家同时提供输入,您将忽略其中一个。

有多种方法可以解决您的多点触控输入问题,但我将解释一个简单的技术 - 修改自 this answer

要让两个玩家都接受输入,您需要两个变量 - 我将它们称为 topTouchPosbottomTouchPos。其中每一个都将是一个Vector2,就像您当前的touchPos 一样,它们将按如下方式计算(在您的update 方法中):

//Initialise both vectors to vectors that can't be touched (negative)
Vector2 topTouchPos = new Vector2(-1,-1), bottomTouchPos = new Vector2(-1,-1);

//Two people can have up to 20 fingers (most touchscreen devices will have a lower limit anyway)
for (int i = 0; i < 20; i++) {
    //Check if this finger ID is touched
    if (Gdx.input.isTouched(i)) {
        //Classify it as either the top or bottom player
        bool bottom = Gdx.input.getY(i) > Gdx.graphics.getHeight()/2;
        if (bottom) bottomTouchPos.set(Gdx.input.getX(i), Gdx.input.getY(i));
        else topTouchPos.set(Gdx.input.getX(i), Gdx.input.getY(i));
    }
}

当您进入主游戏代码时,您必须检查两个玩家。如果topTouchPosbottomTouchPos 不是负数,则将它们的触摸值用于各自的玩家。

希望这会有所帮助(我没有测试过任何代码,因此请注意任何拼写错误)。

【讨论】:

  • 我认为我的游戏知道第二个输入,但由于某种原因它无法更改另一个桨的 x 和 y 坐标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多