SpriteKit的相机抖动效果 [英] A camera shake effect for SpriteKit
本文介绍了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屋!
查看全文