如何将粒子效果添加到iOS应用程序中,而不是使用iOS 7 SpriteKit粒子的游戏? [英] How to add particle effects to an iOS App that is not a game using iOS 7 SpriteKit Particle?

查看:153
本文介绍了如何将粒子效果添加到iOS应用程序中,而不是使用iOS 7 SpriteKit粒子的游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的应用添加雨粒子效果,我一直很难找到实际执行这个想法的方法。

I need to add a rain particle effect to my app, I have been having a tough time finding ways to actually execute this idea.

我尝试了这个CALayer方法教程:

I tried following this CALayer approach tutorial : Link but I am not quite sure if this is the best approach, considering the new iOS 7 SpriteKit Particle Emitter available in Xcode 5.

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

I have already created the .sks file and it's in my Hierarchy, but I am still unable to add it to my storyboard / project.

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

With that being said, How exactly do I add a SpriteKit Particle (sks) to my view? I am not at all familiar with scenes, layering , etc in the SpriteKit framework as I am not a game developer. I need the most details and sample code possible so that I can figure this out please

更新:
我按照SO成员答案中提供的指示:AyatollahAndy,请参阅下面的答案。虽然我能够在视图中显示 SKScene ,但是当收到任何触摸事件时,应用程序会崩溃。我得到以下内容:

UPDATE: I have followed the direction provided in an answer by fellow SO member: AyatollahAndy, please see his answer below. Although I was able to display the SKScene in my view the app crashes when any touch event is received. I get the following:

谢谢

推荐答案

中创建 SKScene UIView 添加 SKEmitterNode 粒子效果。

Create a SKScene in your UIView to add a SKEmitterNode particle effect.

一种方法:

1.在故事板中(或者以编程方式,如果您愿意)在现有视图的顶部添加View对象,并根据需要调整其大小。

2.将新视图的类更改为 SKView

3.在视图控制器.h文件中创建 SKView 的属性:

1.In storyboard (or programatically if you prefer) add a View object on top of the existing View and resize it to your needs.
2.Change the class of the new view to SKView
3.In your view controller .h file create a property for the SKView:

@property IBOutlet SKView *skView;

4.在故事板上链接 SKView 到$ sk $属性。

5.创建一个新类,继承 SKScene 。 MyScene.h将如下所示:

4.Link the SKView on your storyboard to the skView property.
5.Create a new class, subclassing SKScene. MyScene.h will look like:

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

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

MyScene.m below contains code to create a particle effect whenever and wherever the SKView is touched.

#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.在主视图控制器中,包含你的场景类:

6.In your main view controller, include your scene class:

#import "MyScene.h"

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

and add code to viewDidLoad to initialise the 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];
}

你应该有一个工作 SKScene 在主 UIView 中。

You should then have a working SKScene within your main UIView.

这篇关于如何将粒子效果添加到iOS应用程序中,而不是使用iOS 7 SpriteKit粒子的游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆