本文介绍了每5秒增加NSnumber - Cocos2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Cocos2d中每隔5秒增加一个 NSNumber ,但由于某种原因它不工作(可能是明显的错误)。我使用更新方法,但我认为这可能是错误的地方。目前只有+1次。我需要它每隔5秒钟这样做:

   - (void)update:(CCTime)delta 
{
//保存字符串:
NSNumber * anInt = [NSNumber numberWithInt:500];

NSNumber * bNumber = [NSNumber numberWithInt:[anInt intValue] + 1];

NSLog(@Update Number%@,bNumber);

}


解决方案

每5秒运行一次的方法是:



创建存储您的号码和计时器的属性:

  @property(nonatomic,assign)int bNumber; 
@property(nonatomic,strong)NSTimer * timer;

然后将数字初始化为500(根据您的示例,假设您希望它从500开始)并创建计时器:

   - (instanceType)init 
{
self = [super init];

if(self)
{
self.bNumber = 500;

self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self
selector:@selector(incrementScore :)
userInfo:nil
重复:是];
}
}

然后创建一个执行增量的方法:

   - (void)incrementScore:(NSTimer *)timer 
{
self.bNumber ++;
NSLog(@Number =%d,self.bNumber);
}

完成后不要忘记使计时器无效:

   - (void)dealloc 
{
//如果不使用arc不要忘记super dealloc
[super dealloc];

[self.timer invalidate];
self.timer = nil;
}

这是一种方法。如果要在cocos2d中使用update方法,那么您需要跟踪累积的增量。一旦该值在5秒内达到或超过了总毫秒数,则将数字加1。 Delta是自上次更新以来经过的毫秒数。所以例如属性将是:

  @property(nonatomic,assign)int bNumber; 
@property(nonatomic,assign)CCTime totalDelta;

然后在更新中,您将执行以下操作(每秒有1000毫秒):

   - (void)update:(CCTime)delta 
{
const CCTime FiveSeconds = 5000.0f;
self.totalDelta + = delta;

if(self.totalDelta> = FiveSeconds)
{
self.totalDelta = 0;
self.bNumber ++;

NSLog(@Number =%d,self.bNumber);
}
}

我希望这有帮助。你想要做的是很简单,所以我建议刷一下Obj-C编程,然后才能开始制作游戏。


I am trying to increment a NSNumber every 5 seconds by 1 in Cocos2d but for some reason it isn't working (probably obvious mistake). I am using the update method but I think this may be the wrong place. Currently it is only adding +1 once. I need it to do this every 5 seconds constantly:

-(void) update: (CCTime) delta
{
    // Save a string:
    NSNumber *anInt = [NSNumber numberWithInt:500];

    NSNumber *bNumber = [NSNumber numberWithInt:[anInt intValue] + 1];

    NSLog(@"Update Number%@",bNumber);

}
解决方案

An easy way to run something every 5 seconds would be to:

Create a property storing your number and a timer:

@property (nonatomic, assign) int bNumber;
@property (nonatomic, strong) NSTimer* timer;

Then initialize the number to 500 (I assume based on your example you want it to start at 500) and create the timer:

- (instanceType)init
{
    self = [super init];

    if (self)
    {
        self.bNumber = 500;

        self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0f
                                              target:self
                                            selector:@selector(incrementScore:)
                                            userInfo:nil
                                             repeats:YES];
    }
}

Then create a method that does the increment:

- (void)incrementScore:(NSTimer *)timer
{
    self.bNumber++;
    NSLog(@"Number = %d", self.bNumber);
}

Don't forget to invalidate the timer when you are done:

- (void)dealloc
{
    // If not using arc don't forget super dealloc
    [super dealloc];

    [self.timer invalidate];
    self.timer = nil;
}

That is one way. If you want to use the update method in cocos2d then you need to be keeping track of the accumulated delta. Once that value has reached or exceeded the total number of milliseconds in 5 seconds, then you add 1 to the number. Delta is the number of milliseconds that have passed since the last update. So for example the properties will be:

@property (nonatomic, assign) int bNumber;
@property (nonatomic, assign) CCTime totalDelta;

Then in update you would do the following (there are 1000 milliseconds in a second):

- (void)update:(CCTime)delta
{
    const CCTime FiveSeconds = 5000.0f;
    self.totalDelta += delta;

    if (self.totalDelta >= FiveSeconds)
    {
        self.totalDelta = 0;
        self.bNumber++;

        NSLog(@"Number = %d", self.bNumber);
    }
}

I hope this helped. What you are trying to do is pretty simply so I recommend brushing up on Obj-C programming before jumping into making games.

这篇关于每5秒增加NSnumber - Cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:24