MiguelSlv
MiguelSlv

Reputation: 15113

SCNScene - Not rendering changes to SKNode made on touchesBegan handler

I'm subclassing SKNode and implemening the touchbegan handler.

When user clicks on the node, the node position is changed but the node does not move on screen.

I guess i need to force rendering of the SKScene somehow.

To test i did the following minor changes to the template for games of xcode6. Here the changes to the template:

GameViewControler.swift

 let HUD = SKScene(fileNamed: "HUD.sks") 
 scnView.overlaySKScene = HUD

 var customNode = CustomNode()
 customNode.text = "touch here"
 customNode.position = CGPointMake(200, 200);
 HUD.addChild(customNode)

New class CustomeNode.swift

import Foundation
import SpriteKit

public class CustomNode:SKLabelNode{

public override init() {
    super.init();
    userInteractionEnabled = true;
}

public required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

public override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    position.y = (position.y + 100) % 300 + 100; //does not update on screen
}  

}

If add back the action for the ship it works:

let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!
    ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))

how to force rendering?

UPDATE To clarify the complete code of GameViewControler.swift, removing everthing that is not needed.

import UIKit
import QuartzCore
import SceneKit
import SpriteKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // create a new scene
        let scene = SCNScene(named: "art.scnassets/ship.dae") //comes with XCode6 Game Project Template.

        // create and add a camera to the scene
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        scene?.rootNode.addChildNode(cameraNode)

        // place the camera
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

         // retrieve the SCNView
        let scnView = self.view as SCNView

        // set the scene to the view
        scnView.scene = scene

        // allows the user to manipulate the camera
        scnView.allowsCameraControl = true

        // configure the view
        scnView.backgroundColor = UIColor.blackColor()

        let HUD = SKScene(size: self.view.bounds.size)
        scnView.overlaySKScene = HUD
        scnView.overlaySKScene.userInteractionEnabled = true;

        var customNode = CustomNode()
        customNode.text = "touch here"
        customNode.position = CGPointMake(200, 200);
        HUD.addChild(customNode)

        //let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!
        //ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))
    }
}

Upvotes: 0

Views: 1471

Answers (3)

Simonas Karužas
Simonas Karužas

Reputation: 1

I solved similar problem using:

self.gameView!.playing = true;
self.gameView!.loops = true;

Upvotes: 0

MiguelSlv
MiguelSlv

Reputation: 15113

simple add at viewDidLoad bottom:

scnView.play(nil)

Upvotes: 1

xXx
xXx

Reputation: 1

[scene.view setNeedsDisplay:YES] or [node.scene.view setNeedsDisplay:YES]

Upvotes: 0

Related Questions