在时间间隔ios之后更改整数值

时间:2014-01-30 02:59:27

标签: ios objective-c

我正在制作一部带有能量的游戏。我有3次加电。当通电激活时,我使用整数来识别它是哪一个,1,2或3,0表示没有有效的通电。一旦我激活了一个通电,我希望它在一段时间间隔后过期,比如10秒。如何将标识符整数重置为0?

例如,一次通电加速了船舶。这是我的更新方法。

-(void) update:(ccTime)dt
{
if (powerupIdentifier == 0)
{
    shipSpeed = 100;
}
if (powerupIdentifier == 1)
{
    shipSpeed = 200;
}


CCArray* touches = [KKInput sharedInput].touches;    
if ([touches count] == 1)
{
    //MAKES SHIP MOVE TO TAP LOCATION
    KKInput * input = [KKInput sharedInput];
    CGPoint tap = [input locationOfAnyTouchInPhase:KKTouchPhaseBegan];
    ship.position = ccp( ship.position.x, ship.position.y);
    if (tap.x != 0 && tap.y != 0)
    {
        [ship stopAllActions]; // Nullifies previous actions
        int addedx = tap.x - ship.position.x;
        int addedy = tap.y - ship.position.y;
        int squaredx = pow(addedx, 2);
        int squaredy = pow(addedy, 2);
        int addedSquares = squaredx + squaredy;
        int distance = pow(addedSquares, 0.5);
        [ship runAction: [CCMoveTo actionWithDuration:distance/shipSpeed position:tap]];//makes ship move at a constant speed

    }
}

}

1 个答案:

答案 0 :(得分:4)

首先,使用enum而不是int

typedef NS_ENUM(unsigned short, PowerUp) {
    PowerUp_Level0,
    PowerUp_Level1,
    PowerUp_Level2,
    PowerUp_Level3
};

Enum更具可读性,并且比随机整数更能自我记录。

现在,我们说有一个属性:

@property (nonatomic, assign) PowerUp powerUp;

我们可以编写一种重置电源的方法:

- (void)resetPowerUp {
    self.powerUp = PowerUp_Level0;
}

现在,当我们将它设置为某个非零值并需要重置它(10秒后)时,它就像这样简单:

self.powerUp = PowerUp_Level2;
[self performSelector:@selector(resetPowerUp) withObject:nil afterDelay:10.0f];