2015-05-10 114 views
0

我正在尋找觸摸檢測。下面的代碼顯示了我在應用程序中設置了一個圓圈。我想在這個圓圈上檢測觸摸,而不是在整個紋理周圍或整個紋理上。奇怪的是,觸摸檢測不到,在無處我可以檢測到它 Circle類:Libgdx Actor未檢測到觸摸輸入

public class Circle_Obj extends Actor{ 
    private Vector2 position; 
    private float radius; 

    private com.badlogic.gdx.math.Circle circle; 
    private Texture texture; 

    public Circle_Obj(float x, float y, float radius) { 

     position = new Vector2(x,y); 
     this.radius = radius; 

     circle = new com.badlogic.gdx.math.Circle(x,y,radius); 
     texture = new Texture(Gdx.files.internal("texture.png")); 

     addListener(new InputListener(){ 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       Gdx.app.log("TOUCHED", " TOUCHED "); 
       return true; 
      } 
     }); 

    } 

    @Override 
    public void draw(Batch batch, float parentAlpha) { 
     batch.draw(texture,0, 0); 
} 
} 

Screen類:

public class GameScreen implements Screen { 
    private Stage stage; 
    private Circle_Obj circle_obj; 

    public GameScreen() { 
     circle_obj = new Circle_Obj(Static_values.Width/2, Static_values.Height/2, Static_values.Width/100 * 10); 

     stage = new Stage(new FitViewport(Static_values.Width/3, Static_values.Height/3)); 
     stage.addActor(circle_obj); 

     Gdx.input.setInputProcessor(stage); 

    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     stage.draw();  
     } 
    @Override 
    public void dispose() { 
     stage.dispose(); 
     } 
/** other methods **/ 
} 
+0

沒有嘗試只是「階段=新階段(); – Fish

回答

0

您可以使用libgdx的觸摸檢測的圈子類。只有感動的圈子會受到影響。

public boolean is_touched() { 
    if (Gdx.input.justTouched()) { 
     float xx = Gdx.input.getX(); 
     float yy = Gdx.input.getY(); 
     float x = position.x; 
     float y = position.y; 
     return (xx - x) * (xx - x) + (yy - y) * (yy - y) < radius * radius; 
    } 
} 

如果您使用了很多圈子,所以以觸摸位置作爲參數的性能會更好。

在圈內類

public boolean is_touched(float xx,float yy) { 
      float x = position.x; 
      float y = position.y; 
      return (xx - x) * (xx - x) + (yy - y) * (yy - y) < radius * radius; 
     } 
    } 

,並在另一個類

if (Gdx.input.justTouched()) { 
      float xx = Gdx.input.getX(); 
      float yy = Gdx.input.getY(); 
      if (circle0.is_touched(xx, yy)) { 
       // do something about circle0 
      } 
      if (circle1.is_touched(xx, yy)) { 
       // do something about circle1 
      } 
      if (circle2.is_touched(xx, yy)) { 
       // do something about circle2 
      } 
     } 

您也可以忽略觸摸某個圈子,當兩個圓圈重疊和用戶觸動的重疊區域

if (Gdx.input.justTouched()) { 
       float xx = Gdx.input.getX(); 
       float yy = Gdx.input.getY(); 
       if (circle0.is_touched(xx, yy)) { 
        // do something about circle0 
       } 
       else if (circle1.is_touched(xx, yy)) { 
        // do something about circle1 
       } 
       else if (circle2.is_touched(xx, yy)) { 
        // do something about circle2 
       } 
      } 
+0

它不工作,編輯‘>半徑*半徑’是錯誤的,正確的是‘<半徑*半徑’ – user4789408

0

爲了檢測在一個階段中加入一個演員的觸感,演員的方法hit被稱爲(hit detection in the documentation

public Actor hit (float x, float y, boolean touchable) { 
    if (touchable && getTouchable() != Touchable.enabled) return null; 
    return x >= 0 && x < width && y >= 0 && y < height ? this : null; 
} 

您還沒有一個大小設置爲您Circle_Obj演員,所以hit將始終返回null。

現在,你的演員是一個圓圈,所以你可能想要覆蓋hit,以便它檢查給定的座標是否在圓圈中,而不是檢查座標是否在大小爲演員。

喜歡的東西:

@Override 
public Actor hit (float x, float y, boolean touchable) { 
    if (touchable && getTouchable() != Touchable.enabled) return null; 
    return (x - position.x)*(x- position.x) + (y - position.y)*(y - position.y) < radius*radius ? this : null; 
} 
0

如果從演員繼承時,需要設置的界限,否則將無法進行點擊/觸摸能! 只需設置邊界以匹配您的演員包含的紋理。

//add this Set bounds the x, y, width, and height 
    circle_obj.setBounds(0, 0,texture.getWidth(), texture.getHeight());