【问题标题】:Motion effect for SKNodeSKNode 的运动效果
【发布时间】:2014-02-04 12:43:23
【问题描述】:

我得到了 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 类似的东西。所以我的问题是有可能吗?如果没有,那么我该如何实现它。

【问题讨论】:

    标签: ios sprite-kit motion-detection sknode


    【解决方案1】:

    UIInterpolatingMotionEffect 在深层工作,您不能使用像“cloudX”这样的任意 keyPath。即使添加了motionEffects,center属性的实际值也不会改变。 所以答案是,不能添加UIView以外的运动效果。也不能使用除特定属性之外的任意属性,例如“中心”或“框架”。

    【讨论】:

      【解决方案2】:

      UIInterpolatingMotionEffect 只是将设备倾斜度映射到它所应用到的视图的属性——这完全取决于您使用 keyPaths 设置它的内容,以及这些关键路径的设置器的作用。

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

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

      例如...假设您的场景中有一个云精灵,您想在用户倾斜设备时移动它。将其命名为 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,它应该* 自动移动场景中的精灵。

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

      【讨论】:

      • 这里的问题是SKView应该只用于显示一些SKScene,而不是代替SKNodes。使用您的方法,我将不得不处理两个场景而不是一个。这是禁止的,因为您应该始终一次呈现 1 个 SKScene。
      • 这只是您可以使用的一般架构的草图——关键是UIInterpolatingMotionEffect 映射到您在视图上选择的任何属性,所以它的作用取决于您使用什么属性实施以及如何实施。在此示例中,MyScene 是您希望在视图中呈现的任何场景。没有说受运动影响的节点必须是其中唯一的节点,因此如果您希望场景包含其他节点,它可以。或者,您可以在 SKView 子类上设置属性访问器,以使用随着您在视图中呈现不同场景而变化的节点。
      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      相关资源
      最近更新 更多