如何检查圆形是否被触摸以对形状应用动作,例如改变形状的颜色或更新分数



所以我是android游戏开发的新手,我有4个黄色的CircleMapObjects,我正在使用InputProcessor来管理触摸/点击事件。现在,在touchDown()方法中,我想要的是当在游戏中触摸/点击圆圈(圆圈内的任何地方)时,它应该变为绿色。我正在使用libgdx框架,并且是该框架的新手,我遇到了Actor,但我发现它通过查看API来绘制矩形。

InputHandler类:

public class InputHandler implements InputProcessor {
private Spot spot;

public InputHandler(Spot spot){
    this.spot = spot;
}
@Override
public boolean keyDown(int keycode) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public boolean keyUp(int keycode) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public boolean keyTyped(char character) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    float distanceX = (float) Math.sqrt(Math.pow((spot.getSpots()[0].getCircle().x - (screenX)), 2));
    float distanceY = (float) Math.sqrt(Math.pow((spot.getSpots()[0].getCircle().y - (screenY)), 2));
    //Below is for testing purposes not to do with touching a circle but touching a particular 
    //region on the screen
    //float distance = distanceX + distanceY;
//  System.out.println(distanceX);
//  System.out.println(distanceY);
//  System.out.println(spot.getSpots()[0].isVisible() + "" + "Color: " + spot.getSpots()[0].getColor());
    System.out.println("CLICKED: " + " x: " + screenX + " y: " + screenY);
            if (screenX > 45 && screenY < 431)
            {
                spot.getSpots()[0].setColor(Color.RED);
                System.out.println("This works!");
            }

    return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
    // TODO Auto-generated method stub
    return false;
}
@Override
public boolean scrolled(int amount) {
    // TODO Auto-generated method stub
    return false;
}

}

现货类:

public class Spot {
private float x;
private float y;
private float radius;
//Use CircleMapObject instead?
private CircleMapObject spot_1, spot_2, spot_3, spot_4;
private CircleMapObject spotList[];
private float elapsedTime;
private int index;

public Spot(float x, float y, float radius){
    this.x = x;
    this.y = y;
    this.radius = radius;
    //each spot initialised
    spot_1 = new CircleMapObject(x,y,radius);
    spot_2 = new CircleMapObject(x, y-228,radius);
    spot_3 = new CircleMapObject(x+440,y,radius);
    spot_4 = new CircleMapObject(x+440,y-228,radius);
    //spot list initialised
    spotList = new CircleMapObject[4];
    //adding the spots to the spotlist
    spotList[0] = spot_1;
    spotList[1] = spot_2;
    spotList[2] = spot_3;
    spotList[3] = spot_4;
    elapsedTime = 0.0f;
    index = 0;

}
public void update(float delta){

} 

}

设置inputHandler的游戏屏幕:

public class GameScreen implements Screen {

private GameWorld world;
private GameRenderer render;
private Spot spot;

public GameScreen(){
    float screenWidth = Gdx.graphics.getWidth();
    float screenHeight = Gdx.graphics.getHeight();
    System.out.println("Screen width: " + screenWidth + ", " + "Screen height: " + screenHeight);
    float gameWidth = 136;
    float gameHeight = screenHeight / (screenWidth / gameWidth);
    world = new GameWorld();
    render = new GameRenderer(world);
    Gdx.input.setInputProcessor(new InputHandler(world.getSpot()));
}

(GameScreen类中有更多的代码,但这是该类中使用的InputHandler的唯一引用。这也适用于代码的其余部分。)

为了简化这一点,您只需要检查圆圈与触摸位置的距离是否小于圆圈的半径即可检测到命中。简单的矢量数学就可以了,我们计算水平和垂直的距离,用毕达哥拉斯(A²+B²=C²)我们可以计算出最终的距离。

distance = √(circleX - (touchX))² + ((circleY) - touchY)²
if (Math.abs(distance) < circleRadius)
{
    //Touch within circle...
}

Libgdx有一些用于计算距离的向量函数。

if (touchVector.dst(circleOrigin) < circleRadius)
{
    //Touch within circle...
}

当我谈论touchVector时,你需要使用Vector2并输入坐标。当处理位置、方向、速度等时,你应该在3D环境中使用Vector2类或Vector3。

Vector2 touchVector = new Vector2(x,y);
float distance = touchVector.dst(new Vector2(circleOriginX, circleOriginY));