我是游戏开发的新手,并尝试使用AndEngine构建赛车游戏。 当我的精灵(汽车)与硬币发生碰撞时,它会播放声音并消失 我使用平铺地图编辑器来加载我的硬币对象 平铺地图编辑器中的坐标系统将反转,该坐标系统为地图和anndngine gles 2 - 锚点中心创建xml。我以为tmxExtension会处理这个转换.. 到目前为止,我正在计算每个对象位置并在xml编辑器中更改其位置以更正位置。还有其他更简单的方法吗?
以下是我加载地图的代码:
private void createLevel() {
coins = new LinkedList < Sprite > ();
try {
final TMXLoader tmxLoader = new TMXLoader(activity.getAssets(), engine.getTextureManager(),
TextureOptions.BILINEAR_PREMULTIPLYALPHA, vbom);
this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/uom_map_test2.tmx");
} catch (final TMXLoadException e) {
Debug.e(e);
}
FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
Rectangle rect1 = new Rectangle(
0,
0,
1,
camera.getBoundsYMax(),
vbom);
Body wall1 = PhysicsFactory.createBoxBody(physicsWorld, rect1, BodyType.StaticBody, wallFixtureDef);
wall1.setUserData(wall1);
FixtureDef wallFixtureDef2 = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
Rectangle rect2 = new Rectangle(
1100,
0,
1,
camera.getBoundsYMax(),
vbom);
Body wall2 = PhysicsFactory.createBoxBody(physicsWorld, rect2, BodyType.StaticBody, wallFixtureDef2);
for (final TMXObjectGroup group: this.mTMXTiledMap.getTMXObjectGroups()) {
for (final TMXObject object: group.getTMXObjects()) {
if (object.getName().equals("coin")) {
Sprite coin = new Sprite(object.getX(), object.getY(), object.getWidth(), object.getHeight(), resourcesManager.coinTextureRegion, vbom);
FixtureDef coinFixtureDef = PhysicsFactory.createFixtureDef(0, 0.3f, 0.3f);
Body bod = PhysicsFactory.createBoxBody(physicsWorld, coin, BodyType.DynamicBody, coinFixtureDef);
bod.setUserData("coin");
coins.add(coin);
coin.setUserData(bod);
} else if (object.getName().equals("sea")) {
FixtureDef seaFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0f);
Rectangle rect = new Rectangle(
object.getX(),
object.getY(),
object.getWidth(),
object.getHeight(),
vbom);
Body bod = PhysicsFactory.createBoxBody(physicsWorld, rect, BodyType.DynamicBody, seaFixtureDef);
bod.setUserData("sea");
}
}
}
/*for(TMXLayer layer : this.mTMXTiledMap.getTMXLayers()){
layer.detachSelf();
layer.setPosition(0,0);
attachChild(layer);*/
for (int layerID = 0; layerID < this.mTMXTiledMap.getTMXLayers().size(); layerID++) {
this.mTMXTiledMap.getTMXLayers().get(layerID).detachSelf();
attachChild(this.mTMXTiledMap.getTMXLayers().get(layerID));
// layer0:sea layer1:grass layer2:road
for (int i = 0; i < coins.size(); i++) {
this.attachChild(coins.get(i));
}
camera.setBounds(0, 0, mTMXTiledMap.getWidth(), mTMXTiledMap.getHeight());
camera.setBoundsEnabled(true);
}
}
另外,我使用联系人监听器来管理硬币碰撞
public void beginContact(Contact contact) {
Body a = null, b = null;
// TODO Auto-generated method stub
try {
a = contact.getFixtureA().getBody();
b = contact.getFixtureB().getBody();
} catch (NullPointerException n) {
//do nothing
}
if (a != null && b != null) {
if (a.getUserData() != null && b.getUserData() != null) {
if (a.getUserData().equals("player") && b.getUserData().equals("coin") || b.getUserData().equals("player") && a.getUserData().equals("coin")) {
if (a.getUserData().equals("coin")) {
removeCoins(a);
resourcesManager.coinCollect.play();
addToScore(10);
showLevelCleared();
} else if (b.getUserData().equals("coin")) {
removeCoins(b);
resourcesManager.coinCollect.play();
addToScore(5);
}
}
}
}
}
然而并非所有的硬币和#39;检测到碰撞。我得到了一个空的pointerException,这就是为什么我包含了一个catch 有什么理由吗?
答案 0 :(得分:0)
我建议不要创建一个单独的timerhandler来检查碰撞,而是在onManagedUpdate中执行所有这些操作