Cocos2D两个方法的重构一例

前端之家收集整理的这篇文章主要介绍了Cocos2D两个方法的重构一例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)


在RPG游戏项目的GameSpace类中原来有一个方法:

-(instancetype)initWithGameScene:(GameScene *)gameScene mapName:(NSString*)mapName;

功能主要为用指定的地图初始化游戏空间布局,后来由于使用方便的原因,增加了一个类似的方法:

-(instancetype)initWithGameScene:(GameScene *)gameScene mapName:(NSString *)mapName spawnAtTilePos:(CGPoint)tilePoint;

方法比原来的方法仅仅多了一个参数,用来初始化player出现在地图场景中的瓦块位置.

这样就出现了功能类似的2个方法,这势必造成代码的重复,会对将来的代码修改带来非常坏的影响,所以今天我们就想办法对其重构.

首先我们将第一个方法前面的几行代码提炼出去,形成一个新的方法:

-(void)instancePreInit:(GameScene*)gameScene mapName:(NSString*)mapName{ _viewSize = [CCDirector sharedDirector].viewSize;

    _gameScene = gameScene;
    _tiledMap = [CCTiledMap tiledMapWithFile:mapName];

    _tiledMap.anchorPoint = ccp(0,0);
    [self addChild:_tiledMap];

    _mh = [[MapHelper alloc]initWithGameScene:gameScene andTiledMap:_tiledMap];

    CCTiledMapLayer *barrierLayer = [_tiledMap layerNamed:@"BarrierLayer"];
    barrierLayer.visible = NO;

    _objGroup = [_tiledMap objectGroupNamed:@"Objects"];

    _gd = [GameData sharedInstance];

    _bgLayer = [_tiledMap layerNamed:@"BGLayer"];
    NSAssert(_bgLayer,@"ERR:地图中没有_bgLayer层!!!");
}

然后在2个方法中分别用该方法替换原有的代码:

-(instancetype)initWithGameScene:(GameScene *)gameScene mapName:(NSString *)mapName
                  spawnAtTilePos:(CGPoint)tilePoint{
    self = [super init];
    if (self) {
        //原功能代码
        [self instancePreInit:gameScene mapName:mapName];
        //其他代码

另一个方法也类似,现在我们来处理后面的代码.

现在创建后续初始化方法,把以上两个方法的后半段代码抽取到其中:

-(void)instanceSufInit:(CGPoint)spawnPos{
    GameData *gd = [GameData sharedInstance];
    NSString *className = gd.players[0][@"playerName"];
    _panda = [GameCharacter gcWithName:className wihtGameScene:_gameScene];

    _panda.position = spawnPos;

    self.contentSize = [CCDirector sharedDirector].viewSize;
    [_bgLayer addChild:_panda z:50];
    [self setPlayerFaceTo];

    if (gd.players.count > 1) {
        GameCharacter *followGC = nil;
        GameCharacter *targetGC = _panda;
        NSInteger maxFollowCount = MIN(2,gd.players.count-1);
        for (int i = 1; i <= maxFollowCount; i++) {
            className = gd.players[i][@"playerName"];
            followGC = [GameCharacter gcWithName:className wihtGameScene:_gameScene];
            [_bgLayer addChild:followGC];
            [followGC follow:targetGC];
            targetGC = followGC;
        }
    }

    self.userInteractionEnabled = YES;

    _walkableTiles = [NSMutableArray array];
    [self initWalkableTiles];

    _npcArray = [NSMutableArray array];
    _interactThingAry = [NSMutableArray array];
    _followAry = [NSMutableArray array];
}

但是在第一个方法中,对瓦块坐标要做一些修正以居中在瓦块显示,但在第二个方法中不需要修正,所以先要将这点重构之然后再调用后缀方法:

CGPoint spawnPos = [self getPlayerSpawnPos];
        spawnPos = [_mh centerObjectsPos:spawnPos];
        [self instanceSufInit:spawnPos];

第二个方法重构后的代码如下:

CGPoint spawnPos = [_mh positionForTilePos:tilePoint];
[self instanceSufInit:spawnPos];

现在2个方法共享同样的前缀和后缀方法,也消除了冗余代码,我们就这样完成了本次重构.

原文链接:https://www.f2er.com/cocos2dx/340461.html

猜你在找的Cocos2d-x相关文章