ghazi_jaffary ghazi_jaffary - 6 months ago 68
Objective-C Question

Create 2d ripple effect with sprite kit

How can i create a 2D ripple effect on water with sprite kit.

Like i have a scene which include 2D still water and when tapped on that, small circles appears scale and disappear say 5-6 circles scaling parallel giving the feeling of ripples Not actualy doing anything to water just creating images on it.

I was wondering what could be the best way to sole this problem. How can i achieve something like that. Any ideas?

Answer

In node in which you wish to create ripple just do something like this

    NSTimeInterval singleRippleDuration = 1.0f;
    CGFloat ripleEndScale = 3.0f;
    NSTimeInterval timeBetweenRipples = 0.3f;
    NSUInteger numberOfRipples = 5;
    SKAction* scaleUpAction = [SKAction scaleTo:ripleEndScale duration:singleRippleDuration];
    SKAction* fadeOutAction = [SKAction fadeOutWithDuration:singleRippleDuration];

    SKAction* rippleAction = [SKAction group:@[scaleUpAction,fadeOutAction]];

    SKAction* createRipple = [SKAction runBlock:^{
        //Create your ripple node somehow (SKShapeNode or SKSprite will do)
        //set it to a desired position
        [rippleNode setPosition:desiredPosition];
        //Set scale to 0 so it scales from point
        [rippleNode setScale:0.0f];
        [rippleNode runAction:createRipple];
        [self addChild:rippleNode];
    }];

    SKAction* wait = [SKAction waitForDuration:timeBetweenRipples];

    [self runAction:[SKAction repeatAction:[SKAction sequence:@[createRipple,wait]] count:numberOfRipples]];
Comments