2015-05-10 62 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());