触地和触摸事件如何运作-LibGdx

时间:2017-02-13 10:06:03

标签: events libgdx touch

我要在同一个对象上执行两个事件。一个是触摸,另一个是释放/删除触摸。 我希望我可以使用Inputprocessor进行touchup和touchdown事件。

我尝试过的东西。

if (MyInputProcessor.isTouchDown) {

        stickSprite.setSize(stickSprite.getWidth(), stickSprite.getHeight()    + 500.0f);
        } 

       if (MyInputProcessor.isTouchUp)
        {
             if(anglevalue>=0)
                {
                    anglevalue++;
                stickSprite.setRotation(70f);

                }
        }

此处如果没有执行补足条款。

这些事件的确如何运作?

如何有效地使用此活动来满足我的要求?

1 个答案:

答案 0 :(得分:1)

看起来,你想要在用户触摸屏幕时增加图像的高度,然后增加直到他将手指从屏幕上移开,然后将图像旋转90度。

我试过这个,希望它可能会给你一些参考

public class TestGame extends Game implements InputProcessor{

Texture pixelTex;

SpriteBatch spriteBatch;
Sprite sprite;
float w,h;

TouchStatus touchStatus=TouchStatus.NONE;

enum TouchStatus {
    TOUCH_DOWN,TOUCH_UP,NONE
}

@Override
public void create() {

    w=Gdx.graphics.getWidth();
    h=Gdx.graphics.getHeight();
    Gdx.input.setInputProcessor(this);

    spriteBatch=new SpriteBatch();
    pixelTex= getPixmapTexture(Color.WHITE);

    sprite=new Sprite(pixelTex);
    sprite.setColor(Color.YELLOW);
    sprite.setSize(10,10);
    sprite.setPosition(200,200);
}

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

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

    spriteBatch.begin();
    sprite.draw(spriteBatch);
    spriteBatch.end();

    if(touchStatus==TouchStatus.TOUCH_DOWN){

        if(sprite.getY()+sprite.getHeight()<h){
            float currentHeight=sprite.getHeight();
            currentHeight++;
            sprite.setSize(sprite.getWidth(),currentHeight);
        }
    }
    if(touchStatus==TouchStatus.TOUCH_UP){
        float currentRotation=sprite.getRotation();
        currentRotation--;
        sprite.setRotation(currentRotation);
        if(currentRotation<=-90)
            touchStatus=TouchStatus.NONE;
    }
}

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

@Override
public void dispose() {
    super.dispose();
    pixelTex.dispose();
    spriteBatch.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 button) {

    if(touchStatus==TouchStatus.NONE)
    touchStatus=TouchStatus.TOUCH_DOWN;
    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    if(touchStatus==TouchStatus.TOUCH_DOWN)
    touchStatus=TouchStatus.TOUCH_UP;
    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;
  }

public static Texture getPixmapTexture(Color color){
        return new Texture(getPixmapRectangle(1, 1, color));
}

public static Pixmap getPixmapRectangle(int width, int height, Color color){
        Pixmap pixmap=new Pixmap(width, height, Pixmap.Format.RGBA8888);
        pixmap.setColor(color);
        pixmap.fillRectangle(0,0, pixmap.getWidth(), pixmap.getHeight());

        return pixmap;
   }
 }