在SpriteKit游戏中将UIView添加到场景的后面 [英] Add UIView to back of scene in SpriteKit game

查看:70
本文介绍了在SpriteKit游戏中将UIView添加到场景的后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在swift2中制作一个有雪影响的降临日历.我在使用SpiteKit时正在使用游戏模板.

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 仅包含一个自定义UIView(AdventDoor)以及其他一些功能

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

这就是它的样子.

如您所见,积雪的SKEmitterNode粒子位于AdventDoors的后面,而不是在前面.

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

我如何让雪显示在UIView Advent Doors的前面而不是后面?

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

推荐答案

代替门的addsubview,您需要做的是重新排列视图的布局.您需要的是UIView作为主视图,然后将SKView作为子视图添加到主视图.然后,如果要在场景创建过程中添加门,则需要执行self.view.superview.insertSubView(door, atIndex:0)self.view.superview.insertSubView(door, belowSubView:self.view)以便将门放置在场景子视图的后面

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

这篇关于在SpriteKit游戏中将UIView添加到场景的后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆