ios - 如何使用 iOS 7 SpriteKit Particle 向不是游戏的 iOS App 添加粒子效果?

标签 ios objective-c uiview sprite-kit skemitternode

我需要在我的应用程序中添加雨粒子效果,我一直很难找到真正执行这个想法的方法。

我尝试按照这个 CALayer 方法教程进行操作:Link但考虑到 Xcode 5 中可用的新 iOS 7 SpriteKit 粒子发射器,我不太确定这是否是最佳方法。

我已经创建了 .sks 文件并且它在我的层次结构中,但我仍然无法将它添加到我的 Storyboard/项目中。

话虽如此,我究竟该如何将 SpriteKit 粒子 (sks) 添加到我的 View 中?我对 SpriteKit 框架中的场景、分层等一点都不熟悉,因为我不是游戏开发人员。 我需要尽可能多的详细信息和示例代码,以便我可以解决这个问题

更新: 我遵循了 SO 成员 AyatollahAndy 的回答中提供的指示,请在下面查看他的回答。虽然我能够在我的 view 中显示 SKScene,但是当收到任何触摸事件时,应用程序崩溃了。我得到以下信息:enter image description here

谢谢

最佳答案

在您的 UIView 中创建一个 SKScene 以添加一个 SKEmitterNode 粒子效果。

一种方法:

1.在 Storyboard 中(如果您愿意,也可以通过编程方式)在现有 View 之上添加一个 View 对象,并根据您的需要调整其大小。
2.将新 View 的类更改为SKView
3.在您的 View Controller .h 文件中为 SKView 创建一个属性:

@property IBOutlet SKView *skView;

4.将 Storyboard上的 SKView 链接到 skView 属性。
5.创建一个新类,继承SKScene。 MyScene.h 看起来像:

#import <SpriteKit/SpriteKit.h>
@interface MyScene : SKScene
@end

下面的 MyScene.m 包含创建粒子效果的代码,无论何时何地触摸 SKView。

#import "MyScene.h"

@implementation MyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
        SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        myLabel.text = @"Hello, World!";
        myLabel.fontSize = 30;
        myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));

        [self addChild:myLabel];
    }
    return self;
}

//particle explosion - uses MyParticle.sks
- (SKEmitterNode *) newExplosion: (float)posX : (float) posy
{
    SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"]];
    emitter.position = CGPointMake(posX,posy);
    emitter.name = @"explosion";
    emitter.targetNode = self.scene;
    emitter.numParticlesToEmit = 1000;
    emitter.zPosition=2.0;
    return emitter;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        //add effect at touch location
        [self addChild:[self newExplosion:location.x : location.y]];
    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

6.在您的主视图 Controller 中,包括您的场景类:

#import "MyScene.h"

并将代码添加到 viewDidLoad 以初始化 SKView:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the SKView
    SKView * skView = _skView;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}

然后您应该在主 UIView 中有一个可用的 SKScene

关于ios - 如何使用 iOS 7 SpriteKit Particle 向不是游戏的 iOS App 添加粒子效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19620866/

相关文章:

iphone - 按下取消时显示警报 View 并保持在同一 View Controller 上

ios - timeInterval 后的 WebService 调用

ios - 程序化 UIView 内的程序化 UIButton 无法正常工作

ios - 如何快速删除共享 View

ios - 带有 customView (UISwitch) 的 UIBarButtonItem 在创建工具栏后不久就消失了(即使它是一个非常简单的代码!)

ios - 像 Chanel 应用程序一样自定义 Collection View 布局

具有多个输出的 iOS 片段着色器

ios - iPhone UIAlertview 放弃内存

iphone - 在 Xcode 中加速 NSTimer

iphone - 使用 addSubview 添加时,使 View 调整到其父级