gpt4 book ai didi

java - Android Studio Box2D 循环工作时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:26 25 4
gpt4 key购买 nike

我的 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);
}
}

最佳答案

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

关于java - Android Studio Box2D 循环工作时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35691086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com