简体   繁体   English

使用精灵套件创建2d涟漪效果

[英]Create 2d ripple effect with sprite kit

How can i create a 2D ripple effect on water with sprite kit . 如何使用精灵套件在水上创建2D涟漪效果。

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. 就像我有一个场景,其中包括2D 静水 ,当点击它时,小圆圈出现缩放并消失说5-6个圆圈缩放平行给出涟漪的感觉不实际做任何事情只是在它上面创建图像。

I was wondering what could be the best way to sole this problem. 我想知道什么是解决这个问题的最佳方法。 How can i achieve something like that. 我怎样才能实现这样的目标。 Any ideas? 有任何想法吗?

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]];

There is a typo in the "CreateRipple" method, it runs itself in the codeblock rather than "rippleAction" like it should, the corrected method is: “CreateRipple”方法中有一个拼写错误,它在代码块中运行,而不是像它应该的那样“rippleAction”,修正后的方法是:

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:rippleAction];
    [self addChild:rippleNode];
}];

Hope that helps! 希望有所帮助!

Here's a complete very simple example of @Dobroćudni Tapir code. 这是一个完整的非常简单的@DobroćudniTapir代码示例。 If developing for iOS you can try in touchesBegan instead of mouseDown. 如果为iOS开发,您可以尝试使用touchesBegan而不是mouseDown。

import SpriteKit

class GameScene: SKScene {
    override func mouseDown(theEvent: NSEvent) {
        /* Called when a mouse click occurs */
        let location = theEvent.locationInNode(self)
        let singleRippleDuration = 1.0
        let ripleEndScale = CGFloat(3.0)
        let timeBetweenRipples = 0.3
        let numberOfRipples = 3
        let scaleUpAction = SKAction.scaleTo(ripleEndScale, duration: singleRippleDuration)
        let fadeOutAction = SKAction.fadeOutWithDuration(singleRippleDuration)
        let rippleAction = SKAction.group([scaleUpAction, fadeOutAction])

        let createRipple = SKAction.runBlock({
            let rippleNode = SKShapeNode(circleOfRadius: 200)
            rippleNode.position = location
            rippleNode.setScale(0)
            rippleNode.runAction(rippleAction)
            self.addChild(rippleNode)
        })

        let wait = SKAction.waitForDuration(timeBetweenRipples)
        runAction(SKAction.repeatAction(SKAction.sequence([createRipple, wait]), count: numberOfRipples))
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM