(NO.00005)iOS實現炸彈人遊戲(九):遊戲主角(二)

來源:互聯網
上載者:User

(NO.00005)iOS實現炸彈人遊戲(九):遊戲主角(二)

大熊貓豬·侯佩原創或翻譯作品.歡迎轉載,轉載請註明出處.
如果覺得寫的不好請多提意見,如果覺得不錯請多多支援點贊.謝謝! hopy ;)

上篇介紹了遊戲主角的初始化方法,下面我們一次來實現主角的其他方法,首先來看看runAnimation方法,我們使用這個方法來播放主角的動畫:

-(void)runAnimation:(CCAnimation*)animation{    if (_curAnimation == animation) {        return;    }    _curAnimation = animation;    if (_curAnimate) {        [self stopAction:_curAnimate];    }    _curAnimate = [CCActionRepeatForever actionWithAction:                   [CCActionAnimate actionWithAnimation:animation]];    [self runAction:_curAnimate];}

代碼首先檢查將要播放的動畫是否和當前現正播放的動畫相同,如果相同則直接退出.然後如果當前現正播放動畫動作,則將其停止.

接下來為需要播放的動畫建立一個永久迴圈的動作,然後運行該動作.

vcfF9snPudbO77nStfS1xLavu63SssrHyOe0yy48L3A+DQo8cD7W973HwODW0Lu509DSu7j21tjSqrXEt723qCzTprjDu7mw/MCouMPTzs+31tDL+dPQudbO77a80OjSqtXiw7TSu7j2t723qCy+zcrHQSq1xNLGtq/L47eoLrK7yuzPpEEqy+O3qLXE0KG777Dpv8nS1LW9ztK3rdLrus3UrbS0tcRBKs+1wdCyqc7E1tC528nNOjwvcD4NCjxwPtSttLQ6yOe6ztTaQ29jb3MyRNPOz7fW0Mq1z9ZBKtGwwrfL47eoKNK7KTxiciAvPg0Kt63S6zpBKtGwwrfL47eoyOvDxSjSuyk8L3A+DQo8cD64w7e9t6i+zcrHbW92ZVRvd2FyZEJ5QVN0YXK3vbeoOjwvcD4NCjxwcmUgY2xhc3M9"brush:java;">//使用A*尋路演算法移動至目標座標-(void)moveTowardByAStar:(CGPoint)targetLocation{ if (_currentStepAction) { _pendingMove = [NSValue valueWithCGPoint:targetLocation]; return; } CGPoint fromTileCoord = [_mainScene tileCoordForPosition:self.position]; CGPoint toTileCoord = [_mainScene tileCoordForPosition:targetLocation]; if (CGPointEqualToPoint(fromTileCoord, toTileCoord)) { return; } if (![_mainScene isWalkableTile:toTileCoord forRole:self]) { return; } _spOpenSteps = [NSMutableArray array]; _spClosedSteps = [NSMutableArray array]; _shortestPath = nil; [StarA insertStep:[[ShortestPathStep alloc] initWithPosition:fromTileCoord] toOpenList:_spOpenSteps]; do{ ShortestPathStep *currentStep = _spOpenSteps[0]; [_spClosedSteps addObject:currentStep]; [_spOpenSteps removeObjectAtIndex:0]; if (CGPointEqualToPoint(currentStep.position, toTileCoord)) { //如果已經找到最短路徑則完成該最短路徑的移動動作 [self constructPathAndStartAnimationFromStep:currentStep]; _spOpenSteps = nil; _spClosedSteps = nil; break; } NSArray *adjSteps = [_mainScene walkableAdjacentTilesCoordDiagonallyForTileCoord: currentStep.position forRole:self]; for (NSValue *v in adjSteps) { ShortestPathStep *step = [[ShortestPathStep alloc]initWithPosition:[v CGPointValue]]; if ([_spClosedSteps containsObject:step]) { continue; } int moveCost = [StarA costToMoveDiagonallyFromStep:currentStep toAdjacentStep:step]; NSUInteger index = [_spOpenSteps indexOfObject:step]; if (index == NSNotFound) { step.parent = currentStep; step.gScore = currentStep.gScore + moveCost; step.hScore = [StarA computeHScoreFromCoord:step.position toCoord:toTileCoord]; [StarA insertStep:step toOpenList:_spOpenSteps]; }else{//已經存在於開放列表 step = _spOpenSteps[index]; if ((currentStep.gScore + moveCost) < step.gScore) { step.gScore = currentStep.gScore + moveCost; step.parent = currentStep; [_spOpenSteps removeObjectAtIndex:index]; [StarA insertStep:step toOpenList:_spOpenSteps]; } } } }while (_spOpenSteps.count > 0);}

其中比較長,這裡就不詳細說明了,大家需要的資訊上面2個系列的博文完全涵蓋了.有一點要指出的是,因為我也才開始寫這類代碼,所以一開始對類元素的把握也不是太強,按理說這個方法是所有遊戲角色都需要的,所以應該放在它們的父類中.的確是這樣,我在另一個RPG遊戲<<熊貓之魂>>中正是這樣做的,我們有機會再敘.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.