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

标签 ios objective-c

我正在制作一款带有 Prop 的游戏。我有3个通电。当电源处于事件状态时,我使用一个整数来标识它是哪一个,1、2 或 3,其中 0 表示没有事件的电源。一旦我激活了一个 powerup,我希望它在一个时间间隔后过期,比如 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

    }
}

最佳答案

首先,使用 enum 而不是 int

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

Enum 比随机整数更具可读性,并且更易于 self 记录。

现在,假设我们有一个属性:

@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];

关于ios - 在时间间隔后更改整数值 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21446977/

相关文章:

Objective-c 使用必要的委托(delegate)方法添加动态元素

ios - 一行中的多个函数调用

ios - 如何将attributedText UITextView保存到数据库

ios - 使用 appium 自动化 IOS UITableview

ios - 为什么我不能以这种方式在我的 [( String, [ProtocolType] )] 中插入一个 (String, [ProtocolConformer]) 元组?

ios - 在核心数据中拥有与任何其他实体无关的实体是否可以?

objective-c - uint8_t 转二进制补码函数

objective-c - 检查复杂 JSON 响应中的空键

ios - UIScrollView 不会滚动显示更多内容

javascript - 从浏览器将约会写入 iOS 日历