libGDX屏幕触摸位置缩放到摄像机位置

时间:2015-03-03 08:39:39

标签: android

屏幕触摸位置不适合相机位置。

enter image description here

粉红点是鼠标点击的地方(不会将收到的touchX除以缩放,换句话说就是catXtouch=Gdx.input.getX() 当我使用缩放时,问题也符合但是比例较小 “甜蜜点”是指比例为367,但也不准确。

以下是与此问题相关的相关性代码:

    int scaling

    //update is called on create
    public void update(){
    cam = new OrthographicCamera();
    cam.setToOrtho(false,480,800);
    ...
    ...
    scaling = (Gdx.graphics.getWidth()/480);

    //called on render
     if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        cam.unproject(touchPos);
    }

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)    {
    catXtouch=Gdx.input.getX()/scaling;
    return true;

完整代码:

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.Viewport;

public class StageOneClass implements InputProcessor {
private  SpriteBatch batch;
private  Texture building1;
private  Texture building2;
private  Texture side1;
private  Texture side2;
private Texture side3;
private Texture side4;
private Texture cat;
private Texture obs1;
private OrthographicCamera cam;
private long StartCountTime;
private long CorrectCountTime;
private float SideY;
private float BuildY;
private int obsX;
private float obsY;

private TextureAtlas catatlas;
private TextureAtlas birdatlas;
private Animation catanime;
private Animation birdanime;
private float timeIsZero =0;

private int catXtouch;
private int scaling;
private int scrennwidth;

private Rectangle CatRect;
private Rectangle ObsRect;

private Sound catsound;
private ShapeRenderer sr;

private BitmapFont font;
private int score;
private int catY;
private int Direction;
private float BooleanResaultX;
private Viewport Viewport;

    public StageOneClass(){

}

public void update(){
    cam = new OrthographicCamera();
    cam.setToOrtho(false,480,800);
    //Viewport = new FillViewport(480,800,cam);

    catY=20;

    sr = new ShapeRenderer();

    batch = new SpriteBatch();
    building1 = new Texture(Gdx.files.internal("building1.png"));
    building2 = new Texture(Gdx.files.internal("building2.png"));
    side1 = new Texture(Gdx.files.internal("side1.png"));
    side2 = new Texture(Gdx.files.internal("side2.png"));
    side3 = new Texture(Gdx.files.internal("side1.png"));
    side4 = new Texture(Gdx.files.internal("side2.png"));

    cat = new Texture(Gdx.files.internal("cat1.png"));
    StartCountTime = System.currentTimeMillis();

    obs1 = new Texture(Gdx.files.internal("obsone.png"));
    obsY=800;

    catatlas =new TextureAtlas(Gdx.files.internal("catatlas.pack"));
    catanime = new Animation(1/10f,catatlas.getRegions());
    birdatlas= new TextureAtlas(Gdx.files.internal("birdatlas.pack"));
    birdanime= new Animation(1/10f,birdatlas.getRegions());
    Gdx.input.setInputProcessor(this);

    scaling = (Gdx.graphics.getWidth()/480);

    catsound = Gdx.audio.newSound(Gdx.files.internal("catsound.mp3"));

    font = new BitmapFont();
    font.setColor(Color.BLACK);
    font.scale(1);
}

public void MovingBackGround() {
    CorrectCountTime = System.currentTimeMillis();

    if ((CorrectCountTime - StartCountTime) > 1000) {
        BuildY -= 4;
        SideY -= 1;
        obsY -=3;
        if (Direction==0){

        obsX+=2;
        }
        if(Direction==1){
            obsX-=2;
        }

    }
    if (obsX<50){
        Direction=0;
    }
    if(obsX>430-150) {
        Direction =1;
    }
    if (BuildY < -800) {
        BuildY = 0;
    }
    if (SideY < -800) {
        SideY = 0;
    }if(obsY<= 0){

        obsY=800;
    }
}

public void render() {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if (obsY == 800) {
        obsX = MathUtils.random(40, 430 - 150);
    }
    ObsRect = new Rectangle(obsX, obsY, 100, 100);
    CatRect = new Rectangle(catXtouch+40, 20+40, 70, 70);

    if (catXtouch < 25) {
        catXtouch = 25;
    }
    if (catXtouch > 430-150) {
        catXtouch = 430-150;
    }

    MovingBackGround();
    timeIsZero += Gdx.graphics.getDeltaTime();
    batch.setProjectionMatrix(cam.combined);

    batch.begin();
    batch.draw(building1, 50, BuildY, 380, 800);
    batch.draw(building2, 50, BuildY + 800, 380, 800);
    batch.draw(side1, 0, SideY, 50, 800);
    batch.draw(side2, 0, SideY + 800, 50, 800);
    batch.draw(side3, 430, SideY + 800, 50, 800);
    batch.draw(side4, 430, SideY, 50, 800);
    font.draw(batch, " SCORE : " + score, 50, 770);
    batch.draw(catanime.getKeyFrame(timeIsZero, true), catXtouch, catY, 150, 150);

    if ((CorrectCountTime - StartCountTime) > 1000) {
        batch.draw(birdanime.getKeyFrame(timeIsZero, true), obsX, obsY, 100, 100);
    }

    batch.end();
    if (ObsRect.overlaps(CatRect)) {
        //catsound.play();
        score++;
        catsound.play();
        obsY = 850;
    }
     sr.setProjectionMatrix(cam.combined);
      sr.begin(ShapeRenderer.ShapeType.Line);
       sr.setColor(new Color(0, 0, 1, 0));
      sr.rect(obsX, obsY, 100, 100);
       sr.rect(catXtouch+40,catY+40,70,70);
       sr.end();

    if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        cam.unproject(touchPos);
    }
}

public void dispose(){
    building1.dispose();
    building2.dispose();
    side1.dispose();
    side2.dispose();
    side3.dispose();
    side4.dispose();
    cat.dispose();
    catatlas.dispose();
    catsound.dispose();

}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    catXtouch=Gdx.input.getX()/scaling;//(screenX/scaling)-150/2;
    return true;
}
@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}

@Override
public boolean scrolled(int amount) {
    return false;
}
}

1 个答案:

答案 0 :(得分:0)

如果有任何人有这个问题,这就是解决方案

问题是缩放等于2而不是2.25,因为他是一个int使他成为一个双倍并显示getWidth为双

scaling = ((double)Gdx.graphics.getWidth()/480);