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

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

问题描述

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

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 方法教程:链接 但我不太确定这是否是最好的方法,考虑到 Xcode 5 中提供的新 iOS 7 SpriteKit 粒子发射器.

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,请在下面查看他的回答.虽然我能够在我的 view 中显示 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:

谢谢

推荐答案

在你的UIView中创建一个SKScene来添加一个SKEmitterNode粒子效果.

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

一种方法:

1.在情节提要中(或者如果您愿意,可以通过编程方式)在现有视图之上添加一个视图对象,并根据您的需要调整它的大小.
2.将新视图的class改为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 链接到 skView 属性.
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];
}

然后你应该在你的主 UIView 中有一个工作的 SKScene.

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

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

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