WatchKit Move a SimpleSpriteNode in SpriteKit Game - ios

I would like to know if anyone has a way to move a SKSpriteNode in SpriteKit Watch Game Using WKCrownDelegate. either in Y direction or X direction
Hope this Helps Others that are Starting with WatchKit.
This is my GameElement.swift:
extension GameScene {
func addPlayer() {
player = SKSpriteNode(imageNamed: "Spaceship")
player.setScale(0.15)
player.position = CGPoint(x: 5, y: -60)
player.name = “ONE”
player.physicsBody?.isDynamic = false
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
player2 = SKSpriteNode(imageNamed: "Spaceship")
player2.setScale(0.15)
player2.position = CGPoint(x: 5, y: -60)
player2.name = “ONE”
player2.physicsBody?.isDynamic = false
player2.physicsBody = SKPhysicsBody(rectangleOf: player2.size)
addChild(player)
addChild(player2)
playerPosition = player.position
}
}
This is my GameScene.swift:
class GameScene: SKScene, SKPhysicsContactDelegate, WKCrownDelegate {
var watchParticles:SKEmitterNode!
var player:SKSpriteNode!
var player2:SKSpriteNode!
var playerPosition:CGPoint!
override func sceneDidLoad() {
self.scaleMode = SKSceneScaleMode.aspectFill
watchParticles = SKEmitterNode(fileNamed: "watchParticles")
addChild(watchParticles)
self.physicsWorld.gravity = CGVector(dx: 0 , dy: 0)
physicsWorld.contactDelegate = self
addPlayer()
}
func moveSprite(player : SKSpriteNode,moveDirection: String){
switch moveDirection {
case "UP":
print("UP")
player.childNode(withName: "ONE")?.physicsBody?.applyImpulse(CGVector(dx: 60, dy: 0))
case "DOWN":
print("DOWN")
player.childNode(withName: "ONE")?.physicsBody?.applyImpulse(CGVector(dx: -60, dy: 0))
case "STOP":
print("STOPPED")
player.childNode(withName: "ONE")?.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
default:
break
}
}
}
This is My InterfaceController.swift:
class InterfaceController: WKInterfaceController, WKCrownDelegate {
#IBOutlet var skInterface: WKInterfaceSKScene!
private var moveDirection = ""
private var game = GameScene()
private var player = GameScene()
override func awake(withContext context: Any?) {
super.awake(withContext: context)
crownSequencer.delegate = self
crownSequencer.focus()
// Configure interface objects here.
// Load the SKScene from 'GameScene.sks'
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
self.skInterface.presentScene(scene)
crownSequencer.delegate = self
crownSequencer.focus()
// Use a value that will maintain a consistent frame rate
self.skInterface.preferredFramesPerSecond = 30
}
}
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
if rotationalDelta > 0{
moveDirection = "UP"
game.moveSprite(player: player.player, moveDirection: moveDirection)
}else if rotationalDelta < 0{
moveDirection = "DOWN"
game.moveSprite(player: player.player, moveDirection: moveDirection)
}
}
func crownDidBecomeIdle(_ crownSequencer: WKCrownSequencer?) {
moveDirection = "STOP"
game.moveSprite(player: player.player, moveDirection: moveDirection)
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}

Welcome to SO!
Ok, there is a lot to unpack so get some popcorn... I think that you are on the right track here, mostly you need to check for nil when you get errors.
First, this is wrong in your interface controller, and the part that concerned me. Here, you are just instantiating new GameScene instances, which are completely separate from the gameScene instance created by your interface controller a few lines down. Then, you were sending the crown delegate functions to these totally empty gameScenes.:
private var game = GameScene() // You are referencing nothing here, just creating a new gamescene.
private var player = GameScene() // I don't think that player is supposed to be a gamescene!
I fixed it by doing assigning the actual gameScene you want to use to the properties (so they can be used by the crown delegate).
private var game: GameScene!
lazy private var player: SKSpriteNode = self.game.player
override func awake(withContext context: Any?) {
// ... Stuff...
if let scene = GameScene(fileNamed: "GameScene") {
game = scene
This was also changed to represent the new code in your crown delegates:
game.moveSprite(player: player, moveDirection: moveDirection)
In addPlayer you were doing this:
player.physicsBody?.isDynamic = true // This needs to go AFTER you init your pb.
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
...and I fixed it by swapping the lines.
Personally I like to do the following, to help ensure no small mistakes are made:
let pb = SKPhysicsBody(...)
pb.isDynamic = true
player.physicsBody = pb
moveSprite had a bunch of issues with it, so I'm not going to enumerate them as I did above. Check out what I did then ask me if you have any questions. Basically, you were doomed with this func from the start, because you were calling this method from the interface controller with the whacked out player values that were all nil.
Also, the .applyImpulse was giving me pretty bad controls, so I changed it to a plain adjustment of .position. There is still a small issue with coasting before the player stops, but that can be handled in another question :) (note, I only tested this on simulator.. may not be an issue on-device).
Also also, I hate errors caused by spelling mistakes in strings, so I converted this to an enum for you.
func moveSprite(player : SKSpriteNode, moveDirection: Direction) {
// This will give us an equal amount of pixels to move across the watch devices:
// Adjust this number for shorter / longer movements:
let percentageOfScreenToMovePerRotation = CGFloat(1) // One percent
let modifier = percentageOfScreenToMovePerRotation / 100
let amountToMove = self.frame.maxX * modifier
switch moveDirection {
case .UP:
player.position.x += amountToMove
case .DOWN:
player.position.x -= amountToMove
case .STOP:
break
}
}
The real moral of the story here is to check for nil. If you just use someOptional?.someMethod() all the time, then you likely will not be able to easily determine whether or not someMethod() is actually being called or not.. thus, you don't know if the problem is with the calling logic, the method, or with the object not existing, and etc.
Force unwrapping is frowned upon in production code, but IMO it is extremely valuable when first starting out--because it helps you to quickly identify errors.
Later on, you can start using things like if let and guard to help check for nil without crashing your programs, but that adds more clutter and complexity to your code when you are trying to just learn the basics of a new API and language.
And as a final tip, try to not use hard-coded strings whenever possible: put them into an enum or a constant as I have in your code:
// Because I hate string spelling erros, and you probably do too!
enum Direction {
case UP, DOWN, STOP
}
// Because I hate errors related to spelling in strings:
let names = (ONE: "ONE", TWO: "TWO")
Here are the two files in their entirety.. note, I had to comment out a few things to get it to work in my project:
GameScene:
// Because I hate string spelling erros, and you probably do too!
enum Direction {
case UP, DOWN, STOP
}
class GameScene: SKScene, SKPhysicsContactDelegate, WKCrownDelegate {
var watchParticles:SKEmitterNode!
var player: SKSpriteNode!
var player2: SKSpriteNode!
var playerPosition:CGPoint!
// Because I hate errors related to spelling in strings:
let names = (ONE: "ONE", TWO: "TWO")
func addPlayer() {
player = SKSpriteNode(color: .blue, size: CGSize(width: 50, height: 50))
// player = SKSpriteNode(imageNamed: "Spaceship")
// player.setScale(0.15)
player.position = CGPoint(x: 5, y: -60)
player.name = names.ONE
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
player.physicsBody!.isDynamic = true // This was placed *before* pb initialzier (thus never got called)
player2 = SKSpriteNode(color: .yellow, size: CGSize(width: 50, height: 50))
// player2 = SKSpriteNode(imageNamed: "Spaceship")
// player2.setScale(0.15)
player2.position = CGPoint(x: 5, y: -60)
player2.name = names.TWO
player2.physicsBody = SKPhysicsBody(rectangleOf: player2.size)
player2.physicsBody!.isDynamic = false // This was placed *before* pb initialzier (thus never got called)
addChild(player)
addChild(player2)
playerPosition = player.position
}
override func sceneDidLoad() {
self.scaleMode = SKSceneScaleMode.aspectFill
//watchParticles = SKEmitterNode(fileNamed: "watchParticles")
//addChild(watchParticles)
self.physicsWorld.gravity = CGVector.zero
physicsWorld.contactDelegate = self
addPlayer()
}
func moveSprite(player : SKSpriteNode, moveDirection: Direction) {
// This will give us an equal amount of pixels to move across the watch devices:
// Adjust this number for shorter / longer movements:
let percentageOfScreenToMovePerRotation = CGFloat(1) // One percent
let modifier = percentageOfScreenToMovePerRotation / 100
let amountToMove = self.frame.maxX * modifier
switch moveDirection {
case .UP:
player.position.x += amountToMove
case .DOWN:
player.position.x -= amountToMove
case .STOP:
break
}
}
}
InterfaceController:
class InterfaceController: WKInterfaceController, WKCrownDelegate {
#IBOutlet var skInterface: WKInterfaceSKScene!
private var moveDirection = Direction.STOP
private var game: GameScene!
lazy private var player: SKSpriteNode = self.game.player
override func awake(withContext context: Any?) {
super.awake(withContext: context)
crownSequencer.delegate = self
crownSequencer.focus()
if let scene = GameScene(fileNamed: "GameScene") {
game = scene // VERY IMPORTANT!
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
self.skInterface.presentScene(scene)
crownSequencer.delegate = self
crownSequencer.focus()
// Use a value that will maintain a consistent frame rate
self.skInterface.preferredFramesPerSecond = 30
}
else {
fatalError("scene not found")
}
}
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
if rotationalDelta > 0{
moveDirection = .UP
game.moveSprite(player: player, moveDirection: moveDirection)
} else if rotationalDelta < 0{
moveDirection = .DOWN
game.moveSprite(player: player, moveDirection: moveDirection)
}
}
func crownDidBecomeIdle(_ crownSequencer: WKCrownSequencer?) {
moveDirection = .STOP
game.moveSprite(player: player, moveDirection: moveDirection)
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}

Related

GKBehavior/GKGoal causing chaos with position

I'm trying to get a GKActor to position SKNodes in a scene. I thought I had this working. However, when I add a GKBehavior to the actor, I get very erratic position changes flicking all over the place and the actors behaviour isn't working.
I'm using an entity-component architecture in my project, which is a little too complicated to show an example here, so I've created a dramatically simplified playground to illustrate the issue I'm seeing.
//: A SpriteKit based Playground
import PlaygroundSupport
import SpriteKit
import GameplayKit
extension CGPoint {
init(point: vector_float2) {
self.init(
x: CGFloat(point.x),
y: CGFloat(point.y)
)
}
}
class GameScene: SKScene, GKAgentDelegate {
private let playerAgent = GKAgent2D()
private var player : SKShapeNode!
private let enemyAgent = GKAgent2D()
private var enemy : SKShapeNode!
override func didMove(to view: SKView) {
player = SKShapeNode(circleOfRadius: 40)
player.fillColor = .green
addChild(player)
playerAgent.position.x = .random(in: -(640/2)...640/2)
playerAgent.position.y = .random(in: -(480/2)...480/2)
playerAgent.delegate = self
enemy = SKShapeNode(circleOfRadius: 10)
enemy.fillColor = .red
addChild(enemy)
enemyAgent.position.x = .random(in: -(640/2)...640/2)
enemyAgent.position.y = .random(in: -(480/2)...480/2)
enemyAgent.delegate = self
enemyAgent.behavior = GKBehavior(
goals: [
//GKGoal(toSeekAgent: playerAgent)
]
)
}
override func update(_ currentTime: TimeInterval) {
super.update(currentTime)
playerAgent.update(deltaTime: currentTime)
enemyAgent.update(deltaTime: currentTime)
}
func agentDidUpdate(_ agent: GKAgent) {
if agent == enemyAgent {
enemy.position = CGPoint(point: enemyAgent.position)
print(enemy.position)
}
if agent == playerAgent {
player.position = CGPoint(point: playerAgent.position)
}
}
#objc static override var supportsSecureCoding: Bool {
get {
return true
}
}
}
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
if let scene = GameScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
sceneView.presentScene(scene)
}
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
This Playground shows the two nodes placed where their agents are positioned. It's working how I expet. However, once the GKGoal is uncommented, the position of the enemyAgent that is printed out wildly fluxuates and doesn't move toward the playerAgent. This is not what I expeted to happen.
Clearly I'm doing somthing wrong but I can't see where i've made a mistake. I'd love some help from someone with more experince working with GameplayKit. Thank You.
I figured out the issue. The GKAgents update function takes a delta time, and the SKScenes update function provides a current time. I didn't spot this mismatch as they are both TimeIntervals and I was passing it directly with out converting it.
For anyone stumbling across this the way I converted the currentTime to a deltaTime was storing a lastUpdate like this;
var lastUpdate: TimeInterval = 0
override func update(_ currentTime: TimeInterval) {
super.update(currentTime)
defer { lastUpdate = currentTime }
guard lastUpdate != 0 else {
return
}
let deltaTime = currentTime - lastUpdate
playerAgent.update(deltaTime: deltaTime)
enemyAgent.update(deltaTime: deltaTime)
}
I hope this helps someone, as it took me far too long to spot this. I think Apple could make this easy mistake more noticeable by printing an error if the delta time is too big or something.

Save state of GameScene through transitions

I want to build an inventory for my SpriteKit game. For this I want to go to another SKScene File which represents my inventory when I press the pause button. My problem is that when I make a transition from my InventoryScene back to my GameScene, the GameScene loads completely new. This is the transition code from my GameScene class:
func loadInventory(){
if !transitionInProgress{
transitionInProgress = true
if let scene = InventoryScene(fileNamed: "Inventory"){
Globals.InventoryGlobals.levelBeforeSwitch = currentLevel
scene.scaleMode = .aspectFill
let transition = SKTransition.push(with: .down, duration: 0.5)
self.view?.presentScene(scene, transition: transition)
}
}
}
With this code I'll go to my InventoryScene.
Now in my InventoryScene I want to go back to my GameScene with this:
func loadLevel(level: String){
if !transitionInProgress{
transitionInProgress = true
if let scene = GameScene(fileNamed: level){
scene.currentLevel = level
scene.scaleMode = .aspectFill
let transition = SKTransition.doorsOpenHorizontal(withDuration: 1)
self.view?.presentScene(scene, transition: transition)
}
}
}
The transitions are working but my problem is that the GameScene loads completely new, which is obviously because I instantiate a new GameScene. So when the player is in the middle of the level and then goes to the Inventory and back to the GameScene, the player is back at the beginning of the level. If I go from the Inventory back to the scene I want to be the GameScene as it was before (player position, enemy health, etc.)
Has anyone an idea how i can do this?
Retaining your scene is very simple, all you need to do is retain the scene with a strong reference
In your ViewController, it is as simple as storing a variable
class ViewController : UIViewController
{
var gameScene = GameScene(fileNamed:"GameScene")
}
Now for as long as your view controller is alive, your scene will be alive.
To access it, you just need to find a way to tell the MenuScene where your view controller is, then present the scene.
class ViewController : UIViewController
{
var gameScene = GameScene(fileNamed:"GameScene")
lazy var skView : SKView = self.view as! SKView
func gotoMenu()
{
let menu = MenuScene(fileNamed"MenuScene")
menu.viewController = self
skView.presentScene(menu)
}
}
class MenuScene : SKScene
{
var viewController : ViewController!
func returnToGame()
{
view.presentScene(viewcontroller.gameScene)
}
}
But, what if you don't want to use custom SKScene classes all the time, use a view controller, or would rather rely on components, why isn't there a convenient way to go back to a scene.
Well my friend, there is, and it is where userData comes into play
class GameScene : SKScene
{
func gotoMenu()
{
let menu = MenuScene(fileNamed:"MenuScene")
menu.userData = menu.userData ?? ["":Any]()
menu.userData["backToScene"] = self
view.presentScene(menu)
}
}
class MenuScene : SKScene
{
func returnToGame()
{
guard let userData = userData, let scene = userData["backToScene"] as? SKScene
view.presentScene(scene)
}
}
Since we are retaining it in the user data, we can now present the old scene anywhere we have access to the menu scene.
userData is also great in transferring inventory, of course I would create a class to manage the inventory, and just pass the reference via userData
Now, to create a menu that overlays the current scene, that is as simple as applying a new node onto your scene.
You can even use a separate SKS file to layout your menu, and overlay it:
class GameScene : SKScene
{
let menu = MenuScene(fileNamed:"MenuScene")
func overlayMenu()
{
scene.addChild(menu) //You probably want to add an SKCameraNode, and add it to the camera instead
}
override func update(currentTime: CFTimeInterval)
{
if menu.parent != nil
{
menu.update(currentTime:currentTime) //do this only when you need to have a constant update call, be sure to include additional functionality like `didFinishUpdate` in the approprate functions when needed
}
}
}
Of course now would be a good time to develop what is called a worldNode, also may be referred to as gameNode
Essentially what this node is, is the node that holds all your game elements.
This allows you to add overlay nodes that can pause your game.
Your scene hierarchy would like like this:
SKScene
--worldNode
----all nodes that belong in the game
--menuNode
----all nodes that belong on the menu
Now at any time, menu can set the worldNode's isPaused state to true, allowing the game to pause and still giving you the ability to interact with the menuNode
I do layover windows all the time in my Spritekit games, and it doesn't have to be as complicated as you are thinking. Here is how you can do it all in Spritekit without leaving the Scene.
Create a new class which is a subclass of SKSpriteNode for your InventoryDialog.
import SpriteKit
protocol InventoryDialogDelegate: class {
func close()
}
class InventoryDialog: SKSpriteNode {
private var closeButton: SKSpriteNode!
weak var delegate: InventoryDialogDelegate?
init(size: CGSize) {
super.init(texture: nil, color: .clear, size: size)
name = "inventoryDialog"
//do some background design work here
let background = SKSpriteNode(color: .white, size: self.size)
background.zPosition = 1
addChild(background)
closeButton = SKSpriteNode(texture: SKTexture(imageNamed: "closeButton"))
closeButton.position = CGPoint(x: self.size.width / 2 - closeButton.size.width / 2, y: self.size.height / 2 - closeButton.size.height / 2)
closeButton.zPosition = 2
addChild(closeButton)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
if closeButton.contains(touchLocation) {
close()
}
}
func close() {
self.delegate?.close()
}
}
inside your GameScene file
class GameScene: SKScene {
var inventoryDialog: InventoryDialog!
var openButton: SKSpriteNode!
override func didMove(to view: SKView) {
openButton = SKSpriteNode(texture: SKTexture(imageNamed: "openButton"))
openButton.position = CGPoint(x: self.size.width / 2 - closeButton.size.width / 2, y: self.size.height / 2 - closeButton.size.height / 2)
openButton.zPosition = 2
addChild(openButton)
}
func displayInventoryDialog() {
backgroundBlocker = SKSpriteNode(imageNamed: "background3")
backgroundBlocker.size = self.size
backgroundBlocker.zPosition = 4999
addChild(backgroundBlocker)
inventoryDialog = InventoryDialog(size: CGSize(width: 500, height: 800))
inventoryDialog.delegate = self
inventoryDialog.zPosition = 5000
addChild(inventoryDialog)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//pause any action that you don't want running while the dialog is open
gameLayer.isPaused = true
let touch = touches.first
let touchLocation = touch!.location(in: self)
if openButton.contains(touchLocation) {
displayInventoryDialog()
}
}
}
//MARK: - InventoryDialogDelegate Methods
extension GameScene: InventoryDialogDelegate {
func close() {
//at this point you could update any GUI nesc. based on what happened in your dialog
backgroundBlocker.removeFromParent()
inventoryDialog?.removeFromParent()
gameLayer.isPaused = false
}
}
here is exactly what you wanted.
class GameScene: SKScene {
class InventoryManager: UIView {
override init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
//here is where you set up anything you want to display inside the view
self.backgroundColor = UIColor.green
let Button = UIButton()
Button.frame = CGRect(x: 0, y: 0, width: 40, height: 20)
Button.backgroundColor = UIColor.red
let TouchView = UIView()
TouchView.frame = self.frame
Button.center.x = TouchView.center.x
Button.center.y = TouchView.center.y
TouchView.backgroundColor = UIColor.brown
self.addSubview(TouchView)
//make sure you add any objects that you want to the TouchView and not to the self
TouchView.addSubview(Button)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
var InventoryView = InventoryManager()
var Node = SKSpriteNode()
override func didMove(to view: SKView) {
//this keeps the view centered in the screen
InventoryView.center.x = (self.view?.center.x)!
InventoryView.center.y = (self.view?.center.y)!
Node = SKSpriteNode(color: UIColor.blue, size: CGSize(width: 40, height: 40))
Node.position = CGPoint(x: self.frame.size.width / 3, y: self.frame.size.height / 3)
self.addChild(Node)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if !(self.view?.frame.contains(location))! {
InventoryView.removeFromSuperview()
}
if Node.contains(location) && InventoryView.isDescendant(of: self.view!) {
self.view?.addSubview(InventoryView)
}
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
clicking the blue button adds it to the scene again and clicking anywhere else removes it from the scene

Swift/SceneKit - best practice to create menu / level select scenes for the game

I'm quite new to iOS level development, currently trying to create my first SceneKit game - however, i'm not sure what approach should i use to create the "non-3D" pages like menu, level select page, settings etc.
I currently have one GameViewController, and i use empty SCNScene with overlaySKScene to display my SpriteKit menu. Upon clicking the play button in menu i transition to another empty SCNScene where i would like to display list of levels. Should i again use ovelaySKSCene, is this the correct approach?
I also noticed that even if i set scnView.overlaySKScene = nil before transitioning to levelScene , if i tap in the spot of the previous placement of pla button, the nodePressed in overlayScene still contains the playButton node..
Forgive me for the rookie questions, i haven't been able to find a good example of this type of scenarios so far to learn from.
My code so far:
import UIKit
import QuartzCore
import SceneKit
import SpriteKit
class GameViewController: UIViewController {
var scnView: SCNView {
let scnView = view as! SCNView
scnView.backgroundColor = UIColor.init(displayP3Red: 0.98, green: 0.64, blue: 0.04, alpha: 1)
return scnView
}
var menuScene: SCNScene!
var levelsScene: SCNScene!
var ground: SCNNode!
var light: SCNNode!
var scnScene: SCNScene!
var cameraNode: SCNNode!
var brickNode: SCNNode!
var selectedBrickNode: SCNNode!
var bricksArray : [SCNNode] = []
var controls: SKScene!
var levels: SKScene!
override func viewDidLoad() {
super.viewDidLoad()
setupView()
showMenu()
setupCamera()
}
override var shouldAutorotate: Bool {
return true
}
override var prefersStatusBarHidden: Bool {
return true
}
func setupView() {
scnView.showsStatistics = true
scnView.allowsCameraControl = false
scnView.autoenablesDefaultLighting = true
}
func showMenu() {
menuScene = SCNScene( )
scnView.scene = menuScene
self.controls = Controls(size: self.view.bounds.size)
self.controls.isUserInteractionEnabled = false
scnView.overlaySKScene = self.controls
}
func showLevels(){
levelsScene = SCNScene()
self.levels = Levels(size: self.view.bounds.size)
self.levels.isUserInteractionEnabled = false
scnView.overlaySKScene = self.levels
let transition = SKTransition.push(with: .left, duration: 1.0)
self.scnView.present(levelsScene
, with: transition, incomingPointOfView: nil, completionHandler:nil)
}
func setupCamera(){
cameraNode = SCNNode()
let camera = SCNCamera()
camera.zFar = 10000
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: 0, y: 10, z: 20)
let constraint = SCNLookAtConstraint(target: ground)
constraint.isGimbalLockEnabled = true
cameraNode.constraints = [constraint]
let ambientLight = SCNLight()
ambientLight.color = UIColor.darkGray
ambientLight.type = SCNLight.LightType.ambient
cameraNode.light = ambientLight
menuScene.rootNode.addChildNode(cameraNode)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch began ")
for touch: UITouch in touches {
let location = touch.location(in: scnView.overlaySKScene!)
let nodePressed = self.controls.atPoint(location)
print(nodePressed.name)
if (nodePressed.name == "playButton") {
scnView.overlaySKScene = nil
showLevels()
}
}
}
}
“the nodePressed in overlayScene still contains the playButton node...”
You are setting the pointer to the overlay scene to nil, and in touchesbegan you get the location from the overlay scene but then you use atPoint on the skscene stored in the “controls” property, so it still finds the play button. So replace self.controls with scnView.overlaySKScene in touchesbegan.
That said, I don’t know what in general is considered best practice by others but personally, once I need more than just a couple of buttons, I stop using the spritekit overlay scene and instead use regular UIKit elements to build menus on top of the SCNView.

Passing a string between SKScenes using UserDefaults

In a game I'm developing with Swift, I want the player to be able to choose a background in a shop-like scene and thus change the background of every SKScene. I'm trying to accomplish this using UserDefaults, but for some reason it isn't working. Here's the important code of the Shop Scene (I removed the irrelevant code):
import SpriteKit
class ShopScene: SKScene {
var backNumber = 90
var backRemainder = 0
var background = SKSpriteNode()
var backName:String = "back1"
override func didMove(to view: SKView) {
background.texture = SKTexture(imageNamed: "\(backName)")
background.size = self.size
self.addChild(background)
let nextButton: NButton = NButton(defaultButtonImage: "next", activeButtonImage: "nextP", buttonAction: nextAction)
addChild(nextButton)
let selectButton: SButton = SButton(defaultButtonImage: "next", activeButtonImage: "nextP", buttonAction: selectAction)
addChild(selectButton)
}
func selectAction() {
UserDefaults.standard.set(backName, forKey: "backSaved")
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let sceneTransition = SKTransition.fade(withDuration: 0.4)
self.view!.presentScene(sceneToMoveTo, transition: sceneTransition)
}
func nextAction() {
backNumber += 1
backRemainder = backNumber % 3
switch backRemainder {
case 0:
backName = "back1"
case 1:
backName = "back2"
case 2:
backName = "back3"
default:
backName = "back1"
}
background.texture = SKTexture(imageNamed: "\(backName)")
}
}
As you can see, when the select button is pressed backName is saved. Now, this is the relevant code of the Main Menu Scene:
Import SpriteKit
class MainMenuScene: SKScene {
var backName = UserDefaults.standard.string(forKey: "backSaved")
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "\(backName)")
background.size = self.size
self.addChild(background)
When the 'Select' button is pressed, you should transition to Main Menu Scene and see that the background is the one you selected. However, I get a red X in a white background when I run this. I've worked before with UserDefaults to save scores, but I can't figure out why it's not working in this case. Any idea on how to pass strings between SKScenes using UserDefaults? Am I doing something wrong?
NOTE: Although Knight0fDragon's answer is marked correct, his answer works for passing a string between scenes but not for saving that value permanently. In you wish to pass the value and save it, check my answer.
I would recommend not doing UserDefaults, that is meant for preferences to your application. Instead use userData
func selectAction() {
let sceneToMoveTo = MainMenuScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
sceneToMoveTo.userData = sceneToMoveTo.userData ?? NSMutableDictionary() //This lets us ensure userdata exists
sceneToMoveTo.userData!["backSaved"] = backName
let sceneTransition = SKTransition.fade(withDuration: 0.4)
self.view!.presentScene(sceneToMoveTo, transition: sceneTransition)
}
Then implement it with:
import SpriteKit
class MainMenuScene: SKScene {
lazy var backName:String = {return self.userData?["backSaved"] as? String ?? "back1"}() //This allows us to load backName at the time it is needed, or assign back1 if doesn't exist
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: backName)
self.addChild(background)
Note, I am not sure if the as? String is needed, try it without it and see if Swift can infer it
Apparently, backName behaves as an optional because it can have no value, thus being nil. To make my code work I made these changes to Shop Scene:
import SpriteKit
class ShopScene: SKScene {
var backName:String? = UserDefaults.standard.string(forKey: "backSaved")
override func didMove(to view: SKView) {
if backName != nil {
backName = UserDefaults.standard.string(forKey: "backSaved")
} else {
backName = "back1"
}
background.texture = SKTexture(imageNamed: "\(backName!)")
self.addChild(background)
and these changes to Main Menu Scene:
import SpriteKit
class MainMenuScene: SKScene {
var backName:String? = UserDefaults.standard.string(forKey: "backSaved")
override func didMove(to view: SKView) {
if backName != nil {
backName = UserDefaults.standard.string(forKey: "backSaved")
} else {
backName = "back1"
}
let background = SKSpriteNode(imageNamed: "\(backName!)")
self.addChild(background)

Collision detection between two sprites not working

I am trying to detect when the player object collides with the other objects in my game. This is my current code:
import SpriteKit
class GameScene: SKScene {
let player = SKSpriteNode(imageNamed: “Box”)
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.whiteColor()
player.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(player)
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(addObject),
SKAction.waitForDuration(1)
])
))
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(addSecondObject),
SKAction.waitForDuration(1)
])
))
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
player.position = location
}
}
func EndGame() {
println("GAME OVER")
}
func Collision() {
if (CGRectIntersectsRect(player.frame, object.frame )) {
[EndGame];
}
if (CGRectIntersectsRect(player.frame, object1.frame)) {
[EndGame];
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
func addObject() {
let object = SKSpriteNode(imageNamed: "object1”)
object.name = "object1”
object.position = CGPoint(x: size.width/4, y: size.height/4)
self.addChild(object)
}
func addSecondObject() {
let object = SKSpriteNode(imageNamed: "object2”)
object.name = "object2”
object.position = CGPoint(x: size.width/2, y: size.height/2)
self.addChild(object)
}
}
So you can see my collision code is this:
func Collision() {
if (CGRectIntersectsRect(player.frame, object.frame )) {
[EndGame];
}
if (CGRectIntersectsRect(player.frame, object1.frame)) {
[EndGame];
}
}
The problem is that because object and object 1 variables are private to func (addObject) and func (addSecondObject), I can't call them in the above code. When they collide, currently I just want EndGame() to run which prints "Game Over" in the console.
I don't know if the method I have taken for collision detection is correct, but any help would be great! Thanks :)
For very basic sprites, yes, its right. You can imagine a diagonal line image "/" would trigger a collision if the top left corner of it overlapped with something, even though the actual object doesn't overlap.
Apple has a nice page discussing collisions here : https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/CodeExplainedAdventure/HandlingCollisions/HandlingCollisions.html
Also, numerous youtube videos talk about how to get this sort of thing working. eg : https://www.youtube.com/watch?v=dX0KvKtc3_w
You need to make it so the collision detection code runs(possible on a timer or something) and then, perhaps, pass in the objects you want to check or make those objects members of the class (IBOutlet) or similar strategy to gain access to them.
You have to use SKPhysicsbody:
struct PhysicsCategory {
static let None : UInt32 = 0
static let Player : UInt32 = 0b1
static let Object : UInt32 = 0b10
}
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size) // 1
player.physicsBody?.dynamic = true // 2
player.physicsBody?.categoryBitMask = PhysicsCategory.Player// 3
player.physicsBody?.contactTestBitMask = PhysicsCategory.Object//4
player.physicsBody?.collisionBitMask = PhysicsCategory.None // 5
You set the SKPhysicsBody and make it the size of your player-object
Then you make it dynamic. That the physicsbody is dynamic.
That is the categoryBitMask. It's like the identifier of your SKNode. So that other SKNodes can register on you and say: "Hey I want to know if I touch that guy".
That's the contactTestbitMask which handles which SKNodes should interact with you. So you say "If that node touches me, say something".
What should happen, if this node collides.
For more informations and a nice starter tutorial, use raywenderlichs tutorial.

Resources