Android Studio Box2D会让循环工作

时间:2016-02-29 02:38:41

标签: java android android-studio libgdx

我的Box2D项目几乎完全完成了,但是,当我尝试第一个随机出现的障碍并使用while循环无限重复时,while循环不起作用而是只出现一次,我想要我的游戏如下所示,如本视频所示,“https://www.youtube.com/watch?v=X-jEZHDN-gw”while循环不起作用,我做错了什么?有人可以帮帮我吗?

Box2D类:

package com.circlecrashavoider.scene2d;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.circlecrashavoider.BaseScreen;
import com.circlecrashavoider.MainGame;

/**
 * Created by Felipe on 2/19/2016.
 */



public class Box2DScreen extends BaseScreen {
    public Box2DScreen(MainGame game) {
        super(game);
    }

    private World world;

    private Box2DDebugRenderer renderer;

    private OrthographicCamera camera;

    private Body playerBody, floorBody, obstacleBody, obstacle2Body;

    private Fixture playerFixture, floorFixture, obstacleFixture, obstacle2Fixture;

    private boolean mustJump, playerJumping, playerAlive = true;
    @Override
    public void show() {
        world = new World(new Vector2(0, -10), true);
        renderer = new Box2DDebugRenderer();
        camera = new OrthographicCamera(16, 9);
        camera.translate(0, 1);

        world.setContactListener(new ContactListener() {
            @Override
            public void beginContact(Contact contact) {
                Fixture fixtureA = contact.getFixtureA(), fixtureB = contact.getFixtureB();

                if ((fixtureA.getUserData().equals("player") && fixtureB.getUserData().equals("floor")) ||
                        (fixtureA.getUserData().equals("floor") && fixtureB.getUserData().equals("player"))) {
                    if (Gdx.input.isTouched()) {
                        mustJump = true;
                    }
                    playerJumping = false;

                }

                if ((fixtureA.getUserData().equals("player") && fixtureB.getUserData().equals("obstacle")) ||
                        (fixtureA.getUserData().equals("obstacle") && fixtureB.getUserData().equals("player"))) {
                    playerAlive = false;

                }
                if ((fixtureA.getUserData().equals("player") && fixtureB.getUserData().equals("obstacle2")) ||
                        (fixtureA.getUserData().equals("obstacle2") && fixtureB.getUserData().equals("player"))) {
                    playerAlive = false;
                }


            }


            @Override
            public void endContact(Contact contact) {
                Fixture fixtureA = contact.getFixtureA(), fixtureB = contact.getFixtureB();
                if (fixtureA == playerFixture && fixtureB == floorFixture) {
                    playerJumping = true;
                }
                if (fixtureA == floorFixture && fixtureB == playerFixture) {
                    playerJumping = true;
                }

            }

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {

            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {

            }
        });

        playerBody = world.createBody(createplayerBodyDef());
        floorBody = world.createBody(createfloorBodyDef());
        obstacleBody = world.createBody(createobstacleBodyDef(0.5f));
        obstacle2Body = world.createBody(createobstacle2BodyDef(-0.5f));





        CircleShape playerShape = new CircleShape();
        playerShape.setRadius(0.5f);
        playerFixture = playerBody.createFixture(playerShape,1);
        playerShape.dispose();

        PolygonShape floorShape = new PolygonShape();
        floorShape.setAsBox(500,1);
        floorFixture = floorBody.createFixture(floorShape, 1);
        floorShape.dispose();

        obstacleFixture = createobstacleFixture(obstacleBody);
        obstacle2Fixture = createobstacle2Fixture(obstacle2Body);


        playerFixture.setUserData("player");
        floorFixture.setUserData("floor");
        obstacleFixture.setUserData("obstacle");
        obstacle2Fixture.setUserData("obstacle2");
    }

    private BodyDef createobstacleBodyDef(float x) {
        while (true) {

            BodyDef def = new BodyDef();
            def.position.set(0, 0.5f);
            return def;
        }
    }

    private BodyDef createobstacle2BodyDef(float x) {
        BodyDef def = new BodyDef();
        def.position.set(6,2.5f);
        return def;
    }



    private BodyDef createfloorBodyDef() {
        BodyDef def = new BodyDef();
        def.position.set(0,-1);
        return def;
    }

    private BodyDef createplayerBodyDef() {
        BodyDef def = new BodyDef();
        def.position.set(-5 ,0);
        def.type = BodyDef.BodyType.DynamicBody;
        return def;
    }

    private Fixture createobstacleFixture(Body obstacleBody) {
        while (true) {
            Vector2[] vertices = new Vector2[3];
            vertices[0] = new Vector2(-0.5f, -0.5f);
            vertices[1] = new Vector2(0.5f, -0.5f);
            vertices[2] = new Vector2(0, 0.5f);
            PolygonShape shape = new PolygonShape();
            shape.set(vertices);
            Fixture fix = obstacleBody.createFixture(shape, 1);
            shape.dispose();
            return fix;
        }
    }
    private Fixture createobstacle2Fixture(Body obstacle2Body) {
        Vector2[] vertices = new Vector2[3];
        vertices[2] = new Vector2(-0.5f, 0.5f);
        vertices[1] = new Vector2(0.5f, 0.5f);
        vertices[0] = new Vector2(0, -0.5f);
        PolygonShape shape = new PolygonShape();
        shape.set(vertices);
        Fixture fix = obstacle2Body.createFixture(shape, 1);
        shape.dispose();
        return fix;

    }




    @Override
    public void dispose() {
        playerBody.destroyFixture(playerFixture);
        obstacleBody.destroyFixture(obstacleFixture);
        world.destroyBody(playerBody);
        world.destroyBody(obstacleBody);
        world.dispose();
        renderer.dispose();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        if(mustJump) {
            mustJump = false;
            jump();
        }

        if (Gdx.input.justTouched()) {
            mustJump = true;
        }

        if (playerAlive) {
            float velocityY = playerBody.getLinearVelocity().y;
            playerBody.setLinearVelocity(8, velocityY);
        }


        world.step(delta, 6, 2);

        camera.update();
        renderer.render(world, camera.combined);
    }

    private void jump () {
        Vector2 position = playerBody.getPosition();
        playerBody.applyLinearImpulse(0, 6, position.x, position.y, true);
    }
}

1 个答案:

答案 0 :(得分:0)

return fix;将返回;退出while循环的第一次迭代。