所以我是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;
}
}
Spot类:
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的Gamescreen:
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的唯一引用。这也适用于其余代码。)
答案 0 :(得分:1)
为了简化这一点,您只需要检查圆与触摸位置的距离是否小于检测命中的圆的半径。简单的矢量数学可以解决问题,我们水平和垂直地计算距离,使用毕达哥拉斯(A²+B²=C²)我们可以计算最终距离。
distance = √(circleX - (touchX))² + ((circleY) - touchY)²
if (Math.abs(distance) < circleRadius)
{
//Touch within circle...
}
Libgdx有一些vector functions来计算距离。
if (touchVector.dst(circleOrigin) < circleRadius)
{
//Touch within circle...
}
当我谈论touchVector时,你需要使用Vector2并将你的坐标放入。当处理位置,方向,速度等时,你应该使用Vector2类或Vector3用于3D环境。
Vector2 touchVector = new Vector2(x,y);
float distance = touchVector.dst(new Vector2(circleOriginX, circleOriginY));