我正在使用SKScene
和SKEmitterNode
在我的应用中渲染粒子。
我遇到一个问题,偶尔我的场景似乎加载,但粒子没有动画。我有一个设置,在我的NIB中我有一个视图,它的类设置为SKScene
。然后将其作为IBOutlet
连接到我的视图控制器类。
我的particleScene.m
我的实现如下:
@implementation particleScene {
SKEmitterNode* emitter;
bool emitting;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor whiteColor];
[self addChild:[self newExplosion:50 : 280]];
}
return self;
}
- (SKEmitterNode *) newExplosion: (float)posX : (float) posy
{
emitter.name = @"explosion";
emitter.targetNode = self.scene;
emitter = [[SKEmitterNode alloc] init];
[emitter setParticleTexture:[SKTexture textureWithImageNamed:@"spark.png"]];
[emitter setParticleBirthRate:20];
[emitter setParticleSpeedRange:0.2];
[emitter setParticleLifetime:2.5];
[emitter setParticleLifetimeRange:0.49];
[emitter setParticlePositionRange:CGVectorMake(500, self.frame.size.height)];
[emitter setPosition:CGPointMake(posX, posy)];
[emitter setAlpha:0.8];
[emitter setParticleAlphaRange:0.2];
[emitter setParticleAlphaSpeed:-0.7];
[emitter setParticleScale:0.2];
[emitter setParticleScaleSpeed:0.45];
[emitter setYAcceleration:35];
return emitter;
}
-(void)update:(CFTimeInterval)currentTime {
[emitter setParticleColor:[UIColor colorWithHue:drand48() saturation:1.0 brightness:1 alpha:0.5]];
[emitter setParticleColorBlendFactor:1];
/* Called before each frame is rendered */
}
@end
然后,在我的视图控制器的viewDidAppear
方法中,我这样做是为了呈现场景:
SKView * skView = self.particleView;
// Create and configure the scene.
SKScene * scene = [sixthParticleScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
这里有什么我想念的吗?偶尔这会起作用,但大多数情况下场景加载并出现一些粒子,但它们是静态的。