添加SpriteKit粒子到现有的应用程序



我添加了一个SpriteKit粒子到我的应用程序- skView已经添加到一个现有的图像视图(myImageView)。所有的工作都很好,但是skscene不能设置为clear color来显示我的图像在发射器后面。你可以改变颜色,但不能改变透明的颜色。'

    SKView *skView = [[SKView alloc] initWithFrame:CGRectMake(0.0, 0.0, 768, 1024)];
[myImageView addSubview:skView];
SKScene *skScene = [SKScene sceneWithSize:skView.frame.size];
skScene.scaleMode = SKSceneScaleModeAspectFill;
skScene.backgroundColor = [UIColor clearColor];

SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle          mainBundle] pathForResource:@"MyParticle" ofType:@"sks"]];

排放国。

= CGPointMake(400,0);
[skScene addChild:emitter];
[skView presentScene:skScene];

[self.view addSubview:myImageView];

不幸的是,你不能在ios7中使SKView透明。在iOS 8中,可以使用:

skView.allowsTransparency = YES;
skScene.backgroundColor = [UIColor clearColor];

在iOS 7中,你可以添加一个和你的skView一样大的spriteView作为背景,并将你的myImage设置为纹理。

出于性能原因,我建议您避免将SKScene的颜色设置为[UIColor clearColor](尽可能使用不透明视图)。相反,你可以创建一个SKSpriteNode并将其添加到场景中作为背景…

SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"image"];
background.size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
// Bottom/left of your image
background.anchorPoint = CGPointZero;
background.position = CGPointZero;
// Make sure the background is behind other nodes in the scene
background.zPosition = -100;
[self addChild:background];

由于背景被添加到SKScene中,当你过渡到一个新的场景时,它会被自动删除/释放。

最新更新