SpriteKit 的相机抖动效果 [英] A camera shake effect for SpriteKit

查看:40
本文介绍了SpriteKit 的相机抖动效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道一些开箱即用的库,它们可能会为 SKNode 提供相机抖动等效果?

Does anyone know some out of the box libraries that might provide effects such as camera shake to an SKNode?

如果没有,有没有一种简单的方法可以使用动作来实现相机抖动?

If not is there an easy way to implement a camera shake using actions?

谢谢

推荐答案

我发现了一个使用 SKAction 的优雅方法,它可以让你的节点动摇.例如,水平摇动:

I found an elegant method using SKAction, that make shake your node. For example, with an horizontal shake:

-(void)shake:(NSInteger)times {
    CGPoint initialPoint = self.position;
    NSInteger amplitudeX = 32;
    NSInteger amplitudeY = 2;
    NSMutableArray * randomActions = [NSMutableArray array];
    for (int i=0; i<times; i++) {
        NSInteger randX = self.position.x+arc4random() % amplitudeX - amplitudeX/2;
        NSInteger randY = self.position.y+arc4random() % amplitudeY - amplitudeY/2;
        SKAction *action = [SKAction moveTo:CGPointMake(randX, randY) duration:0.01];
        [randomActions addObject:action];
    }

    SKAction *rep = [SKAction sequence:randomActions];

    [self runAction:rep completion:^{
        self.position = initialPoint;
    }];
}

这篇关于SpriteKit 的相机抖动效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆