gpt4 book ai didi

java - Libgdx:暂停而不触发 justtouch

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

我最近问过this question ,它工作得很好,我遇到的唯一问题是整个游戏都是基于触摸事件的,当用户触摸屏幕时,就会创建一个对象。

现在发生的情况是,当用户触摸暂停按钮(纹理打包器)时,会创建一个对象并且游戏暂停。我想防止在触摸暂停时创建对象。我曾经能够做这样的事情:

private Vector3 touchPos; 
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);

if (Gdx.input.justTouched()) {
if (touchPos.x > pauseX && touchPos.x < pauseX + pauseX) {
if (touchPos.y > pauseY && touchPos.y < pauseX + pauseY) {
setGamePause(!getGamePause());
}}}

但似乎无法与纹理打包程序一起使用,也许我的实现是错误的,不确定,还有其他方法吗?

private float pauseY = Gdx.graphics.getHeight() - 115;
private float pauseX = Gdx.graphics.getWidth() / 6;
button.setSize(150, 150)

它位于屏幕的左上角

最佳答案

如果pauseXpauseY分别是矩形/按钮的左侧位置和右侧位置,则

您需要按钮/矩形区域的宽度和高度,例如假设buttonWidthbuttonHeight

if(Gdx.input.justTouched()) {
if (touchPos.x > pauseX && touchPos.x < pauseX + button.getWidth()) {
if (touchPos.y > pauseY && touchPos.y < pauseY + button.getHeight()) {
setGamePause(!getGamePause());
}
}
}

请检查测试:

public class GdxTest extends Game  implements  InputProcessor{

private Stage stage;
Vector3 vector3;
TextButton button;
float pauseX,pauseY;

@Override
public void create() {

vector3=new Vector3();
ExtendViewport extendViewport=new ExtendViewport(700,1200,new OrthographicCamera());
stage=new Stage(extendViewport);

Skin skin=new Skin(Gdx.files.internal("skin/uiskin.json"));
skin.get("font-label", BitmapFont.class).getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

pauseX=300;
pauseY=500;
button=new TextButton("Pause",skin);
button.setPosition(pauseX,pauseY);

stage.addActor(button);
Gdx.input.setInputProcessor(this);
}

@Override
public void render() {
super.render();

Gdx.gl.glClearColor(0,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

stage.draw();
stage.act();

////Touch Detection without processor

vector3.set(Gdx.input.getX(),Gdx.input.getY(),0);
stage.getCamera().unproject(vector3);

if(Gdx.input.justTouched()){
if(vector3.x>pauseX && vector3.x<pauseX+button.getWidth() && vector3.y>pauseY && vector3.y<pauseY+button.getHeight())
System.out.println("TOuched");
}

///////
}

@Override
public void resize(int width, int height) {
super.resize(width,height);
stage.getViewport().update(width,height);
}

@Override
public void dispose() {
stage.dispose();
}

@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 button1) {

vector3.set(screenX,screenY,0);
stage.getCamera().unproject(vector3);

if(vector3.x>pauseX && vector3.x<pauseX+button.getWidth() && vector3.y>pauseY && vector3.y<pauseY+button.getHeight())
System.out.println("TOuched");


return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}

@Override
public boolean scrolled(int amount) {
return false;
}
}

关于java - Libgdx:暂停而不触发 justtouch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43435585/

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