问题描述
我需要向我的应用程序添加雨粒子效果,我一直很难找到实际执行这个想法的方法.
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 方法教程:链接 但我不太确定这是否是最好的方法,考虑到新的 iOS 7 SpriteKit 粒子发射器在 Xcode 5 中可用.
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.将新视图的类改为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 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!