Выпуск кнопки главного меню SpriteKit - PullRequest
0 голосов
/ 08 апреля 2020

Интересно, если бы вы могли помочь, я создал главное меню с фоном и кнопкой узла. Когда я нажимаю на кнопку «ВОСПРОИЗВЕДЕНИЕ», игра не переходит к моей GameScene, а вместо этого вызывает мой оператор печати. Вот код ниже:

import Foundation
import SpriteKit
import GameplayKit

class MainMenu: SKScene, SKPhysicsContactDelegate {
    override func didMove(to view: SKView) {

        background()
        playButton()

    }

    func background()
    {
        let back = SKSpriteNode(imageNamed: "MainMenu")
        back.anchorPoint = CGPoint(x: 0, y: 0)
        back.zPosition = 1
        back.size = CGSize(width: frame.width,height: frame.height)
        back.name = "Background"

        addChild(back)

    }

    func playButton()
    {
        let button = SKSpriteNode(imageNamed: "button")
        button.zPosition = 2
        button.name = "Play Button"
        button.position = CGPoint(x: frame.width/2, y: frame.height/2)
        button.setScale(0.3)

        addChild(button)
    }

    func loadGame(){

        guard let skView = self.view as SKView? else{
            print("Could not get Skview")
            return
        }
        guard let scene = GameScene(fileNamed: "GameScene") else {
            print("Error Getting GameScene")
            return
        }
        //let skView = view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true
        skView.showsPhysics = true

        scene.scaleMode = .aspectFill

        skView.presentScene(scene)
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else{
            return
        }

        let touchLoation = touch.location(in: self)
        let touchNodes = nodes(at: touchLoation)
        let firstTouchedNode = atPoint(touchLoation).name
        print(firstTouchedNode)

        if firstTouchedNode == "Play Button"{
        loadGame()

            }

        }
}

1 Ответ

0 голосов
/ 08 апреля 2020
func loadGame(){

guard let skView = self.view as SKView? else{
    print("Could not get Skview")
    return
}
var scene:SKScene = GameScene(size: self.size)
}
//let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
skView.showsPhysics = true

scene.scaleMode = .aspectFill

var transition:SKTransition = SKTransition.fadeWithDuration(1)
self.view?.presentScene(scene, transition: transition)

}



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

for touch in touches {
        let location = touch.location(in: self)
        let node = self.atPoint(location)

        if node.name == "Play Button"{
          //when playbutton is pressed execute code here
          loadGame()

     }
 }
...