我的游戏几乎已经准备好了,然而,当我添加障碍物碰撞并将其设置为玩家活着时,圆圈就会继续前进,就像障碍物不是敌人一样,我该如何解决这个问题呢?这是MainGame.java中的代码:
package com.circlecrashavoider;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
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.Manifold;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.circlecrashavoider.entities.FloorEntity;
import com.circlecrashavoider.entities.ObstacleEntity;
import com.circlecrashavoider.entities.PlayerEntity;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Created by Felipe on 2/22/2016.
*/
public class GameScreen extends BaseScreen {
private Stage stage;
private World world;
private PlayerEntity player;
private List<FloorEntity> floorList = new ArrayList<FloorEntity>();
private List<ObstacleEntity> obstacleList = new ArrayList<ObstacleEntity>();
public GameScreen(MainGame game) {
super(game);
stage = new Stage(new FitViewport(1024, 620));
world = new World(new Vector2(0, -10), true);
world.setContactListener(new ContactListener() {
private boolean areCollided(Contact contact, Object userA, Object userB) {
Object userDataA = contact.getFixtureA().getUserData();
Object userDataB = contact.getFixtureB().getUserData();
if (userDataA == null || userDataB == null) {
return false;
}
return (userDataA.equals(userA) && userDataB.equals(userB)) ||
(userDataA.equals(userB) && userDataB.equals(userA));
}
@Override
public void beginContact(Contact contact) {
if (areCollided(contact, "player", "floor")) {
player.setJumping(false);
}
if (areCollided(contact, "player", "obstacle")) {
if (player.isAlive()) {
player.setAlive(false);
}
}
}
@Override
public void endContact(Contact contact) {
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
});
}
@Override
public void show() {
Texture playerTexture = game.getManager().get("player.png");
Texture floorTexture = game.getManager().get("floor.png");
Texture overfloorTexture = game.getManager().get("overfloor.png");
Texture obstacleTexture = game.getManager().get("obstacle.png");
player = new PlayerEntity(world, playerTexture, new Vector2(1, 2));
floorList.add(new FloorEntity(world, floorTexture, overfloorTexture, 0, 1000, 1));
obstacleList.add(new ObstacleEntity(world, obstacleTexture, 6, 1));
stage.addActor(player);
for (FloorEntity floor : floorList) {
stage.addActor(floor);
}
for (ObstacleEntity obstacle : obstacleList) {
stage.addActor(obstacle);
}
}
@Override
public void hide() {
player.detach();
player.remove();
for (FloorEntity floor: floorList) {
floor.detach();
floor.remove();
}
for (ObstacleEntity obstacle: obstacleList) {
obstacle.detach();
obstacle.remove();
}
}
@Override
public void render(float delta) {
Gdx.gl20.glClearColor(0.5f, 0.6f, 1, 3f);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
world.step(delta, 6 ,2);
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
world.dispose();
}
}
以下是您需要更多信息的下载链接,这是我完成游戏所需要做的最后一件事。
http://www.mediafire.com/download/992ys6adq4s6s4y/CircleCrashAvoider.7z