cocos2d运行顺序在不同的目标 [英] cocos2d running sequences in order on different targets

查看:113
本文介绍了cocos2d运行顺序在不同的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个基本的游戏场景:



有些单位靠近敌人单位,射弹,然后调整敌人单位的生活。我的问题是,我不知道如何安排这三个事件一个接一个运行。如果所有这些动作都是在同一个目标上完成的,那么这将是非常容易的,但有两个不同的目标。



最好的方法是什么? / p>

代码如下所示:

  Unit * unit = [self getActiveUnit ]; 
Unit * enemy = [self getEnemyInRange:unit];

CGpoint A = unit.sprite.position;
CGPoint B = [self getPositionClose:enemy for:unit];

CCSequence * unitMove = [self generateUnitMoveFrom:A to:B];

Projectile * proj = [self generateProjectile];
CCSequence * projMove = [self generateProjMoveFrom:A to:B];

CCSequence * attackDone = [self generateAttackDoneFor:unit enemy:enemy];

//这是我不知道如何做的部分
//按顺序执行这些顺序,而不是同时执行
[unit.sprite runAction :unitMove];
[proj.sprite runAction:projMove];
[proj.sprite runAction:removeSprite];
[self runAction:attackDone];

这是最好的方法是什么?即使使用CCActionManager,它仍然看起来相当复杂,因为我想我将添加一个额外的回调之间所有这些动作恢复计划的行动下一个目标。



任何想法?



谢谢!

解决方案

cccc> cccc code>



actionWithTarget:unit.sprite selector:@selector(runAction :) object:unitMove],
[CCCallFuncO actionWithTarget:proj.sprite selector:@selector(runAction :) object:projMove],
[CCCallFuncO actionWithTarget: proj.sprite selector:@selector(runAction :) object:removeSprite],
[CCCallFunc actionWithTarget:self selector:@selector(attackDone)],
nil]];


So, i have a basic game scenerario:

Some unit moves close to an enemy unit, shoots projectile and then enemy unit's life is adjusted. My problem is that I am not sure how to schedule these three events to run one after another. If all these actions were done on the same target, then this would be super easy, but there are two different targets.

What would be best approach to do this?

Code looks like this:

Unit* unit = [self getActiveUnit];
Unit* enemy = [self getEnemyInRange:unit];

CGpoint A = unit.sprite.position;
CGPoint B = [self getPositionClose:enemy for:unit];

CCSequence* unitMove = [self generateUnitMoveFrom:A to:B];

Projectile* proj = [self generateProjectile];
CCSequence* projMove = [self generateProjMoveFrom:A to:B];

CCSequence* attackDone = [self generateAttackDoneFor:unit enemy:enemy];

// This is the part that i don't know how to do
// Execute these in order and sequentially, not at the same time
[unit.sprite runAction:unitMove];
[proj.sprite runAction:projMove];
[proj.sprite runAction:removeSprite];
[self runAction:attackDone];

What is the best approach to do this? Even using CCActionManager it still seems fairly complicated because i think i would have to add an extra call back between all these actions to resume scheduled actions for the next target.

Any ideas?

Thanks!

解决方案

I would attempt to use CCSequence

[self runAction:[CCSequence actions:
  [CCCallFuncO actionWithTarget:unit.sprite selector:@selector(runAction:) object:unitMove],
  [CCCallFuncO actionWithTarget:proj.sprite selector:@selector(runAction:) object:projMove],
  [CCCallFuncO actionWithTarget:proj.sprite selector:@selector(runAction:) object:removeSprite],
  [CCCallFunc actionWithTarget:self selector:@selector(attackDone)],
                     nil]];

这篇关于cocos2d运行顺序在不同的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆