纹理不是分开的

时间:2016-06-20 22:07:05

标签: java android libgdx

我正在尝试绘制不同纹理的气球,我希望它们在不同的时间出现在每个气球之间大约半秒,然后在世界中移动。渲染第一个气球,但大约半秒后,其他气球将渲染到前一个气球旁边,而不是在初始位置: 这是代码:

private Ballons ballons;
    private Ballons ballons2;
    private Ballons ballons3;
    private Ballons ballons4;

    private Texture background;
    public Celebration(Fruits game, float level)
    {
        this.game=game;
        this.level=level;
        gamecam=new OrthographicCamera();
        gameport=new StretchViewport(820/Fruits.PPM,580/Fruits.PPM,gamecam);
        stage=new Stage(gameport,((Fruits) game).batch);
        background=new Texture("Wining.jpg");
        gamecam.position.set(gameport.getWorldWidth()/2f,gameport.getWorldHeight()/2f,0);
        temp=0;
        counter=400;
        world=new World(new Vector2(0,0.5f),true);
        b2dr=new Box2DDebugRenderer();
        ballons=new Ballons(world,this,1,140);
        ballons4=new Ballons(world,this,2,290);
        ballons3=new Ballons(world,this,3,480);
        ballons2=new Ballons(world,this,4,650);


    }

    @Override
    public void show() {

    }
    public void handleinput(float dt)
    {
        if(Gdx.input.isTouched())
            game.setScreen(new GlobalWorld(game, 1));
    }
    public void update(float dt)
    {
        handleinput(dt);
        world.step(1 /60f, 6, 2);
        //player.update(dt);
        gamecam.update();
//        renderer.setView(gamecam);
        ballons.update(dt);
        ballons2.update(dt);
        ballons3.update(dt);
        ballons4.update(dt);
    }

    @Override
    public void render(float delta) {
        float level;
        update(delta);
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        counter--;
        b2dr.render(world, gamecam.combined);
        game.batch.setProjectionMatrix(gamecam.combined);

        game.batch.begin();

        game.batch.draw(background, 0, 0, gameport.getWorldWidth(), gameport.getWorldHeight());
        if (counter < 200) {
            ballons.draw(game.batch);
            ballons2.draw(game.batch);

        }
        if (counter < 100) {
            ballons3.draw(game.batch);
            ballons4.draw(game.batch);

        }
        game.batch.end();


    }

    @Override
    public void resize(int width, int height) {
        gameport.update(width, height);


    }

}

气球课程:

public class Ballons extends Sprite {
    public World world;
    public Body b2body;
    private TextureRegion collectorStand;
    public Ballons(World world,Celebration screen,float image, float x)
    {
       // super(screen.getAtlas().findRegion("boy1"));
        this.world=world;
        defineCollector(x);
        if(image==1) {
            collectorStand = new TextureRegion(new Texture("Balloon1.png"));
            setBounds(0, 0, 200 / Fruits.PPM, 200 / Fruits.PPM);
        }
        else if(image==2) {
            collectorStand = new TextureRegion(new Texture("Balloon2.png"));
            setBounds(0, 0, 170 / Fruits.PPM, 185 / Fruits.PPM);
        }
        else if(image==3) {
            collectorStand = new TextureRegion(new Texture("Balloon3.png"));
            setBounds(0, 0, 150 / Fruits.PPM, 175 / Fruits.PPM);
        }
        else if(image==4) {
            collectorStand = new TextureRegion(new Texture("Balloon4.png"));
            setBounds(0, 0, 150 / Fruits.PPM, 175 / Fruits.PPM);
        }
        else if(image==5) {
            collectorStand = new TextureRegion(new Texture("Balloon5.png"));
            setBounds(0, 0, 150 / Fruits.PPM, 175 / Fruits.PPM);
        }
        setRegion(collectorStand);
    }
    public void update(float dt)
    {
        setPosition(b2body.getPosition().x-getWidth()/2,b2body.getPosition().y-getHeight()/2.8f);
    }
    public void defineCollector(float x)
    {
        BodyDef bdef=new BodyDef();
        bdef.position.set(x/Fruits.PPM,-20/Fruits.PPM);
        bdef.type=BodyDef.BodyType.DynamicBody;
        b2body=world.createBody(bdef);
        FixtureDef fdef=new FixtureDef();
        CircleShape shape=new CircleShape();
        shape.setRadius(15/Fruits.PPM);
        fdef.shape=shape;
        b2body.createFixture(fdef);
    }
}

2 个答案:

答案 0 :(得分:0)

无论您在counter方法中调用update(delta);,实际render值是多少。我看到了

    ballons.update(dt);
    ballons2.update(dt);
    ballons3.update(dt);
    ballons4.update(dt);

很遗憾,您尚未附加Ballons课程实施,但我非常确定update方法正在修改气球的位置。

您应该在if-else语句中调用ballon的update方法,而不是在update方法中。它可能看起来像

    public void update(float dt)
    {
        handleinput(dt);
        world.step(1 /60f, 6, 2);
        //player.update(dt);
        gamecam.update();
        //renderer.setView(gamecam);
    }

    @Override
    public void render(float delta) {
        ...

        if (counter < 200) {
            ballons.update(dt);
            ballons2.update(dt);

            ballons.draw(game.batch);
            ballons2.draw(game.batch);
        }
        if (counter < 100) {
            ballons3.update(dt);
            ballons4.update(dt);

            ballons3.draw(game.batch);
            ballons4.draw(game.batch);
        }

答案 1 :(得分:0)

问题是你的更新功能只是将气球的位置固定到Box2D机身。由于Box2D主体是在创建时创建并添加到世界中的,因此即使没有绘制,主体仍然会移动。

您可以禁用气球的物体,直到它们准备好被绘制,如下所示:

Balloons.java

public void defineCollector(float x) {
    BodyDef bdef=new BodyDef();
    bdef.position.set(x/Fruits.PPM,-20/Fruits.PPM);
    bdef.type=BodyDef.BodyType.DynamicBody;
    b2body=world.createBody(bdef);
    FixtureDef fdef=new FixtureDef();
    CircleShape shape=new CircleShape();
    shape.setRadius(15/Fruits.PPM);
    fdef.shape=shape;
    b2body.createFixture(fdef);

    b2body.setActive(false);
}

Celebration.java

public Celebration(Fruits game, float level) {
    ...

    stage.addAction(
        Actions.sequence(
            Actions.delay(0.5f), 
            Actions.run(
                new Runnable() {
                    ballons.b2body.setActive(true);
                    ballons2.b2body.setActive(true);
                }
            ),
            Actions.delay(0.5f), 
            Actions.run(
                new Runnable() {
                    ballons3.b2body.setActive(true);
                    ballons4.b2body.setActive(true);
                }
            )
        )
    );
}

public void update(float dt) {
    stage.act(dt);

    ...
}

@Override
public void render(float delta) {
    ...

    if (ballons.b2body.isActive()) {
        ballons.draw(game.batch);
    }
    if (ballons2.b2body.isActive()) {
        ballons2.draw(game.batch);
    }
    if (ballons3.b2body.isActive()) {
        ballons3.draw(game.batch);
    }
    if (ballons4.b2body.isActive()) {
        ballons4.draw(game.batch);
    }
}

我建议使用Scene2D来处理您的逻辑,方法是更改​​气球以扩展Image并将气球添加到舞台上,如下所示:stage.addActor(balloon)。然后,您可以在act方法内处理逻辑,并覆盖draw以将纹理定位在Box2D主体上。