在GameScene.m中添加matchRun的实现:
-(void)matchRun{
CCLOG(@"%@ invoke!",NSStringFromSelector(_cmd));
//如果比赛还未结束,啥都不做直接退出.
if (_matching) {
return;
}
//重置比赛设置
[self matchReset];
_matching = YES;
for (Player *player in _players) {
CCTime duration = (CCRANDOM_0_1() * 500.0/100) + 5.0;
CCActionMoveBy *moveBy = [CCActionMoveBy actionWithDuration:duration position:ccp(0.9f, 0)];
CCActionJumpBy *jump = [CCActionJumpBy actionWithDuration:duration position:ccp(0, 0) height:0.05 jumps:20];
CCActionCallBlock *blk = [CCActionCallBlock actionWithBlock:^{
_finishedCount++;
[player endMatch];
if (_finishedCount == 1) {
_bestElapsedTime = player.elapsedTime;
}
//如果所有选手都完成比赛则复位比赛参数
if (_finishedCount == PlayerCount) {
_finishedCount = 0;
_matching = NO;
}
}];
CCActionSequence *seq = [CCActionSequence actionWithArray:@[moveBy,blk]];
[player runAction:seq];
[player runAction:jump];
[player startMatch];
}
}
代码比较多,下面简单讲解一下.首先如果前面的比赛未结束则直接退出方法.重置比赛参数.
创建2个动作,一个是玩家从起点到终点的移动动作.为了使得比赛过程活泼一点,还是用了另一个动作,模拟玩家跳跃着移动 ;)
因为要在玩家到达终点时触发回调,所以这里还需要一个回调action,在回调中完成:确定玩家名次,执行玩家到终点时的方法(下面自会道来),记录最好成绩以及复位比赛参数等功能.
然后用一个序列动作串联移动好回调动作,最后同时执行序列和跳跃动作.
打开Player.h文件添加一个新的属性:
@property (nonatomic,assign) NSTimeInterval elapsedTime;
最后打开Player.m文件,补全matchRun所需的方法:
-(void)startMatch{
_now = [NSDate new];
self.elapsedTime = 0;
}
-(void)endMatch{
self.elapsedTime = -1*[_now timeIntervalSinceNow];
}
执行效果如下:
时间: 2024-11-08 22:16:05