我正在尝试使用 cocos2d 和 Tiled(用于地图)为 iphone 制作平台游戏。我在网上看到的所有教程都使用 Tiled 中的图层来进行碰撞检测。我想使用对象来做到这一点......不是图层。使用对象,您可以创建自定义形状,为游戏提供更好的“现实”。举个例子说明我的意思:
我将地面绘制为背景,并在顶部创建了一个对象层。我想检测玩家与它的碰撞,而不是背景图块。
现在使用那里最著名的教程:http : //www.raywenderlich.com/15230/how-to-make-a-platform-game-like-super-mario-brothers-part-1 我正在尝试重写 checkForAndResolveCollisions方法来检查对象的碰撞。问题是在 Tiled 中,坐标系与 cocos2d 不同。Tiled 从左上角开始,cocos2d 从左下角开始......不仅如此......我注意到 Tiled 中对象属性的宽度和高度(可能)与 iPhone 设备中的对象属性不对应。上面的矩形具有以下属性:
它的 w/h 为 480/128 平铺(对于视网膜设备),这意味着如果我这样保持它们,它在地图内可能很大。我的猜测是我必须将其除以 2。到目前为止,我得到了这个:
-(void)checkForAndResolveObjCollisions:(Player *)p {
CCTiledMapObjectGroup *objectGroup = [map objectGroupNamed:@"Collision"];
NSArray* tiles = [objectGroup objects];
CGFloat x, y, wobj, hobj;
for (NSDictionary *dic in tiles) {
CGRect pRect = [p collisionBoundingBox]; //3
x = [[dic valueForKey:@"x"] floatValue];
y = [[dic valueForKey:@"y"] floatValue];
wobj = [[dic valueForKey:@"width"] floatValue];
hobj = [[dic valueForKey:@"height"] floatValue];
CGPoint position = CGPointMake(x, y);
CGPoint objPos = [self tileForPosition:position];
CGRect tileRect = CGRectMake(objPos.x, objPos.y, wobj/2, hobj/2);
if (CGRectIntersectsRect(pRect, tileRect)) {
CCLOG(@"INTERSECT");
CGRect intersection = CGRectIntersection(pRect, tileRect);
NSUInteger tileIndx = [tiles indexOfAccessibilityElement:dic];
if (tileIndx == 0) {
//tile is directly below player
p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + intersection.size.height);
p.velocity = ccp(p.velocity.x, 0.0);
p.onGround = YES;
} else if (tileIndx == 1) {
//tile is directly above player
p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y - intersection.size.height);
p.velocity = ccp(p.velocity.x, 0.0);
} else if (tileIndx == 2) {
//tile is left of player
p.desiredPosition = ccp(p.desiredPosition.x + intersection.size.width, p.desiredPosition.y);
} else if (tileIndx == 3) {
//tile is right of player
p.desiredPosition = ccp(p.desiredPosition.x - intersection.size.width, p.desiredPosition.y);
} else {
if (intersection.size.width > intersection.size.height) {
//tile is diagonal, but resolving collision vertially
p.velocity = ccp(p.velocity.x, 0.0);
float resolutionHeight;
if (tileIndx > 5) {
resolutionHeight = -intersection.size.height;
p.onGround = YES;
} else {
resolutionHeight = intersection.size.height;
}
p.desiredPosition = ccp(p.desiredPosition.x, p.desiredPosition.y + resolutionHeight );
} else {
float resolutionWidth;
if (tileIndx == 6 || tileIndx == 4) {
resolutionWidth = intersection.size.width;
} else {
resolutionWidth = -intersection.size.width;
}
p.desiredPosition = ccp(p.desiredPosition.x + resolutionWidth , p.desiredPosition.y);
}
}
}
// }
}
p.position = p.desiredPosition; //8
}
- (CGPoint)tileForPosition:(CGPoint)p
{
NSInteger x = (NSInteger)(p.x / map.tileSize.width);
NSInteger y = (NSInteger)(((map.mapSize.height * map.tileSize.width) - p.y) / map.tileSize.width);
return ccp(x, y);
}
我正在获取对象 x,y,w,h 并尝试将它们转换为 cocos2d 尺寸和大小。
以上翻译为:
Dimens: 480.000000, 128.000000
Coord: 0.000000, 40.000000
基本上是一团糟。而且它不工作......根本。玩家刚刚跌倒。我很惊讶以前没有人根据对象进行过碰撞检测……除非我错了。
有谁知道这是否可以完成或如何完成?有点像他在这里做什么:https ://www.youtube.com/watch?feature=player_detailpage&v=2_KB4tOTH6w#t=30
对不起,很长的帖子。感谢您提前提供任何答案。