ios - SKNode 的运动效果

标签 ios sprite-kit motion-detection sknode

我正在获取 int SpriteKit。并且想知道如何在 SKNode 对象上创建运动效果。

对于 UIView,我使用以下方法:

+(void)registerEffectForView:(UIView *)aView
                   depth:(CGFloat)depth
{
UIInterpolatingMotionEffect *effectX;
UIInterpolatingMotionEffect *effectY;
effectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
                                                          type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
effectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
                                                          type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];


effectX.maximumRelativeValue = @(depth);
effectX.minimumRelativeValue = @(-depth);
effectY.maximumRelativeValue = @(depth);
effectY.minimumRelativeValue = @(-depth);

[aView addMotionEffect:effectX];
[aView addMotionEffect:effectY];
}

我还没有发现任何与 SKNode 类似的东西。所以我的问题是这可能吗?如果没有,我该如何实现。

最佳答案

UIInterpolatingMotionEffect只是将设备倾斜映射到它所应用的 View 的属性——这都是关于你设置它的keyPath,以及这些关键路径的 setter 所做的。

您发布的示例将水平倾斜映射到 View 的 center 属性的 x 坐标。当设备水平倾斜时,UIKit 会自动调用 View 上的 setCenter:(或者设置 view.center =,如果你喜欢这样的语法),传递一个点X 坐标与水平倾斜量成比例地偏移。

您也可以在自定义 UIView 子类上定义自定义属性。由于您使用的是 Sprite Kit,因此可以子类化 SKView 以添加属性。

例如...假设您的场景中有一个云 Sprite ,您想要在用户倾斜设备时移动它。将其命名为 SKScene 子类中的一个属性:

@interface MyScene : SKScene
@property SKSpriteNode *cloud;
@end

并在移动它的 SKView 子类中添加属性和访问器:

@implementation MyView // (excerpt)

- (CGFloat)cloudX {
    return ((MyScene *)self.scene).cloud.position.x;
}
- (void)setCloudX:(CGFloat)x {
    SKSpriteNode *cloud = ((MyScene *)self.scene).cloud;
    cloud.position = CGPointMake(x, cloud.position.y);
}

@end

现在,您可以创建一个 UIInterpolatingMotionEffect,其 keyPathcloudX,它应该*自动移动场景中的 Sprite 。

(*完全未经测试的代码)

关于ios - SKNode 的运动效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21552911/

相关文章:

ios - 在 Collection View 的 performBatchUpdates 崩溃

swift - 为什么我的 didBeginContact 函数检测到太多冲突?

Swift Spritekit 碰撞处理

c# - 通过脸部中心进行光标移动?

image-processing - 如何以编程方式检测屏幕撕裂?

ios - 检查 PFObject 是否为 nil

ios - UITextView:获取带有换行信息的文本

ios - 如何将 CNContactPickerViewController 与 objective-c 一起使用?

swift - 如何复制具有物理属性的 Sprite 然后将其添加到场景中?

android - 使用 OpenCV 进行运动检测