Libgdx:暂停不触发justtouch
问题描述:
我最近问了this question,它完美的工作,我遇到的唯一问题是整个游戏是基于触摸事件,当用户触摸屏幕,一个对象被创建。Libgdx:暂停不触发justtouch
现在发生的事情是,当用户触摸暂停按钮(贴图打包器)时,会创建一个对象并暂停游戏。如果暂停被触摸,我想防止创建对象。我曾经是能够做这样的事情:
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)
这是在屏幕的左上角
答
如果pauseX
和pauseY
留位置和矩形/按钮的右侧位置分别然后
你需要按钮的宽度和高度/矩形区域,就像buttonWidth
,buttonHeight
。
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;
}
}
不知道什么纹理包装机具有与此有关呢? – Moira
@ 1blustone所以这是我以错误的方式实施上述方法?或者有其他的方法 – Lynob
我的意思是我不太清楚你的标题是什么意思 - 所以你想要做的就是当你按下按钮时停止/恢复所有的东西(包括对象创建)?看看[Ashley](https://github.com/libgdx/ashley)并为所有内容创建系统,并在游戏成功时停止调用'engine.update'。当然,保持你的暂停按钮在'引擎'外面,这样你就可以恢复它:) – Moira