0

I have a ship and I want to make it to shoot every 1 sec. So here's what I did,

@property (nonatomic, assign) float lastDamageTime;

and in the update method,

if (CACurrentMediaTime - self.lastDamageTime > 1)
{
    self.lastDamageTime = CACurrentMediaTime;
    [self shoot];
}

But the problem is, say I pause the game right after the ship shoot a bullet, and then 1 sec later, I resume the game, the if statement pass the check, and the ship will shoot another bullet immediately. And that's not what I want.

So, what can I do to make sure the ship fire bullets each sec weather I pause the game or not? Should I use CCTimer instead?

Thank you in advance for your answer.

4

2 回答 2

0

您可以尝试将拍摄安排为一个CCRepeatForever动作,以便 cocos2d 为您处理暂停。它看起来像这样(假设您在 custom 中ShipSprite)。-

CCDelayTime *delay = [CCDelayTime actionWithDuration:1.0];
CCCallFunc *funcShoot = [CCCallFunc actionWithTarget:self selector:@selector(shoot)];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:[CCSequence actionOne:delay two:funcShoot]];
[self runAction:repeat];
于 2013-04-18T07:39:13.700 回答
0

而不是 CCRepeatForever 你可以尝试这样的事情

[self schedule:@selector(shoot) interval:1];

在您的暂停代码中取消安排拍摄

[self unschedule:@selector(shoot)];

在您的代码中检测到敌人的呼叫

[self unschedule:@selector(shoot)];
[self performSelector:@selector(shoot)];
[self schedule:@selector(shoot) interval:1];

它会立即拍摄,但是它只会拍摄一次,然后以 1 秒的间隔重新启动等等......所以如果你需要改变它,你可以调整时间

于 2013-04-18T23:22:27.350 回答