2015-09-26 63 views
0

在此圖像中enter image description here如何使用touchDraggedenter image description here僅限於x軸?使用touchDragged更改音量(libGDX)

這是我的代碼,以使這個動作:

iBtnDrag = new Image(tBtnDrag); 
iBtnDrag.addListener(new ClickListener() { 

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

     @Override 
     public void touchDragged(InputEvent event, float x, float y, int pointer) { 
      iBtnDrag.setPosition(x, 448, Align.center); 
     } 
}); 

回答

1

你應該創建DragListener

... 

    float startX; 

    DragListener listener = new DragListener() 
    { 
     public void dragStart(InputEvent event, float x, float y, int pointer) 
     { 
      startX = x; 
     } 

     public void drag(InputEvent event, float x, float y, int pointer) 
     { 
      //x, y are delta from starting point so 
      iBtnDrag.setPosition(startX + x, 448, Align.center); 
     } 

     public void dragStop(InputEvent event, float x, float y, int pointer) 
     { 
      //when stopping drag 
     } 
    }; 

buuuut一個更好的方式是隻創建Slider。爲了達到這個目的最好是使用skin mechanism

你的皮膚文件應該是這樣的,例如:

com.badlogic.gdx.scenes.scene2d.ui.Slider$SliderStyle: 
    { 
     default-horizontal: { background: greenBackground, disabledBackground: sliderDisabled, knob: woodenKnob, disabledKnob: transparent }, 
    }, 

,然後你創建滑塊像

music= new Slider(minValue, maxValue, stepSize, isVertical, skin); 
    music.setWidth(musicSiderWidth); 

滑塊允許用戶獲得其價值致電

music.getValue(); 

這其實很方便

+0

雅,就是這樣。滑塊是完美的解決方案。 Thanx @ m.antkowicz – Crowni