如何在libGDX中处理具有不同方向的多个镜头?

时间:2015-10-17 17:37:45

标签: java libgdx

我用libGDX编写游戏。主角可以射击一些飞行的敌人。 继承人我的问题: 我不知道如何同时处理多个镜头。这是我的代码:

    private Image backgroundImage = new Image(backgroundTexture);
    private Stage levelStage = new Stage(), menuStage = new Stage();
    private Table menuTable = new Table();
    private Skin menuSkin = Manager.menuSkin;
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Rectangle farmer, ufo;
    private Array<Rectangle> lasers;
    private boolean ufoMovementLeft = true;
    private boolean leftArrow = false;
    private boolean rightArrow = false;
    private float laserMovement[] = new float[100];
    private int ufoLife = 15;

    @Override
    public void show() {
        levelStage.addActor(backgroundImage);
        Gdx.input.setInputProcessor(levelStage);
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);
        batch = new SpriteBatch();

        farmer = new Rectangle();
        farmer.x = 480 / 2 - 56 / 2; farmer.y = 80;
        farmer.width = 55; farmer.height = 140;

        ufo = new Rectangle();
        ufo.x = 500 / 2; ufo.y = 600;
        ufo.width = 160; ufo.height = 70;

        lasers = new Array<Rectangle>();
    }

    public void spawnLaser() {
        Rectangle laser = new Rectangle();
        if(Gdx.input.justTouched()) {
             lasers.add(laser);
             Vector3 touchPos = new Vector3();
             touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
             laserMovement[lasers.size] = touchPos.x;
             camera.unproject(touchPos);
             laser.x = farmer.x + 50; laser.y = 110;
             laser.width = 25; laser.height = 20;
          }
    }

    @Override
    public void render(float delta) {
         Gdx.gl.glClearColor(0, 0, 0, 1);
         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
         camera.update();
         levelStage.act();
         levelStage.draw();

         batch.setProjectionMatrix(levelStage.getCamera().combined);
         batch.begin();
         movement();
         if(leftArrow) {if(farmer.x >= 50) farmer.x -= 2; 
batch.draw(farmerLeftTexture, farmer.x, farmer.y);}
         else if(rightArrow) {if(farmer.x <= 370) farmer.x += 2; 
batch.draw(farmerRightTexture, farmer.x, farmer.y);}
         else batch.draw(farmerBackTexture, farmer.x, farmer.y);
         if(ufoMovementLeft) {ufo.x -= 1; 
if(ufo.x < 50) ufoMovementLeft = false; } 
             else {ufo.x += 1; if(ufo.x > 300) ufoMovementLeft = true ; } 
         batch.draw(ufoTexture, ufo.x, ufo.y);
         if(Gdx.input.justTouched()) spawnLaser();
         Iterator<Rectangle> iter = lasers.iterator();
            while(iter.hasNext()) {
               Rectangle laser = iter.next();
               laser.y += 300 * Gdx.graphics.getDeltaTime(); 
               laser.x += (laserMovement[lasers.size] - farmer.x + farmer.x * 0.25) * 2 * Gdx.graphics.getDeltaTime();
               if(laser.overlaps(ufo)){iter.remove(); ufoLife -= 1;}
               if(laser.y + 64 > 550) iter.remove();
            }
         for(Rectangle laser: lasers) {
             batch.draw(laserTexture, laser.x, laser.y);
         }

此时的问题是拍摄的照片是&#34;好像它们是一个&#34;。如果玩家的射击方向与之前的其他射击方向不同,则此时屏幕上的每个子弹都会改变其方向。 我只是不知道如何解决这个问题,所以每个帮助都很感激:)。 干杯,Joshflux

编辑:我编辑了代码,这里有一些截图: first显示向左侧发射的射击,但在second中你可以看到,在我向右侧射了两枪后,前两枪也开始向右移动

0 个答案:

没有答案