我正在制作一款带有 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/