简体   繁体   中英

levels in xcode spritekit, swift

I have made some games in Xcode using spritkit(Swift); however, never with levels. I do not really understand how you make different levels in spritekit. It does not seem like a good idea to make different levels in different "GameScenes", or is it? I would really appreciate some help with this. Thanks in advance!

I understand this is very late but I'd like to help future programmers. The way I was taught to add levels in a way similar to how Angry Birds has its levels. What you do is create a new .sks file by going in Xcode to

File => New => File...

When the popup appears select SpriteKit Scene. Save its name to whatever you feel like. At this time, take a break to make your level alone in the new SpriteKit Scene. Here's an example for a game similar to Angry Birds:

例

[Required] Now, to be precise in where you want the level to be, go to your GameScene.sks file and add a new node to the place you want your level to be. In the Attributes inspector, name the node to levelNode.

在此处输入图片说明

Now, go to your GameScene.swift file. Go to the top of the file and add

var levelNode:SKNode!

Then navigate to your didMoveToView function and add the following code to the beginning of it:

levelNode = self.childNodeWithName("levelNode")

After the that line of code, add this:

let resourcePath = NSBundle.mainBundle().pathForResource("WhateverYouNamedYourLevel", ofType: "sks")
let newLevel = SKReferenceNode (URL: NSURL (fileURLWithPath: resourcePath!))
    levelNode.addChild(newLevel)

Good Job! You have just created your first level! Hope this helped, feel free to leave an upvote! :p

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM