Question

I stumbled upon a problem and I can't find the answer for it. I am working with the SK template from Xcode to create an iOS game. I am a beginner, so bear with me.

Basically I have this code:

SKAction *releaseBubbles = [SKAction sequence:@[
                                                    [SKAction performSelector:@selector(createBubbleNode)onTarget:self],
                                                    [SKAction waitForDuration:speed]]];
[self runAction: [SKAction repeatAction:releaseBubbles
                                      count:300]];

which executes in

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

I change the level to my game in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { and when I change the level it should also change that speed parameter. Of course, this doesn't work because I believe that my action is starting when the scene is initialised and I never get to switch the parameter.

What I need to do is populate the screen continuously with bubbles appearing at a certain pace (relative to the level).

I really have no clue how to fix this, because it seems to me like I need to stop and restart the action sequence somehow...

Looking forward to your valuable input.

Was it helpful?

Solution

To continuously populate the screen with bubbles you can use the update: method of your SKScene. Here is how to do it.

First, add a property that will store a date when you last added a bubble.

@property(nonatomic, strong) NSDate *lastBubbleCreationDate;

Then, change your update: method to:

-(void)update:(CFTimeInterval)currentTime
{
    // Create new bubble every 5s.
    if (ABS([_lastBubbleCreationDate timeIntervalSinceNow]) > 5)
    {
        [self createBubbleNode];
    }
}

Finally, in your createBubbleNode method you have to store the time when you created last bubble:

-(void)createBubbleNode
{
    // Your code here

    // Set the date to now.
    _lastBubbleCreationDate = [NSDate date];
}

You also need to call createBubbleNode to set the initial value of the _lastBubbleCreationDate. You can do this in didMoveToView: method. Just add this method to your scene implementation:

- (void)didMoveToView:(SKView *)view
{    
    // Creates first bubble and sets the initial value of the _lastBubbleCreationDate
    [self createBubbleNode];
}

In next levels you can just change the 5s value to create bubbles more often which will make the game more difficult.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top