Tomato_code
Tomato_code

Reputation: 15

Detect a Tap within the first Scene : SpriteKit Swift iOS, having small problem

so I have a menu screen in my app. Swift, SpriteKit, iOS 7-10,11,12,13..

All I want is a user to tap the button on my screen, and have it move them to another screen. This works in other references in my code. However All I did was change the first loading scene in the GameViewController to my own swift file, and this is how the code looks:

import Foundation
import SpriteKit
import GameplayKit
import UIKit


class FrontMenu : SKScene {
    var play = SKSpriteNode(imageNamed: "pbut")
    
var credit = SKSpriteNode()

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self)
            let node : SKNode = self.atPoint(location)
            if node.name == "play" {
                print("play tapped.")
// this is where I would have placed the transition, but this did not work.
                
            }
            else if node.name == "cred" {
                       NSLog("credits tapped")
                      
                       
                   }
             if location.x <= -90 && location.y >= -160 {
                NSLog("Area hit for play")
// This area is still not detected within the app, and there is no button function.
                let gameScene = GameScene(fileNamed: "GameScene")
                gameScene?.scaleMode = .aspectFill
                self.view?.presentScene(gameScene!, transition: SKTransition.fade(withDuration: 0.86))

Basically that does that. You can see where I implemented the transition. Am I missing an import? Am I missing something to change to the overall Plist file? or Maybe something else within GameViewController.swift, I changed the first loading swift file, so that it does load this code first in the app. However it does not detect the touches. Thank you for your help!

Upvotes: 0

Views: 198

Answers (1)

nicholas
nicholas

Reputation: 346

It looks like you are missing code that would set the name for your node. Make sure that in your didMove function that you are setting the name of the node and adding it to the scene.

//inside scene
override func didMove(to view: SKView) {
    play.name = "play"
    self.addChild(play)
}

The other thing you have to make sure of is that you created a SpriteKit scene file in your project and set the custom class to be FrontMenu

As for switching the file in your GameViewController, you should just be using the name of the file and not the file extension for fileNamed::

//inside GameViewController
override func viewDidLoad() {
        super.viewDidLoad()
        
        if let view = self.view as! SKView? {
            // Load the SKScene from 'MneuScene.sks'
            if let scene = SKScene(fileNamed: "MenuScene") {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .resizeFill
                // Present the scene
                view.presentScene(scene)
            }
        }
}

Upvotes: 1

Related Questions