0

I am trying to use the addCloud: method with the number argument on a selector but I am not able to receive a correct output for the image name.

loading image resource: "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];
}
jscs
  • 63,694
  • 13
  • 151
  • 195
wrynux
  • 23
  • 5
  • Please add further information than this. What exactly is your question? what is your expected output, is there an error? – Deepend Aug 20 '14 at 16:50

1 Answers1

1

The signature of your timer callback should be -(void)addCloud:(NSTimer *)timer. Timer callbacks don't receive the value supplied to the userInfo parameter directly; they always receive an NSTimer reference. You can ask the timer for its userInfo property, which you can then cast to the NSString you're looking for:

NSString *spriteIndex = timer.userInfo;
NSString *spriteName = [NSString stringWithFormat:@"cloud%@.png", spriteIndex];
warrenm
  • 31,094
  • 6
  • 92
  • 116