我设法加载了一个tmx地图,现在我想创建一个精灵无法移动的障碍物,我像这样恢复了障碍物:
try {
final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, new ITMXTilePropertiesListener() {
@Override
public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {
/* We are going to count the tiles that have the property "cactus=true" set. */
if(pTMXTileProperties.containsTMXProperty("obstacle", "true")) {
//TMXTiledMapExample.this.mCactusCount++;
//coffins[coffinPtr++] = pTMXTile.getTileRow() * 15 + pTMXTile.getTileColumn();
}
}
});我如何处理与障碍物的碰撞,以防止玩家穿过障碍物(如墙)?
发布于 2013-03-06 22:55:48
因为我用的是这个
if(pTMXTileProperties.containsTMXProperty("obstacle", "true")) {
//TMXTiledMapExample.this.mCactusCount++;
//coffins[coffinPtr++] = pTMXTile.getTileRow() * 15 + pTMXTile.getTileColumn();
//initRacetrackBorders2();
// This is our "wall" layer. Create the boxes from it
final Rectangle rect = new Rectangle(pTMXTile.getTileX()+10, pTMXTile.getTileY(),14, 14);
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
PhysicsFactory.createBoxBody(mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
rect.setVisible(false);
mScene.attachChild(rect);
}祝你玩得开心!
发布于 2013-02-19 07:51:23
我相信您所问的是如何实现冲突处理。需要说明的是:碰撞检测是您确定某个对象与其他对象发生碰撞(重叠)的步骤。碰撞处理就是你,比如说,移动其中一个东西,让它不再重叠。在这种情况下,我假设我们已经通过了冲突检测,并继续进行冲突处理,因为您使用的是名为"onTMXTileWithPropertiesCreated“的方法,我猜这意味着玩家在这样的磁贴上。这就是我们的想法,非常简单:

由于玩家(或其他精灵)的移动,当您检测到精灵与您希望无法通过的精灵发生碰撞时--用您的术语来说是“真实的”,您需要将精灵向后移动一段距离,以防止它重叠。
用矩形来做这件事非常简单。对其他形状做这件事会变得有点复杂。因为您使用的是TMX平铺映射,所以现在可能可以使用矩形。这是一个使用矩形的基本示例。
public boolean adjustForObstacle(Rect obstacle) {
if (!obstacle.intersect(this.getCollisionRect())) return false;
// There's an intersection. We need to adjust now.
// Due to the way intersect() works, obstacle now represents the
// intersection rectangle.
if (obstacle.width() < obstacle.height()) {
// The intersection is smaller left/right so we'll push accordingly.
if (this.getCollisionRect().left < obstacle.left) {
// push left until clear.
this.setX(this.getX() - obstacle.width());
} else {
// push right until clear.
this.setX(this.getX() + obstacle.width());
}
} else {
if (this.getCollisionRect().top < obstacle.top) {
// push up until clear.
this.setY(this.getY() - obstacle.height());
} else {
// push down until clear.
this.setY(this.getY() + obstacle.height());
}
}
return true;
}这样做就是计算重叠的矩形,并沿着重叠的最小维度移动精灵,使其不再重叠。由于您使用的是AndEngine,因此可以使用IShape中的collidesWith()方法,该方法比上面的方法更巧妙地检测冲突。
https://stackoverflow.com/questions/14940361
复制相似问题