在Tiled tile属性上添加CCSprite

时间:2013-05-09 18:42:55

标签: cocos2d-iphone tile

我正在尝试在玩家旁边添加一个Sprite,但只有当玩家旁边的Tile不是Wall时。我知道Tiled瓷砖工作正常,因为他们在这种方法中工作:

CGPoint p = CGPointMake(tileCoord.x, tileCoord.y - 1);
if ([self isValidTileCoord:p] && ![self isWallAtTileCoord:p]) {
    [tmp addObject:[NSValue valueWithCGPoint:p]];
    t = YES;

我正在检查Tiled与这两种方法的协调:

-(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");
}

我想要做的是只在那里没有Circle1磁贴的情况下将Wall精灵添加到播放器的左侧。问题是这个代码总是检测到错误,无论是否有墙。你们中的任何人都明白为什么它没有正确检测到墙壁以及我如何解决它?

2 个答案:

答案 0 :(得分:2)

去年我也在视网膜模式中观察到同样的问题。它在非视网膜中起作用,但在视网膜模式下未检测到。

所以最后用自定义字典检查边缘图块。整数比较比字符串比较占用更少的CPU时间。所以用枚举替换它。

只有在没有其他简单方法可行的情况下才能采用这种方法。

我使用此代码查找在meta属性中设置的tile的边缘。

typedef enum GRID_TYPE{
    kGrideType_Normal = 4001,
    kGrideType_Collidable,
    kGrideType_Collectable,      
    kGrideType_Sea,
    kGrideType_Edge,
    kGrideType_enemy,
    kGrideType_gold,

}GridType;

//初始化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);

}

答案 1 :(得分:1)

您应该检查您的瓷砖地图是否具有正确的图层设置。我曾经犯了一次错误,很容易忘记