2017-02-13 51 views
1

我有兩個事件。 我希望我可以通過使用Inputprocessor來實現touchup和touchdown事件。如何在同一object.One在觸摸來執行和其他有關釋放/移除觸摸觸和潤色事件工作LibGdx

我試過的東西。

if (MyInputProcessor.isTouchDown) { 

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

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

       } 
     } 

在這裏,如果觸摸子句沒有執行。

如何這樣的活動真的有效麼?

我如何使用這個事件,有效地履行我的要求?

+0

顯示您MyInputProcessor。另外,什麼'angleValue'爲?你是否意識到你在每一幀上增加了一個? – Tenfour04

回答

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; 
    } 
} 
+0

嘿!謝謝!我運行這個代碼,它看起來像我想要做的天。我嘗試了不同的方式來實現這種類型的邏輯,並得到這個非常困難的時間。我希望使用此代碼,我可以實現的邏輯,我想要!再次感謝! – Niranjana