【问题标题】:Objective-C get tile positions from CCTMXTiledMap (Cocos2d)Objective-C 从 CCTMXTiledMap (Cocos2d) 获取瓦片位置
【发布时间】:2012-04-13 18:29:24
【问题描述】:

我正在尝试创建一个简单的循环来遍历 CCTMXTiledMap 中 CCTMXLayer 中的所有图块。我想要实现的只是打印出每个图块的位置,但对于我的生活,我无法弄清楚如何循环通过图块或如何访问它们的容器。我是 Objective-c 新手,所以请温柔:)

请停下来!

到目前为止我所拥有的:

CGSize mapSize = [_tileMap mapSize];
CCTMXLayer *backgroundLayer = [_tileMap layerNamed:@"Background"];
int h = 0;
int w = 0;
NSInteger i = 0;
for(h = 0; h < mapSize.height; h++)
{
    for(w = 0; w < mapSize.width; w++)
    {
        i = h * mapSize.width + w;
        // I tried the following but none of it worked:
        //CCArray* tiles = [backgroundLayer  children];
        //CCSprite *tile = [backgroundLayer tileAt:tileCoord];
        //CCSprite*tile = [tiles objectAtIndex:i];
        //CCNode* tile = [backgroundLayer children getChildByTag:i];
        CGPoint position = [tile position];
        [_debugHud addTileId:i at:position];
    }
}

【问题讨论】:

    标签: objective-c cocos2d-iphone


    【解决方案1】:

    CCSprite *tile = [backgroundLayer tileAt:tileCoord]; 应该可以正常工作,假设 tileCoord = CGPointMake(h,w); 或类似的东西。

    您确定您的CCTMXLayer *backgroundLayer 不是nil 并且是正确的层吗?

    【讨论】:

    • 老实说,我不确定,仍在尝试使用 xcode。从我看到它看起来并不空。关于“tileAt:tileCoord”的问题,坐标不是tile indecies,它是实际位置,对吧?因为我不能使用上面的 for 循环来遍历图块?
    • 坐标是平铺指示,你写的上面的循环其实很好! CCTMXTiledMap.mapSize 是地图的瓦片大小,而不是像素或点。来自 CCTMXTiledMap.h:/** the map's size property measured in tiles */ @property (nonatomic,readonly) CGSize mapSize; /** the tiles's size property measured in pixels */ @property (nonatomic,readonly) CGSize tileSize;
    • 天啊哈哈,难怪没人问这个问题,谢谢!赞赏!
    • 确保检查 CC_CONTENT_SCALE_FACTOR!您将在软管上除以 2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多