1

Im attempting to use some of the code from a solution found on this page for creating a button Create Button in SpriteKit: Swift

class GameScene: SKScene {
   let button = SKSpriteNode(imageNamed: "yourImgName")

   override func didMoveToView(view: SKView) {
      button.name = "btn"
      button.size.height = 100
      button.size.width = 100
      button.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 50)
      self.addChild(button)

      //Adjust button properties (above) as needed
      }

   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
      let touch = touches.first
      let positionInScene = touch!.locationInNode(self)
      let touchedNode = self.nodeAtPoint(positionInScene)

    if let name = touchedNode.name {
        if name == "btn" {

            let yourNextScene = YourNextScene(fileNamed: "YourNextScene")
            self.view?.presentScene(yourNextScene!)

          }
      }
   }
}

and the current code that I have is supposed to make the player jump when the button is pressed, but nothing is currently happening when its pressed

import SwiftUI
import SpriteKit
import UIKit

class GameScene: SKScene {
    override func didMove(to view: SKView) {
        let button = SKSpriteNode(imageNamed: "playerOBJ")
        let player = SKSpriteNode(imageNamed: "playerOBJ")
        let playerRadius = player.frame.width / 2.0
    
    
        player.position = CGPoint(x: 200, y: 500)
        player.name = "Jimmy"
    
        addChild(player)
    
        player.physicsBody = SKPhysicsBody(circleOfRadius: playerRadius)
        player.physicsBody?.allowsRotation = false
        player.physicsBody?.friction = 0
        player.physicsBody?.restitution = 0
        player.zPosition = 100
    
        // Button
        button.name = "btn"
        button.size.height = 100
        button.size.width = 100
        button.position = CGPoint(x: 100, y: 100)
        self.addChild(button)
    
    
        // Physics
        physicsBody = SKPhysicsBody(edgeLoopFrom: frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)))
    
        Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { _ in
            player.physicsBody?.applyForce(CGVector(dx: 100, dy: 1000))
        }
    
         func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            let touch = touches.first
            let positionInScene = touch!.location(in: self)
            let touchedNode = self.atPoint(positionInScene)

            if let name = touchedNode.name {
                if name == "btn" {
                    player.physicsBody?.applyForce(CGVector(dx: 0, dy: 10000))
                }
            }
        }
    }
    override func update(_ currentTime: TimeInterval) { }
}

I'm thinking that maybe this is an issue with the press not being resitered at all but I'm not fully sure

Superbia
  • 13
  • 6

1 Answers1

0

Your main problem is you put all of your code inside the didMove function. You put the touchesBegan function inside the didMove function. When the didMove function finishes, touchesBegan goes away so none of your touches are going to be handled in the game.

You also declared the button and the player as local variables inside the didMove function.

override func didMove(to view: SKView) {
    let button = SKSpriteNode(imageNamed: "playerOBJ")
    let player = SKSpriteNode(imageNamed: "playerOBJ")
    // Rest of function omitted
}

When the didMove function finishes, the button and player go away too. You also have the same image name for the player and button.

The fix is to make the button and player variables properties of the GameScene class. Move touchesBegan out of the didMove function too.

class GameScene: SKScene {
    // Declare button and player here.
    var button = SKSpriteNode()
    var player = SKSpriteNode()

    override func didMove(to view: SKView) {
        // Initialize button, player, and everything else here.
    }

    func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // Code in touchesBegan func goes here.
    }
}
Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66