Cocos2d iphone 在平铺瓷砖属性上添加CCSprite
我试图在播放器旁边添加一个精灵,但仅当播放器旁边的瓷砖不是墙时。我知道瓷砖工作正常,因为他们用这种方法完成工作:Cocos2d iphone 在平铺瓷砖属性上添加CCSprite,cocos2d-iphone,tile,Cocos2d Iphone,Tile,我试图在播放器旁边添加一个精灵,但仅当播放器旁边的瓷砖不是墙时。我知道瓷砖工作正常,因为他们用这种方法完成工作: CGPoint p = CGPointMake(tileCoord.x, tileCoord.y - 1); if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) { [tmp addObject:[NSValue valueWithCGPoint:p]]; t = YES; 我正
CGPoint p = CGPointMake(tileCoord.x, tileCoord.y - 1);
if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
[tmp addObject:[NSValue valueWithCGPoint:p]];
t = YES;
我正在使用以下两种方法检查平铺坐标:
-(BOOL)isProp:(NSString*)prop atTileCoord:(CGPoint)tileCoord forLayer:(CCTMXLayer *)layer {
if (![self isValidTileCoord:tileCoord]) return NO;
int gid = [layer tileGIDAt:tileCoord];
NSDictionary * properties = [_tileMap propertiesForGID:gid];
if (properties == nil) return NO;
return [properties objectForKey:prop] != nil;
}
-(BOOL)isWallAtTileCoord:(CGPoint)tileCoord {
return [self isProp:@"Wall" atTileCoord:tileCoord forLayer:_bgLayer];
}
(谢谢RayWenderlich)
我添加精灵的代码是
CGPoint tileCoord = ccp(_player.position.x - 24 +60, player.position.y);
CGPoint cTileCoord = [self tileCoordForPosition:tileCoord];
NSLog(@" t: %@, c: %@",
CGPointCreateDictionaryRepresentation(tileCoord),
CGPointCreateDictionaryRepresentation(cTileCoord)
);
if (![self isWallAtTileCoord:cTileCoord])
{
NSLog(@"False");
circle1.position = ccp(_player.position.x - 24 +60, _player.position.y);
[self addChild:circle1];
}
else
{
NSLog(@"True");
}
我想这样做的是,当没有
墙
瓷砖时,只在播放器的左侧添加圆圈1
精灵。问题是,无论是否有墙,该代码总是检测false。你们中有谁知道为什么它不能正确地检测到细胞壁,以及我如何修复它吗?去年,我在视网膜模式下也观察到了同样的问题。它在非视网膜模式下工作,但在视网膜模式下未检测到
所以最后使用自定义字典检查边缘平铺。整数比较比字符串比较占用更少的CPU时间。所以用enum替换了它
只有在没有其他简单方法奏效的情况下,才采用这种方法。
我使用这段代码来查找在meta属性中设置的磁贴边缘
typedef enum GRID_TYPE{
kGrideType_Normal = 4001,
kGrideType_Collidable,
kGrideType_Collectable,
kGrideType_Sea,
kGrideType_Edge,
kGrideType_enemy,
kGrideType_gold,
}GridType;
//Init tilemap和网格类型
tileMap.meta = [tileMap layerNamed:PP_TILE_META_LAYER];
[tileMap initTileAnimation];
//代码中的某个地方
CGPoint point = [self getTileCoordForPosition:position];
GridType type = [self getTileType:point];
if(type == kGrideType_Edge)
{
//touched edge tile....
}
职能:
-(GridType)getTileType:(CGPoint)pos
{
// not defined USE_COCOS2D_FOR_TILE_IDENTIFICATION
#ifdef USE_COCOS2D_FOR_TILE_IDENTIFICATION
GridType type = kGrideType_Normal;
CGPoint tileCoord = position;//[self tileCoordForPosition:position];
unsigned int tileGid = [self.meta tileGIDAt:tileCoord];
if (tileGid) {
NSDictionary *properties = [self propertiesForGID:tileGid];
if (properties)
{
NSString *collision = [properties valueForKey:TILE_PROPERTY_COLLIDABLE];
if (collision && [collision caseInsensitiveCompare:@"true"] == NSOrderedSame)
{
type = kGrideType_Collidable ;
}
NSString *collectable = [properties valueForKey:TILE_PROPERTY_COLLECTABLE];
if (collectable && [collectable caseInsensitiveCompare:@"true"] == NSOrderedSame) {
type = kGrideType_Coin ;
}
NSString *collectable = [properties valueForKey:TILE_PROPERTY_CHEST];
if (collectable && [collectable caseInsensitiveCompare:@"true"] == NSOrderedSame) {
type = kGrideType_Chest ;
}
}
}
return type;
#else
int type = [[mTileInfoDict objectForKey:[NSString stringWithFormat:@"TILE(%d,%d)", (int)pos.x,(int)pos.y]] intValue];
GridType gType = kGrideType_Normal;
if(type!=0)
gType = (GridType)type;
return (GridType)gType;
#endif
}
-(void)initTileAnimation
{
mTileInfoDict = [[NSMutableDictionary alloc] init];
//Parse all the tile in map
mBgLayer = [self layerNamed:PP_TILE_MAP_BG_LAYER];
int rowSize = self.mapSize.width ;
int colSize = self.mapSize.height ;
for(int x=0; x<rowSize; x++)
{
for (int y=0; y<colSize; y++)
{
CGPoint tileCord = ccp(x,y) ;
NSString *key = [NSString stringWithFormat:@"TILE(%d,%d)", (int)tileCord.x,(int)tileCord.y];
GridType tileType = kGrideType_Normal;
unsigned int tileGid = [self.meta tileGIDAt:tileCord];
if (tileGid)
{
NSDictionary *properties = [self propertiesForGID:tileGid];
if (properties)
{
/* Check Tile : IS SEA - TILE */
NSString *sea = [properties valueForKey:TILE_PROPERTY_SEA];
if (sea && [sea isEqualToString:@"true"])
{
tileType = kGrideType_Sea;
[mTileInfoDict setObject:[NSNumber numberWithInt:tileType] forKey:key];
continue;
}
/* Check Tile : IS Inky - TILE */
/* Check Tile : IS COLLECTABLE - COIN */
NSString *collectable = [properties valueForKey:TILE_PROPERTY_COLLECTABLE];
if (collectable && [collectable isEqualToString:@"true"])
{
tileType = kGrideType_Collectable;
[mTileInfoDict setObject:[NSNumber numberWithInt:tileType] forKey:key];
PPCoins *coinSprite = [PPCoins spriteWithSpriteFrameName:@"coin_0000.png"];
coinSprite.tag = kTagCoinSprite;
coinSprite.anchorPoint = ccp(0.5f,0.5f);
CCSprite *sprite = [mBgLayer tileAt:tileCord];
coinSprite.position = ccp(sprite.position.x+coinSprite.contentSize.width*0.5f, sprite.position.y+coinSprite.contentSize.height*0.5f) ;
[self addChild:coinSprite z:3 tag:kTagCoinSprite];
[coinSprite runAnimation];
{
coinSprite.key = key;
[mCoinDict setObject:coinSprite forKey:key];
}
continue;
}
/* Check Tile : IS COLLIDABLE - TILE */
NSString *collidable = [properties valueForKey:TILE_PROPERTY_COLLIDABLE];
if (collidable && [collidable isEqualToString:@"true"])
{
tileType = kGrideType_Collidable;
[mTileInfoDict setObject:[NSNumber numberWithInt:tileType] forKey:key];
continue;
}
/* Check Tile : IS Edge - TILE */
NSString *edge = [properties valueForKey:TILE_PROPERTY_EDGE];
if (edge && [edge isEqualToString:@"true"])
{
tileType = kGrideType_Edge;
[mTileInfoDict setObject:[NSNumber numberWithInt:tileType] forKey:key];
continue;
}
NSString *redTargetCoin = [properties valueForKey:TILE_PROPERTY_TARGET_COIN];
if (redTargetCoin && [redTargetCoin isEqualToString:@"true"])
{
tileType = kGrideType_TargetCoins;
[mTileInfoDict setObject:[NSNumber numberWithInt:tileType] forKey:key];
}
}
else
{
[mTileInfoDict setObject:[NSNumber numberWithInt:kGrideType_Normal] forKey:key];
}
}
}
}
}
- (CGPoint)getTileCoordForPosition:(CGPoint)position
{
// CGPoint nodeSpace1 = [self convertToNodeSpace:position];
// float x = floor(nodeSpace1.x / self.tileSize.width);
// float y = floor(self.mapSize.height - (nodeSpace1.y / self.tileSize.height));
//
// if( x >= TILE_IN_ROW)
// x = TILE_IN_ROW - 1;
//
// if( y >= TILE_IN_COL)
// y = TILE_IN_COL - 1;
//
// return ccp(x, y);
int maxTileCol = self.mapSize.height;// (_tileMap.contentSize.height)/TILE_SIZE;
int x = ( (position.x-self.position.x)/TILE_SIZE);
int y = maxTileCol - ( ((position.y)-self.position.y)/TILE_SIZE);
if( x >= TILE_IN_ROW)
x = TILE_IN_ROW - 1;
if( y >= TILE_IN_COL)
y = TILE_IN_COL - 1;
return ccp(x, y);
}
-(GridType)getTileType:(CGPoint)pos
{
//未定义使用COCOS2D进行标识
#ifdef使用_COCOS2D_进行_TILE_标识
GridType类型=KGridType_Normal;
CGPoint tileCoord=位置;/[自tileCoordForPosition:位置];
无符号int-tileGid=[self.meta-tileGIDAt:tilecord];
如果(tileGid){
NSDictionary*properties=[self-propertiesForGID:tileGid];
如果(属性)
{
NSString*collision=[properties-valueForKey:TILE_-PROPERTY_-collisable];
if(冲突和[collision caseInsensitiveCompare:@“true”]==SensorDeredName)
{
type=kGrideType\u可碰撞;
}
NSString*collectable=[properties valueForKey:TILE_PROPERTY_collectable];
if(可收集和[collectable caseInsensitiveCompare:@“true”]==SensorDeredName){
类型=kGrideType_硬币;
}
NSString*collectable=[properties valueForKey:TILE_PROPERTY_Cast];
if(可收集和[collectable caseInsensitiveCompare:@“true”]==SensorDeredName){
类型=kGrideType_胸部;
}
}
}
返回类型;
#否则
int type=[[mTileInfoDict objectForKey:[NSString stringWithFormat:@“TILE(%d,%d)”,(int)pos.x,(int)pos.y]]intValue];
GridType gType=kGrideType_Normal;
如果(类型!=0)
gType=(GridType)类型;
返回(GridType)gType;
#恩迪夫
}
-(无效)初始化初始化
{
mTileInfoDict=[[NSMutableDictionary alloc]init];
//解析地图中的所有平铺
mBgLayer=[self layerNamed:PP_TILE_MAP_BG_LAYER];
int rowSize=self.mapSize.width;
int colSize=self.mapSize.height;
for(int x=0;x=TILE\u IN\u COL)
//y=第1列中的瓷砖;
//
//返回ccp(x,y);
int maxTileCol=self.mapSize.height;/(\u tileMap.contentSize.height)/TILE\u SIZE;
int x=((position.x-self.position.x)/TILE_SIZE);
int y=maxTileCol-((position.y)-self.position.y)/瓷砖尺寸;
如果(x>=平铺在行中)
x=第1行中的瓷砖;
如果(y>=平铺中的平铺)
y=第1列中的瓷砖;
返回ccp(x,y);
}
您应该检查平铺贴图是否具有正确的图层设置。有一次我犯了一个错误,很容易忘记,问一个明显的问题,你是否检查过你的“墙”瓷砖是否在\bgLayer
上,并且它们的墙的确切属性设置为True?如果是,我会记录你试图添加精灵的位置(而不是瓷砖坐标位置)然后在isProp
@stenger96中记录正在测试的磁贴坐标位置,所有记录都相同。。你认为你可以发布一个链接,这样我就可以下载这个项目并自己测试它吗?如果你找到了答案,奥斯卡,把它作为一个答案发布(如果现有的答案还没有涵盖这个答案)。将“答案”工作代码发布到问题中只会混淆问题。