仅在比演员所在位置高1个单元格或低1个单元格的位置选择演员



有一个尺寸为800x600的游戏场。该字段是一张包含4x4单元格的图片。

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Texture;
public class TriAngle extends Figure implements InputProcessor {
private float w = 800;
private float h = 600;
public TriAngle() {
setTextureRegion(new Texture(Gdx.files.internal("TriAngle.png")));
setBounds(200, 400, w / 4, h / 4);
Gdx.input.setInputProcessor(this);
}
@Override
public boolean keyDown(int i) {
return false;
}
@Override
public boolean keyUp(int i) {
return false;
}
@Override
public boolean keyTyped(char c) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
if((screenX > getX() && screenX < getX() + getWidth())&&(screenY > getY() && screenY < getY() + getHeight())){
this.setPosition(screenX, screenY, 0);
}
return true;
}
@Override
public boolean mouseMoved(int x, int y) {
/* if (x >= 0) {
this.setPosition(x,y);
}*/
return true;
}
@Override
public boolean scrolled(int i) {
return false;
}
}

Кругомпечена这是一个很好的例子。

在此处输入图像描述

在此处输入图像描述

如何严格在三角形下选择工作单元格?为什么工作单元格会根据三角形的位置而变化(三角形上方有1个单元格,三角形下方有1个(?

我看到您在选择和位置方面遇到了问题,请尝试使用OrthographicCamera:

public class TriAngle extends Figure implements InputProcessor {
private OrthographicCamera camera;
private Vector3 unprojected;
private float w = 800;
private float h = 600;
public TriAngle() {
camera = new OrthographicCamera();
camera.setToOrtho(false, w, h);
unprojected = new Vector3();
setTextureRegion(new Texture(Gdx.files.internal("TriAngle.png")));
setBounds(200, 400, w / 4, h / 4);
Gdx.input.setInputProcessor(this);
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
unprojected.set(camera.unproject(new Vector3(screenX, screenY, 0f)));
if(unprojected.x > getX() && unprojected.x < getX() + getWidth() && unprojected.y > getY() && unprojected.y < getY() + getHeight()){
this.setPosition(unprojected.x, unprojected.y, 0);
}
return true;
}
}

最新更新