【问题标题】:SKScene and Swift Files are not linkingSKScene 和 Swift 文件未链接
【发布时间】:2016-06-11 17:24:44
【问题描述】:

在我的 xcode 项目中,我试图从一个 SKScene 过渡到另一个。触发转换的是 SKLabelNode 的触摸。所有这些都正常工作。但是在场景更改后,我的类中控制“StartScence.sks”的代码都不起作用。好像我的“StartScene.swift”和“StartScene.sks”没有关联。

这是我的 GameScene 中的代码,

import SpriteKit

class GameScene: SKScene {

var isTouched: Bool = false
var booleanTouched: Bool!
let ns = SKScene(fileNamed: "StartScene")
let crosswf = SKTransition.crossFadeWithDuration(2)


override func didMoveToView(view: SKView) {


    let backgroundimage = SKSpriteNode(imageNamed: "ipbg")
    backgroundimage.size = CGSize(width: self.frame.size.width, height: self.frame.size.height)
    backgroundimage.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
    addChild(backgroundimage)

    let playButton = SKLabelNode(fontNamed: "")
    playButton.name = "play"
    playButton.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2 + 100)
    playButton.text = "Play"

    let wait = SKAction.waitForDuration(2)
    let run = SKAction.runBlock({


        let randomNumber = Int(arc4random_uniform(UInt32(4)))

        switch(randomNumber){

        case (0):

            playButton.fontColor = UIColor.blueColor()

        case 1:

            playButton.fontColor = UIColor.yellowColor()

        case 2:

            playButton.fontColor = UIColor.purpleColor()

        case 3:

            playButton.fontColor = UIColor.orangeColor()

        default: print("default")


        }

    })

    addChild(playButton)
    var repeatActionForever = SKAction.repeatActionForever(SKAction.sequence([wait, run]))
    runAction(repeatActionForever)
    backgroundimage.zPosition = 1
    playButton.zPosition = 2



}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let touch = touches.first! as UITouch
    let touchLocation = touch.locationInNode(self)
    let touchedNode = nodeAtPoint(touchLocation)


    if (touchedNode.name == "play"){

        scene!.view?.presentScene(ns!, transition: crosswf)

    }else{


    }


}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}

}

这是我的“StartScene.swift”代码,它没有正确控制“StartScene.sks”。

import SpriteKit

class StartScene: SKScene {


override func didMoveToView(view: SKView) {


    print("Scene Loaded")

}

}

【问题讨论】:

    标签: xcode sprite-kit skscene


    【解决方案1】:

    有两点需要注意。

    1. 在您的代码中,您当前正在像这样加载 .SKS 文件

      let ns = SKScene(fileNamed: "StartScene")
      

    您的新场景将加载,因为所有 SKS 文件都属于 SKScene 类。 但是,它只会使用该类的代码。

    如果您希望它与您的 StartScene 类(SKScene 的子类)中的代码一起加载。换行到这个

        let ns = StartScene(fileNamed: "StartScene")
    
    1. 我们还可以使 SKS 文件具有自定义类,而不是默认的 SKScene。所以当它被加载时,它使用一个自定义类。

    在 Xcode 中打开 SKS 文件,以便为场景提供自定义类。在场景编辑器中,没有选择任何内容。单击实用程序区域,切换到自定义类检查器,这是右侧的最后一个选项卡。

    给它一个自定义的 StartScene 类。

    它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多