从屏幕中删除Shaperenderer形状

时间:2017-04-28 01:12:56

标签: java android libgdx box2d

我使用Shaperenderer(LibGDX)绘制了不同的形状,并将它们连接到Box2d世界主体并使它们在重力作用下落下。我的问题是如何在屏幕底部将它们从屏幕上移除???谢谢。

感谢Kevin的回复。可能我对自己的问题不太清楚。我将提供一段代码来帮助澄清我的问题:

BodyDef floorDef = new BodyDef(); 
floorDef.type = BodyDef.BodyType.StaticBody;  
floorDef.position.set(0f,0f);

PolygonShape floorShape = new PolygonShape();
floorShape.setAsBox(Gdx.graphics.getWidth(),10f); 
floor = world.createBody(floorDef); 
floor.createFixture(floorShape, 1f); 

BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
body = world.createBody(bodyDef);

CircleShape shape = new CircleShape();
shape.setPosition(new Vector2(0f,0f));
shape.setRadius(80f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 0.5f;
body.createFixture(fixtureDef);

world.setContactListener(new ContactListener() {

            @Override
            public void beginContact(Contact contact) {

                if((contact.getFixtureA().getBody() == body &&
                        contact.getFixtureB().getBody() == floor)
                        ||
                        (contact.getFixtureA().getBody() == floor &&
                                contact.getFixtureB().getBody() == body)) {
                    Colliding = true;
                    collisionSound.play(1.0f);

                }

            }

            @Override
            public void endContact(Contact contact) {


                if((contact.getFixtureA().getBody() == body || contact.getFixtureB().getBody() == body)) {

                    world.destroyBody(body);

                    Colliding = false;

                }
            }

            @Override
            public void postSolve(Contact arg0, ContactImpulse arg1) {

            }

            @Override
            public void preSolve(Contact arg0, Manifold arg1) {

            }
        });

        // Shape is the only disposable of the lot, so get rid of it
        shape.dispose();

        floorShape.dispose();
    }


  @Override
    public void render(float delta) {

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

        world.step(Gdx.graphics.getDeltaTime(), 6, 2);

        stage.act(Gdx.graphics.getDeltaTime());

        stage.draw();

        shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
        Gdx.gl20.glLineWidth(10);
        shapeRenderer.setColor(Color.CHARTREUSE);
        shapeRenderer.rect(floor.getPosition().x,floor.getPosition().y,Gdx.graphics.getWidth(),10f);
        shapeRenderer.end();
        shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
        Gdx.gl20.glLineWidth(10);
        shapeRenderer.setColor(Color.FOREST);
        shapeRenderer.circle(body.getPosition().x ,body.getPosition().y ,80f);
        shapeRenderer.end();

    }

所以我想删除在星号之间突出显示的shaperenderer创建的形状。

1 个答案:

答案 0 :(得分:0)

从屏幕中删除ShapeRenderer形状 - >我们没有添加任何内容,因此没有删除的概念。就像我们 Box2d正文添加到物理世界中一样,我们可以从该世界中删除正文。

我们在屏幕上绘制形状,所以不知何故,我要停止绘图部分。 我的ShapeRenderer形状是视图,它的模型是Box2d体,所以当身体到达底部时移除该身体。

private Array<Body> array;

@Override
public void create(){         //create or show depending on who is parent
    array=new ArrayList<Body>();
}

@Override
public void render(float delta) {

    ....
    world.getBodies(array);
    for (Body body:array){
        if(body.getType()== BodyDef.BodyType.DynamicBody) { // I've to draw only circle
            shapeRenderer.circle(body.getPosition().x ,body.getPosition().y ,80f);
        }
    }
}