Java Libgdx矩形检测触摸
我想检测用户是否点击了骆驼并增加了金额,但这不起作用(如果我点击(移动的)骆驼,什么也不会发生)。我在互联网上找不到解决这个问题的办法 我在render()中添加了以下内容: 编辑:Java Libgdx矩形检测触摸,java,onclick,libgdx,touch,rectangles,Java,Onclick,Libgdx,Touch,Rectangles,我想检测用户是否点击了骆驼并增加了金额,但这不起作用(如果我点击(移动的)骆驼,什么也不会发生)。我在互联网上找不到解决这个问题的办法 我在render()中添加了以下内容: 编辑: @Override public void create() { spawnLama(); textureAtlas = new TextureAtlas(Gdx.files.internal("data/lamination.pack")); animation = new Animation(1/15f
@Override
public void create() {
spawnLama();
textureAtlas = new TextureAtlas(Gdx.files.internal("data/lamination.pack"));
animation = new Animation(1/15f, textureAtlas.getRegions());
camera = new OrthographicCamera(1280, 720);
Gdx.input.getX
和Gdx.input.getY
给出屏幕坐标,这些坐标需要转换回世界坐标。如果您使用的是相机,则可以使用camera.unproject(touchVector)
如果您使用的是没有指定相机的舞台,则可以通过stage.getCamera()
获取相机
如果你没有,那么你必须自己把它转换回来,我相信你必须先翻转y坐标,然后将它们添加到你在游戏世界中看到的左下角点。但通过安装摄像头,你的生活变得更轻松了
if(Gdx.input.justTouched()){
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
for (Lama lama : lamas) {
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}
除此之外,您在
contains
方法中传递了两次Gdx.input.getX()
。谢谢!我使用正交摄影机,但这仍然不起作用。@AlGrande您是否使用该摄影机的投影绘制矩形?我添加了新的相关代码。我是这样画的:batch.draw(currentFrame,lama.x,lama.y,currentFrame.getRegionWidth(),currentFrame.getRegionHeight());lama.move()做什么?您确定更新的lama.rect
正确吗?尝试记录lama rect的值,并自己比较。我用touch
和camera.unproject()
所做的更正将为您提供正确的世界位置。因此,检查批处理.draw(currentFrame、lama.x、lama.y、currentFrame.getRegionWidth()、currentFrame.getRegionHeight())是否正确
与该if
语句中的矩形相同。或者将绘图更改为绘图(currentFrame、rect.getX、rect.getY、rect.getWidth、rect.getHeight)代码>或其他方式,并根据当前的x、y、宽度和高度生成一个用于碰撞的矩形。
private void spawnLama() {
Lama lama = new Lama();
lama.x = MathUtils.random(-1000, -200);
lama.y = MathUtils.random(-350, 100);
lama.speedx = MathUtils.random(-5, 5);
if(lama.speedx >= -1 && lama.speedx <=1){
lama.speedx = 2;
}
lama.speedy = MathUtils.random(-5, 5);
if(lama.speedy >= -1 && lama.speedy <=1){
lama.speedy = 2;
}
Rectangle livinglama = new Rectangle();
livinglama.x = lama.x;
livinglama.y = lama.y;
livinglama.width = 64;
livinglama.height = 64;
lama.lamarect = livinglama;
lamas.add(lama);
lastLamaTime = TimeUtils.nanoTime();
}
@Override
public void render() {
Gdx.gl.glClearColor(110 / 255F, 211 / 255F, 43 / 255F, 1 / 255F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
elapsedTime += Gdx.graphics.getDeltaTime();
if(spawnlama == true){
spawnLama();
spawnlama = false;
}
for (Lama lama : lamas) {
if(lama.x <= -1000){
lama.speedx = -lama.speedx;
}
else if(lama.x >= -200){
lama.speedx = -lama.speedx;
}
if(lama.y <= -350){
lama.speedy = -lama.speedy;
} else if(lama.y >=100){
lama.speedy = -lama.speedy;
}
if(lama.x == -500){
if(lama.speedx < 0){
lama.speedx --;
}
if(lama.speedx >= 0){
lama.speedx++;
}
}
lama.move();
}
if(Gdx.input.justTouched()){
for (Lama lama : lamas) {
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}
batch.begin();
for (Lama lama : lamas) {
TextureRegion currentFrame;
currentFrame = animation.getKeyFrame(elapsedTime, true);
if(!currentFrame.isFlipX()) {
if (lama.speedx >= 0) {
currentFrame.flip(true, false);
}
}else{
if (lama.speedx < 0) {
currentFrame.flip(true, false);
}
}
batch.draw(currentFrame, lama.x, lama.y, currentFrame
.getRegionWidth(), currentFrame.getRegionHeight());
}
elapsedTime += Gdx.graphics.getDeltaTime();
public class Lama {
public int x, y, speedx, speedy;
public Rectangle lamarect;
// float delay = 1; // seconds
void move() {
x += speedx;
y += speedy;
lamarect.x = x;
lamarect.y = y;
}
}
if(Gdx.input.justTouched()){
Vector3 touch = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touch);
for (Lama lama : lamas) {
if(lama.lamarect.contains(touch.x, touch.y)){
money+=100;
}else{
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.app.debug("POSITION", "X touched: " + touch.x + " Y touched: " + touch.y);
}
}
}