libgdx 和 stage 的相机问题

时间:2021-05-03 10:48:04

标签: java android libgdx game-development stage

我不久前开始使用 libgdx,有一次我开始使用带有舞台的相机,但遇到了很多问题:

  1. 在我的舞台上,我添加了一个演员来绘制精灵,但我看不到精灵,即使我直接绘制它。我只有在不使用舞台时才能看到它。

2)如果我画一个没有舞台的精灵,触摸不会移动它(它应该移动到我触摸的地方),尽管它可以很好地移动圆形演员。

3)camera.zoom/stage.getCamera().zoom eps 似乎没有任何效果,除了使触摸变得更糟之外,甚至现在连圆圈都不会移动到触摸。

这是代码,它是一个屏幕类,我只是通过 changeScreen 与游戏类一起运行。我在安卓设备上测试过。 TheWorld.jpeg 是一个 1024x576 的图像,如果这意味着什么的话。

如果我的问题不清楚,请告诉我。


import com.badlogic.gdx.Gdx;
//import com.badlogic.gdx.


import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.graphics.glutils.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.utils.viewport.*;
import com.badlogic.gdx.InputProcessor;

import com.badlogic.gdx.math.Vector3;
import android.graphics.drawable.shapes.*;


class Shape extends Actor {
    public ShapeRenderer shapeRenderer;
    float x,y;
    
    public Shape(int xp, int yp){
        shapeRenderer = new ShapeRenderer();
        x = xp;
        y = yp;
    }
    @Override
    public void draw(Batch batch, float delta){

        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(0, 1, 0, 1);
        
        shapeRenderer.circle(this.x,
        this.y, 75);
        shapeRenderer.end();
    }
}

class Drawing extends Actor {
    private Sprite tex;
    private Shape shape;
    public Texture textureBackground;

    private Stage stage;
    public ShapeRenderer shapeRenderer;
    
    public Drawing(Sprite t, Shape s, Stage ss){
        tex = t;
        shape = s;
        stage = ss;
        shapeRenderer = new ShapeRenderer();
    }
    @Override
    public void draw(Batch batch, float delta) {
        tex.draw(stage.getBatch());

        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(1, 0, 0, 1);

        shapeRenderer.circle(shape.x,
                             shape.y, 90);
        shapeRenderer.end();
        batch.draw(textureBackground, shape.x, shape.y, 50,50);
        //theWorld.draw(stage.getBatch());
        
    }
    
}

public class Screen01 implements Screen, InputProcessor
{

        //Stage stage;
        Shape shape;
        MyGdxGame parent;
        
        public OrthographicCamera camera;
        private Stage stage;
        private Viewport viewport;
       
        private Texture textureBackground;
        private Sprite theWorld;


        //size I use to test
        private static final int WORLD_WIDTH = 400;
        private static final int WORLD_HEIGHT = 400;
    
        private Drawing tex;
        
        public Screen01(MyGdxGame game){
            camera = new OrthographicCamera(WORLD_WIDTH,WORLD_HEIGHT);
            camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
            
            viewport = new StretchViewport(WORLD_WIDTH, WORLD_HEIGHT, camera);

            stage = new Stage(viewport);
            stage.getViewport().apply();
            Gdx.input.setInputProcessor(this);

            textureBackground = new Texture("TheWorld.jpeg");
            theWorld = new Sprite(textureBackground);//new Texture(Gdx.files.internal("TheWorld.png")));
            theWorld.setPosition(0,0);
        
            
        
            /*stage = new Stage(
                new StretchViewport(
                    Gdx.graphics.getWidth(),
                    Gdx.graphics.getHeight()
                )
            );*/
            
            shape = new Shape(30,30);
            tex = new Drawing(theWorld,shape,stage);
            tex.textureBackground = textureBackground;
            parent = game;
            stage.addActor(shape);
            stage.addActor(tex);
            
        }
        
        @Override
        public void show() {
             stage.addActor(shape);
             stage.addActor(tex);
             //Gdx.input.setInputProcessor(stage);
            // TODO Auto-generated method stub
        }

        @Override
        public void render(float delta) {

            Gdx.gl.glClearColor(.25f, .25f, .25f, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


            stage.getBatch().setProjectionMatrix(stage.getCamera().combined);
            stage.getCamera().update();

            stage.getBatch().begin();
            
            //if I do it like this it will draw
            //stage.getBatch().draw(textureBackground, shape.x, shape.y, 10,10);

            stage.getBatch().end();

            stage.act(Gdx.graphics.getDeltaTime());
            stage.draw();
            
            //still doesn't draw
            tex.draw(stage.getBatch(),0);
                // TODO Auto-generated method stub
            }


        @Override
        public void resize(int width, int height) {
            stage.getViewport().update(width, height);
            stage.getCamera().viewportWidth = WORLD_WIDTH;
            stage.getCamera().viewportHeight = WORLD_HEIGHT * height / width;
            stage.getCamera().position.set(stage.getCamera().viewportWidth / 2, stage.getCamera().viewportHeight / 2, 0);
            stage.getCamera().update();
        }

        @Override
        public void pause() {
           // camera.zoom -= 2;
            // TODO Auto-generated method stub
        }
        
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            Vector3 worldCoordinates = camera.unproject(
                new Vector3(screenX,screenY,0),
                viewport.getScreenX(),viewport.getScreenY(),
                camera.viewportWidth,
                camera.viewportHeight);
            //camera.zoom++;
            //worldCoordinates = viewport.unproject(worldCoordinates);
            
            //this works with normal zoom
            shape.x = (worldCoordinates.x);
            shape.y = (worldCoordinates.y);
            
            //this doesn't work
            theWorld.setPosition(worldCoordinates.x, worldCoordinates.y);
            
            //theWorld.setCenter(worldCoordinates.x-theWorld.getWidth()/2
            //, worldCoordinates.y-theWorld.getHeight()/2);
            //camera.zoom++;
            return false;
        }

        @Override
        public void resume() {
            // TODO Auto-generated method stub
        }

        @Override
        public void hide() {
            // TODO Auto-generated method stub
        }

        @Override
        public void dispose() {
            stage.dispose();
        }
        
        @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;
        }
}```[TheWorld.jpeg][1]


  [1]: https://i.stack.imgur.com/e63AT.jpg

0 个答案:

没有答案