在您的UIView
中创建一个SKScene
以添加SKEmitterNode
粒子效果。这样做的
方式一:
1.In故事板(或编程,如果你喜欢)对现有的View的顶部添加视图对象,并将其调整到您的需要。
2.Change类新的视图来SKView
3.In您的视图控制器的.h文件中创建为SKView
属性:
@property IBOutlet SKView *skView;
4.Link的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。在您的主视图控制器,包括场景类:
#import "MyScene.h"
并将代码添加到viewDidLoad中初始化的SKView:你的主要UIView
内
- (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
。
我很高兴听到在我的应用程序中没有SpriteKit的“整个软件包”就可以实现这一点,正如我所说 - >我该如何将所述的粒子添加到普通视图? O已经生成了'.sks'文件。 – vzm
嗯,是的,你必须链接SpriteKit.framework意思是“整个包”。这并不重要,因为这个库是内置在iOS中的,并不会增加应用程序的大小。尽管如此,为了渲染粒子效果,您必须创建一个带有SKScene的SKView并将粒子效果放在它上面。除非其他所有视图都由Sprite Kit节点组成,否则所有其他视图都在顶部或底部。 – LearnCocos2D
为什么不只是添加一个“部分透明的雨滴覆盖”,并有一个[UIView animationXXX:]应用于它? – dklt