Add UIView to back of scene in SpriteKit game

大城市里の小女人 提交于 2019-12-13 02:39:15

问题


I am trying to make a advent calendar with a snow affect in swift2. I am using the game template while using SpiteKit.

Here is my code so far:

GameScene.swift

import SpriteKit

class GameScene: SKScene {
    /*override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            let location = touch.locationInNode(self)
            print(location)
        }
    }*/

    func test()
    {
        //Generate Doors

        //Initilization
        var adventDoors = [AdventDoor]()
        let offset = CGVector(dx: 10,dy: 10)
        var size = CGRectMake(offset.dx, offset.dy, 60, 60)

        var ypos:CGFloat = 20
        var xpos:CGFloat = 10
        var index = 0
        var xDoor = AdventDoor(frame: size)
        let randomIdentifier = [Int](1...24).shuffle()
        for _ in 1...4
        {
            for i in 1...6
            {
                size = CGRectMake(xpos, ypos, 60, 60)
                xDoor = AdventDoor(frame: size)
                xDoor.opaque = false
                xDoor.restorationIdentifier = "\(randomIdentifier[index])"
                xDoor.generateDoor()
                adventDoors.append(xDoor)
                print("1...6")
                ypos += 80
                //xpos += 20
                index++

                if i == 6
                {
                    print("Moving to next view")
                }

            }

            xpos += 80
            ypos = 20
        }

        size = CGRectMake(10, 500, 300, 60)
        xDoor = AdventDoor(frame: size)
        xDoor.opaque = false
        xDoor.restorationIdentifier = "\(25)"
        xDoor.generateDoor()
        adventDoors.append(xDoor)


        index = 0

        for door in adventDoors
        {
            index++
            self.view?.addSubview(door)
        }
        print("\(index) doors were added")
    }
}

GameViewController.swift

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()



        if let scene = GameScene(fileNamed:"GameScene") {

            // Configure the view.
            let skView = self.view as! SKView
            //skView.showsFPS = true
            //skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill
            scene.backgroundColor = UIColor.greenColor()
            skView.presentScene(scene)

            scene.test()

            //Snow
            let wrappedSnowPath = NSBundle.mainBundle().pathForResource("Snow", ofType: "sks")
            if let snowPath = wrappedSnowPath
            {
                let snowEmitter:SKEmitterNode = NSKeyedUnarchiver.unarchiveObjectWithFile(snowPath) as! SKEmitterNode
                let screenBounds = UIScreen.mainScreen().bounds
                snowEmitter.position = CGPointMake(screenBounds.width, screenBounds.height)
                scene.addChild(snowEmitter)
            }
        }
    }

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return .AllButUpsideDown
        } else {
            return .All
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

AdventDoor.swift just contains a custom UIView (AdventDoor) along with some more functions

This is what it looks like.

As you can see, the snow SKEmitterNode particles are behind the AdventDoors and not in front.

How would I get my snow to display in front of the UIView Advent Doors instead of behind?


回答1:


Instead of addsubview for the door, What you need to do is rearrange how your views are laid out. What you need is a UIView as your main view, then you add the SKView as a child to the main view. Then if you want to add the doors in during the scene creation process, you need to do self.view.superview.insertSubView(door, atIndex:0) or self.view.superview.insertSubView(door, belowSubView:self.view) so that the doors are placed behind the scene subview



来源:https://stackoverflow.com/questions/33972461/add-uiview-to-back-of-scene-in-spritekit-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!