2D资产渲染问题

时间:2015-04-16 17:58:50

标签: java libgdx rendering

我是2D编程的新手,现在我正在尝试用一些在屏幕底部滚动的云和管道制作2D游戏。

处理云层很容易,但是我对管道有一些问题 - 我想把它们渲染成飞鸟中的那些,但在我的情况下是水平的。我无法形象我的代码出了什么问题。

Screenshot

   public class GameRenderer {

    private GameWorld myWorld;
    private OrthographicCamera cam;
    private ShapeRenderer shapeRenderer;

    private SpriteBatch batcher;

    private int midPointY;
    private int gameHeight;

    // Game Objects
    private Margarine marg;
    private ScrollHandler scroller;
    private Clouds cloud1, cloud2, cloud3;
    private Pipe pipe1, pipe2, pipe3;

    // Game Assets
    private TextureRegion cloud;
    private TextureRegion pipe;

    public GameRenderer(GameWorld world, int gameHeight, int midPointY) {
        myWorld = world;

        this.gameHeight = gameHeight;
        this.midPointY = midPointY;

        cam = new OrthographicCamera();
        cam.setToOrtho(false, 136, gameHeight);

        batcher = new SpriteBatch();
        batcher.setProjectionMatrix(cam.combined);
        shapeRenderer = new ShapeRenderer();
        shapeRenderer.setProjectionMatrix(cam.combined);

        // Call helper methods to initialize instance variables
        initGameObjects();
        initAssets();
    }

    private void initGameObjects() {
        marg = myWorld.getMargarine();
        scroller = myWorld.getScroller();
        cloud1 = scroller.getCloud1();
        cloud2 = scroller.getCloud2();
        cloud3 = scroller.getCloud3();
         pipe1 = scroller.getPipe1();
         pipe2 = scroller.getPipe2();
         pipe3 = scroller.getPipe3();  

    }

    private void initAssets() {

        cloud = AssetLoader.cloud;
        pipe = AssetLoader.pipe;
    }

    public void render(float runTime) {

        Gdx.gl.glClearColor(102 / 255.0f, 102 / 255.0f, 255 / 255.0f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        shapeRenderer.begin(ShapeType.Filled);


        shapeRenderer.end();

        batcher.begin();

        //clouds

        batcher.draw(cloud, cloud1.getX(), cloud1.getY(), cloud1.getWidth(),
                cloud1.getHeight());

        batcher.draw(cloud, cloud2.getX(), cloud2.getY(), cloud2.getWidth(),
                cloud2.getHeight());

        batcher.draw(cloud, cloud3.getX(), cloud3.getY(), cloud3.getWidth(),
                cloud3.getHeight());

        //pipes

        batcher.draw(pipe, pipe1.getX(), pipe1.getY(), pipe1.getWidth(),
                pipe1.getHeight());
        batcher.draw(pipe, pipe1.getX(), pipe1.getY() + pipe1.getHeight() + 45,
                pipe1.getWidth(), midPointY + 66 - (pipe1.getHeight() + 45));

        batcher.draw(pipe, pipe2.getX(), pipe2.getY(), pipe2.getWidth(),
                pipe2.getHeight());
        batcher.draw(pipe, pipe2.getX(), pipe2.getY() + pipe2.getHeight() + 45,
                pipe2.getWidth(), midPointY + 66 - (pipe2.getHeight() + 45));

        batcher.draw(pipe, pipe3.getX(), pipe3.getY(), pipe3.getWidth(),
                pipe3.getHeight());
        batcher.draw(pipe, pipe3.getX(), pipe3.getY() + pipe3.getHeight() + 45,
                pipe3.getWidth(), midPointY + 66 - (pipe3.getHeight() + 45));


        //margarine
        batcher.draw(AssetLoader.marg, marg.getX(), marg.getY(),
                marg.getWidth(), marg.getHeight());



        batcher.end();

    }

}


public class Pipe extends Scrollable {

    private Random r;

    // When Pipe's constructor is invoked, invoke the super (Scrollable)
    // constructor
    public Pipe(float x, float y, int width, int height, float scrollSpeed) {
        super(x, y, width, height, scrollSpeed);
        // Initialize a Random object for Random number generation
        r = new Random();
    }

    @Override
    public void reset(float newX) {
        // Call the reset method in the superclass (Scrollable)
        super.reset(newX);
        // Change the height to a random number
        height = r.nextInt(90) + 15;
    }
}

 public ScrollHandler(float yPos) {

        cloud1= new Clouds(80, 0, 30, 80, SCROLL_SPEED);
        cloud2 = new Clouds(50 , cloud1.getTailY(), 30, 80, SCROLL_SPEED);
        cloud3 = new Clouds(10, cloud2.getTailY(), 30, 80, SCROLL_SPEED);

        pipe1 = new Pipe(210, 0, 22, 60, SCROLL_SPEED);
        pipe2 = new Pipe(0, pipe1.getTailY() + PIPE_GAP, 22, 70, SCROLL_SPEED);
        pipe3 = new Pipe(0, pipe1.getTailY() + PIPE_GAP, 22, 60, SCROLL_SPEED);
    }

    public void update(float delta) {
        // Update our objects

        cloud1.update(delta);
        cloud2.update(delta);
        cloud3.update(delta);

        pipe1.update(delta);
        pipe2.update(delta);
        pipe3.update(delta);


         if (cloud1.isScrolledDown()) {
            cloud1.reset(cloud3.getTailY());

         } else if (cloud2.isScrolledDown()) {
            cloud2.reset(cloud1.getTailY());

         } else if (cloud3.isScrolledDown()) {
            cloud3.reset(cloud2.getTailY());
         }


         if (pipe1.isScrolledDown()) {
             pipe1.reset(pipe3.getTailY() + PIPE_GAP);
         } else if (pipe2.isScrolledDown()) {
             pipe2.reset(pipe1.getTailY() + PIPE_GAP);

         } else if (pipe3.isScrolledDown()) {
             pipe3.reset(pipe2.getTailY() + PIPE_GAP);
         }

}

    public Clouds getCloud1() {
        return cloud1;
    }

    public Clouds getCloud2() {
        return cloud2;
    }

    public Clouds getCloud3() {
        return cloud3;
    }

    public Pipe getPipe1() {
        return pipe1;
    }

    public Pipe getPipe2() {
        return pipe2;
    }

    public Pipe getPipe3() {
        return pipe3;
    }
}

1 个答案:

答案 0 :(得分:0)

解决。我错误地改变了高度和宽度。