gpt4 book ai didi

java - Libgdx 输入在 Android 上不起作用

转载 作者:行者123 更新时间:2023-12-02 12:34:40 24 4
gpt4 key购买 nike

我是 Libgdx 的新手,我正在尝试制作一张可以使用相机探索的 map 。我在自己的 Map 类中实现了 GestureListener

public class Map extends Stage implements GestureListener {

public String mapName;
private Sprite background;
public LocationPoint points[];
private OrthographicCamera camera;
private Batch batch;
public Music anbientSound;
public int numOfPoints;
public int locationsX[];
public int locationsY[];


public Map(Sprite background) {
this.background = background;
}


public Sprite getBackground() {
return background;
}


public void activate() {

InputMultiplexer inputChain = new InputMultiplexer();

if(points==null) {
points = new LocationPoint[numOfPoints];
for(int i = 0; i < numOfPoints; i++) {
points[i] = new LocationPoint(locationsX[i], locationsY[i]);
addActor(points[i]);
}
}

batch = GameUtils.batch;
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0);
camera.update();

Music music = GameUtils.addMusic(anbientSound);
music.setLooping(true);
music.play();

inputChain.addProcessor(this);
inputChain.addProcessor(new GestureDetector(this));

Gdx.input.setInputProcessor(inputChain);
}


public void draw() {

Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(Gdx.gl20.GL_COLOR_BUFFER_BIT);

Batch batch = this.batch;
batch.setProjectionMatrix(camera.combined);

batch.begin();
background.draw(batch);
batch.end();

batch.begin();

for(int i = 0; i < numOfPoints; i++) {
points[i].draw(batch, 1);
addActor(points[i]);
}

batch.end();
}


public void dispose() {
GameUtils.stopMusic();
background.getTexture().dispose();
anbientSound.dispose();
}


@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
camera.translate(-deltaX, deltaY);

float initialX = camera.viewportWidth / 2;
float initialY = camera.viewportHeight / 2;

GameUtils.limitBound(camera, initialX, initialY, background.getWidth(), background.getHeight());
camera.update();

return true;
}


@Override
public boolean touchDown(float x, float y, int pointer, int button) {
return false;
}


@Override
public boolean tap(float x, float y, int count, int button) {
return false;
}


@Override
public boolean longPress(float x, float y) {
return false;
}


@Override
public boolean fling(float velocityX, float velocityY, int button) {
return false;
}


@Override
public boolean panStop(float x, float y, int pointer, int button) {
return false;
}


@Override
public boolean zoom(float initialDistance, float distance) {
return false;
}


@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
Vector2 pointer1, Vector2 pointer2) {
return false;
}


@Override
public void pinchStop() {}

}

方法activate()用于激活Map类的资源。 Map 类还有一个名为 LocationPointsImageButtons

public class LocationPoint extends ImageButton {

private Monster monster;
private Trap trap;
public boolean occuped;
public boolean isTrap;
public int f = 20;
public int k = 20;


public LocationPoint(float x, float y) {
super(GameUtils.getLocationDrawable());
this.setSize(46, 46);
setPosition(x, y);
addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
setPosition(f, k);
f += 10;
k += 10;
return super.touchDown(event, x, y, pointer, button);
}
});
}


public void occup(Monster monster) {
this.monster = monster;
occuped = true;

if(isTrap)
captured();

}

@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
}


public void empty() {
monster = null;
occuped = false;
}

public void captured() {
monster.capture();
}


public Monster getMonster() {
return monster;
}

}

在 LocationPoint 类中,我添加一个 InputListener 来在触摸该 LocationPoint 时制作一个东西。当在 Android 上玩游戏时,会发生 LocationPoints 事件和 Map pan 事件。但是在移动相机后,当我触摸 LocationPoint 时,它不会触发该点事件。

但是当我将相机返回到初始位置时,LocationPoints 事件工作正常!你能帮助我吗? (抱歉我蹩脚的英语......)

最佳答案

  • Actor 已经是一个子进程,因此请从 draw() 方法中删除多余的调用。

    for(int i = 0; i < numOfPoints; i++) {
    points[i].draw(batch, 1);
    addActor(points[i]); // <-- Not Should be in draw() call
    }
  • Stage 拥有由默认构造函数创建的自己的 SpriteBatch,请使用该批处理或在 Stage 构造函数中传递自己的批处理。

    如果你想自己画一些东西,请使用StagegetBatch()方法。

  • Map类中创建相机也是多余的,使用getViewport().getCamera()返回舞台相机,您可以类型转换为OrthographicCamera

  • dispose() 方法中调用 super.dispose();

<小时/>

所有 Actor 都是你自己画的吗?如果您没有做超出范围的事情,则无需重写 Stagedraw() 方法。

关于java - Libgdx 输入在 Android 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45177344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com