我需要在我的应用程序中添加雨粒子效果,我一直很难找到真正执行这个想法的方法。
我尝试按照这个 CALayer 方法教程进行操作:Link但考虑到 Xcode 5 中可用的新 iOS 7 SpriteKit 粒子发射器,我不太确定这是否是最佳方法。
我已经创建了 .sks
文件并且它在我的层次结构中,但我仍然无法将它添加到我的 Storyboard/项目中。
话虽如此,我究竟该如何将 SpriteKit 粒子 (sks) 添加到我的 View 中?我对 SpriteKit 框架中的场景、分层等一点都不熟悉,因为我不是游戏开发人员。 我需要尽可能多的详细信息和示例代码,以便我可以解决这个问题
更新:
我遵循了 SO 成员 AyatollahAndy 的回答中提供的指示,请在下面查看他的回答。虽然我能够在我的 view
中显示 SKScene
,但是当收到任何触摸事件时,应用程序崩溃了。我得到以下信息:
谢谢
最佳答案
在您的 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/