【问题标题】:Can you add a sprite kit particle emitter to the background of a UIViewController?你能在 UIViewController 的背景中添加一个 sprite kit 粒子发射器吗?
【发布时间】:2015-06-11 21:36:50
【问题描述】:

我是 sprite kit 游戏开发的新手,我想知道是否可以将粒子发射器添加到 UIViewController。我想在屏幕上有按钮和粒子发射器,以解决我的应用程序想法,这就是问题所在。 SKScene 没有内置按钮对象,所以我决定将粒子发射器添加到 UIViewController 类可能会更好。是否可以在背景中制作粒子发射器并在其上放置按钮?

【问题讨论】:

    标签: ios


    【解决方案1】:

    您是否有特定的原因要为此使用 SpriteKit?如果您只想在 UIViewController 中集成粒子效果,请考虑使用视图并添加 CAEmitterLayer 和 CAEmitterCell。

    它们在不使用任何 SpriteKit 的情况下工作得很好,而且性能很好。

    【讨论】:

      【解决方案2】:

      SpriteKit 的一大优点是易于使用 UIKit 实现:

      创建一个 SKScene 子类:

      //ParticleScene.h
      #import <SpriteKit/SpriteKit.h>
      
      @interface ParticleScene : SKScene
      
      @property SKEmitterNode *emitter;
      
      @end
      
      
      
      //ParticleScene.m
      #import "ParticleScene.h"
      
      @implementation ParticleScene
      
      -(instancetype)initWithSize:(CGSize)size{
          if (self = [super initWithSize:size]) {
              self.backgroundColor = [UIColor clearColor];
              [self addEmitterNode];
          }
          return self;
      }
      
      -(void) addEmitterNode{
          SKEmitterNode *emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"SpriteKitParticleEmitter" ofType:@"sks"]];
          emitter.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height);
          [self addChild:emitter];
      }
      
      @end
      

      创建 SpriteKitParticleEmitter.sks 顶部菜单 -> 文件/新建/文件.../(资源)SpriteKit 粒子文件

      在这里你可以随意修改发射器。

      将发射器添加到您的 UIViewController 您可以将 UIView 添加到 Storyboard 并将其子类设置为 SKView,或者您可以通过编程方式添加 SKView:

      添加-(void) viewDidLoad方法:

      SKView *spriteKitView = self.particleView; // Defined on storyboard
      //Use SKView *spriteKitView = [[UIView alloc] initWithFrame:frame] to create it programatically and then [self.view addSubView:view];
      spriteKitView.showsFPS = NO;
      spriteKitView.showsNodeCount = NO;
      spriteKitView.allowsTransparency = YES; //Prevents this view to show as a black square
      spriteKitView.backgroundColor = [UIColor clearColor];
      
      SKScene *scene = [JPParticleScene sceneWithSize:view.bounds.size];
      scene.scaleMode = SKSceneScaleModeAspectFill;
      
      [view presentScene:scene];
      

      如果您使用故事板,只需将 self.spriteKitView 放在您的 UIButtons 后面即可。

      【讨论】:

      • 什么是 JPParticleScene?
      猜你喜欢
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 2013-11-29
      • 1970-01-01
      • 2015-09-27
      相关资源
      最近更新 更多