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

时间:2014-02-03 10:30:06

标签: ios objective-c sprite-kit

如何使用精灵工具包在水面上创建2D涟漪效果。

就像我有一个场景,包括 2D 仍然,当点击它时,小圆圈出现比例并消失说 5-6 圆形缩放平行给出涟漪的感觉不是实际做任何事情只是在它上面创建图像。

我想知道什么是解决这个问题的最佳方法。我怎样才能实现这样的目标。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

在您希望创建纹波的节点中,只需执行类似这样的操作

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

答案 1 :(得分:1)

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

希望有所帮助!

答案 2 :(得分:0)

以下是@DobroćudniTapir代码的完整非常简单的示例。如果为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))
    }
}