user2083920
user2083920

Reputation: 35

How to delay an action inside the INIT in XCODE?

BELOW INIT

yellowbg = [CCSprite spriteWithFile:@"yellowxlixk.png"];
[yellowbg setPosition:ccp(509, 671.75)];
      yellowbg.scale = .75;
[self addChild:yellowbg z: 1];

How would I go about delaying this object? I am very new to this, please be basic in your explanation.

Upvotes: 0

Views: 767

Answers (1)

Dhruvik
Dhruvik

Reputation: 982

if you want to add this object on the layer after 2 or 3 sec, then you can use this one..

[self performSelector:@selector(addImageToLayer) withObject:nil afterDelay:2.0]; // specify delay time

Here is your function :

-(void) addImageToLayer
{
   yellowbg = [CCSprite spriteWithFile:@"yellowxlixk.png"];
   [yellowbg setPosition:ccp(509, 671.75)];
   yellowbg.scale = .75;
   [self addChild:yellowbg z: 1];
}

another option is, you can use this one also :

[self schedule:@selector(addImageToLayer) interval:2];

function : but in the function you have to stop scheduler to calling this method., by the above code of line it call function after every 2 seconds., that's why you have to stop that.

-(void) addImageToLayer
{
   yellowbg = [CCSprite spriteWithFile:@"yellowxlixk.png"];
   [yellowbg setPosition:ccp(509, 671.75)];
   yellowbg.scale = .75;
   [self addChild:yellowbg z: 1];
   [self unschedule:@selector(addImageToLayer)];  //to stop scheduler to calling function repeatedly
}

You can use either of this ways to delaying. hope this helps..

Upvotes: 2

Related Questions