-1

我正在尝试在玩家旁边添加一个 Sprite,但前提是玩家旁边的瓷砖不是墙。我知道 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];
}

(感谢雷文德利希)

我添加精灵的代码是

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瓷砖的情况下将精灵添加到播放器的左侧。问题是这段代码总是检测到错误,无论那里是否有墙。你们中的任何人都明白为什么它不能正确检测到墙壁以及如何修复它吗?

4

2 回答 2

2

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

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

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

我使用此代码来查找在元属性中设置的图块的边缘。

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

}GridType;

//初始化tilemap和grid类型

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);

}
于 2013-05-15T16:48:58.570 回答
1

您应该检查您的瓦片地图是否具有正确的图层设置。我犯了一次这个错误,很容易忘记

于 2013-05-16T22:34:58.283 回答