libGDX:如何让一个世界移动

时间:2016-07-30 18:05:59

标签: java libgdx box2d

我是libgdx中的新手,正在制作一个简单的2D游戏,我希望我的box2d世界(或背景)随输入触摸移动(当我按下按钮时向上移动)但我不想知道如何移动世界并将我的玩家放在中心位置。我想知道:

  1. 如何使用输入控件移动我的世界。
  2. 将我的播放器放在中间位置。
  3. 并且看起来每当我玩这个游戏时我的玩家都在移动,而不是世界。 如果你能提供一个答案的例子。

    所以,这是我的代码

     public void show() {
            world = new World(new Vector2(0, 25), true);
            renderer = new Box2DDebugRenderer();
            cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
          /*  cam.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
            cam.update();*/
    
    
            shapeRenderer = new ShapeRenderer();
    
            batch = new SpriteBatch();
            texture = new Texture("img/imgs.png");
            sprite = new Sprite(texture);
            sprite.setSize(10, 10);
            sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
            sprite.setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
            //sprite.setBounds(200, 200, 64, 64);
            //sprite.setPosition(Gdx.graphics.getWidth()/2 -sprite.getWidth()/2, Gdx.graphics.getHeight()/2-sprite.getHeight()/2);
    
    
           /* //Body definition
            BodyDef bodyDef = new BodyDef();
            bodyDef.type = BodyDef.BodyType.DynamicBody;
            bodyDef.position.set(0, Gdx.graphics.getHeight() / 2 - 250);
    
            box = world.createBody(bodyDef);
    
            //Polygon Shape
            PolygonShape shape = new PolygonShape();
            shape.setAsBox(10, 20);
    
    
            //fixture definition
            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.shape = shape;
            fixtureDef.density = 5f;
            fixtureDef.friction = .5f;
    
            box.createFixture(fixtureDef);
    
            box.setUserData(sprite);
    
            shape.dispose();
    
    */    }
    
        @Override
        public void render(float delta) {
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
            world.step(1 / 60f, 6, 2);
            cam.translate(0, 12);
            cam.update();
            batch.setProjectionMatrix(cam.combined);
    
            if(Gdx.input.isKeyPressed(Input.Keys.A))
                spriteY += Gdx.graphics.getDeltaTime() * spriteSpeed;
            if(Gdx.input.isKeyPressed(Input.Keys.D))
                spriteY += Gdx.graphics.getDeltaTime() * sprite1Speed;
    
    
    
            batch.begin();
            batch.draw(sprite, spriteX, spriteY);
            batch.end();
    
            shapeRenderer.setColor(Color.RED);
            shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
            shapeRenderer.rect(0, 200, 50, 50);
            shapeRenderer.translate(1, 0, 0);
            shapeRenderer.end();
    
    
        }
    
        @Override
        public void resize(int width, int height) {
            cam.viewportWidth = width;
            cam.viewportHeight = height;
            cam.update();
    
    
        }
    
        @Override
        public void pause() {
    
        }
    
        @Override
        public void resume() {
    
        }
    
        @Override
        public void hide() {
            dispose();
        }
    
        @Override
        public void dispose() {
            //world.dispose();
            shapeRenderer.dispose();
            batch.dispose();
    
    
        }
    
    } 
    

    对不起,对于代码中的混乱(我是一个新手),我希望我的世界向上移动,输入控件和我的精灵只是放在中心。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

你的问题听起来像是想创造一个平台游戏或者有点儿。

我会告诉你,在创建(2d)平台游戏时构建逻辑的最佳方法是你的角色应该始终保持它的位置相同,背景和其他固体物体应移动朝向玩家(基于输入控件或自动),所以给你看起来像你的播放器自然移动和背景&物体以他的方式出现。

我的回答非常一般,因为你的问题很普遍。如果您需要代码,您必须向我们提供代码,以便我们知道您遇到的问题。祝你好运:)