Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(四)

时间:2023-12-22 19:29:32

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.

如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;)


现在打开MainScene.m文件,首先设置实例变量:

@implementation MainScene{
    CGSize _viewSize;

    CatSprite *_cat;

    CCTiledMap *_tileMap;
    CCTiledMapLayer *_bgLayer;
    CCTiledMapLayer *_objectLayer;
    //在cocos2Dv3中没有CCSpriteBatchNode类,而CCNode已经做了优化,所以用CCNode代替.
    CCNode *_batchNode;
}

然后将帮助方法添加进去,里面有不少要修改的地方,大家可以对比原代码和我转换后方法中的区别:

-(BOOL)isValidTileCoord:(CGPoint)tileCoord {
    if (tileCoord.x < 0 || tileCoord.y < 0 ||
        tileCoord.x >= _tileMap.mapSize.width ||
        tileCoord.y >= _tileMap.mapSize.height) {
        return FALSE;
    } else {
        return TRUE;
    }
}

-(BOOL)isProp:(NSString*)prop atTileCoord:(CGPoint)tileCoord
                                                        forLayer:(CCTiledMapLayer *)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];
}

-(BOOL)isBoneAtTileCoord:(CGPoint)tileCoord {
    return [self isProp:@"Bone" atTileCoord:tileCoord forLayer:_objectLayer];
}

-(BOOL)isDogAtTileCoord:(CGPoint)tileCoord {
    return [self isProp:@"Dog" atTileCoord:tileCoord forLayer:_objectLayer];
}

-(BOOL)isExitAtTileCoord:(CGPoint)tileCoord {
    return [self isProp:@"Exit" atTileCoord:tileCoord forLayer:_objectLayer];
}

-(void)removeObjectAtTileCoord:(CGPoint)tileCoord {
    [_objectLayer removeTileAt:tileCoord];
}

- (CGPoint)tileCoordForPosition:(CGPoint)position {
    int x = position.x / _tileMap.tileSize.width;
    int y = ((_tileMap.mapSize.height * _tileMap.tileSize.height) - position.y) / _tileMap.tileSize.height;
    return ccp(x, y);
}

-(void)setViewpointCenter:(CGPoint) position {
    int x = MAX(position.x, _viewSize.width / 2);
    int y = MAX(position.y, _viewSize.height / 2);
    x = MIN(x, (_tileMap.mapSize.width * _tileMap.tileSize.width)
            - _viewSize.width / 2);
    y = MIN(y, (_tileMap.mapSize.height * _tileMap.tileSize.height)
            - _viewSize.height/2);
    CGPoint actualPosition = ccp(x, y);

    CGPoint centerOfView = ccp(_viewSize.width/2, _viewSize.height/2);
    CGPoint viewPoint = ccpSub(centerOfView, actualPosition);

    _tileMap.position = viewPoint;

}

- (CGPoint)positionForTileCoord:(CGPoint)tileCoord {
    int x = (tileCoord.x * _tileMap.tileSize.width) + _tileMap.tileSize.width/2;
    int y = (_tileMap.mapSize.height * _tileMap.tileSize.height) - (tileCoord.y * _tileMap.tileSize.height) - _tileMap.tileSize.height/2;
    return ccp(x, y);
}

-(void)update:(CCTime)delta{
    [self setViewpointCenter:_cat.position];
}

接下来是比较重要的touchBegan和初始化方法.这里我将原来在init中的代码基本上都放到了didLoadFromCCB方法中,道理是一样的:

-(void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
    CGPoint location = [[CCDirector sharedDirector] convertTouchToGL:touch];
    location = [_tileMap convertToNodeSpace:location];
    [_cat moveToward:location];
}

-(void)didLoadFromCCB{
    _viewSize = [CCDirector sharedDirector].viewSize;

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"CatMaze.plist"];

    _tileMap = [CCTiledMap tiledMapWithFile:@"CatMaze.tmx"];
    [self addChild:_tileMap];

    CGPoint spawnTileCoord = ccp(24,0);
    CGPoint spawnPos = [self positionForTileCoord:spawnTileCoord];
    [self setViewpointCenter:spawnPos];

    _bgLayer = [_tileMap layerNamed:@"Background"];
    _objectLayer = [_tileMap layerNamed:@"Objects"];
    _batchNode = [CCNode node];
    [_tileMap addChild:_batchNode];
    _cat = [[CatSprite alloc] initWithMainScene:self];
    _cat.position = spawnPos;
    [_batchNode addChild:_cat];

    self.userInteractionEnabled = YES;
}

注意Cocos2D v3.4中的触碰开启只需要简单的一句:

self.userInteractionEnabled = YES;

原来的那么多都可以省略了.现在编译运行游戏,效果如下图所示:

Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(四)

可以看到现在cat可以移动到屏幕最左边附近了,所以我们没有白费劲.现在的cat可以穿墙入室,所以我们需要让它与地图障碍物发生碰撞,这也是下一篇的内容 ;)