将字符串传递给NSTimer的操作方法

时间:2014-08-20 16:41:19

标签: objective-c cocoa nstimer

我正在尝试在选择器上使用带有number参数的addCloud:方法,但是我无法为图像名称接收正确的输出。

正在加载图片资源:“cloud< __ NSCFTimer:0x170162400> .png”

- (void)level:(float)interval sprite:(NSString *)charSprite
{

    if ([charSprite isEqualToString:@"1"]) {

        [NSTimer scheduledTimerWithTimeInterval:k1 target:self selector:@selector(addCloud:) userInfo:charSprite repeats:YES];


    }

}

- (void)addCloud:(NSString *)nnumber
{
   NSString *nvalue = [NSString stringWithFormat:@"cloud%@.png",nnumber];
    _cloud = [SKSpriteNode spriteNodeWithImageNamed:nvalue];
    [_cloud setScale:0.4];

    _cloud.position = CGPointMake(self.frame.size.width + _cloud.size.width/2, actualY);
    [self addChild:_cloud];
}

1 个答案:

答案 0 :(得分:1)

计时器回调的签名应为-(void)addCloud:(NSTimer *)timer。定时器回调不直接接收提供给userInfo参数的值;他们总是收到NSTimer引用。您可以向计时器询问其userInfo属性,然后您可以将其转换为您正在寻找的NSString

NSString *spriteIndex = timer.userInfo;
NSString *spriteName = [NSString stringWithFormat:@"cloud%@.png", spriteIndex];