Removing Background of SKView - Particle Emitter -

2019-09-14 15:25发布

问题:

So I followed this tutorial and created a small particle emitter for when the player achieves a goal (kinda like a celebration).

I'm using a single view application, just like the tutorial, but I can't seem to remove the background of the emitter... so I get a black square underneath the particles.

I changed the View/SKView in storyboard to clear colour, but it still appears to be black when the app is run...

Apple Docs say this: To create an emitter with no background color, set the opacity in the color picker to 0.

Which I did here in the Particle.sks using the editor:

But when I run the app, it still displays a coloured box the size of the View...

How can I remove all background, so when the particles emit, it's just the particles?

回答1:

When you create your scene view, you need to allow for transparency, so add self.allowsTransparency = true; if you have a custom class for your view, or in the view controller viewDidLoad: add view.allowsTransparency = true;

edit:

- (void)viewDidLoad
{
    [super viewDidLoad];

    BokehScene * scene = [BokehScene sceneWithSize:_particleBackground.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    self.view.allowTransparency = Yes;
    [_particleBackground presentScene:scene];
}


回答2:

To make your particle emitter's background colour transparent, you need to set SKView.allowsTransparency = true, and Particle emitter scene's background to .backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0) Example:

class GameViewController: UIViewController {

   override func viewDidLoad() {
    super.viewDidLoad()

    let skView = SKView()
    skView.frame = CGRectMake(100, 10, 200, 200)
    skView.allowsTransparency = true


    let scene = Emmiter() // this is your particle emitter class 
    scene.size = skView.bounds.size
    scene.scaleMode = .AspectFill
    scene.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0)


    skView.presentScene(scene)

    view.addSubview(skView)

   } 
}


回答3:

Swift 4 Control + drag from the SKView in your storyboard to the View Controller to create an outlet skView. Then enable background transparency and set the background colour to clear.

import UIKit
import SpriteKit

class ViewController: UIViewController {

  var scene: Emitter!
  var size: CGSize! 

  @IBOutlet weak var skView: SKView!

  override func viewDidLoad() {
      super.viewDidLoad()

      size = self.skView.frame.size
      scene = Emitter(size: size)
      scene.backgroundColor = UIColor.clear
      skView.allowsTransparency = true
      skView.presentScene(scene)
  }
}