我正在继承SKNode并实现touchbegan处理程序。
当用户点击节点时,节点位置会更改,但节点不会在屏幕上移动。
我想我需要以某种方式强制渲染SKScene。
为了测试我对xcode6游戏的模板进行了以下微小更改。 这里是对模板的更改:
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)
新类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
}
}
如果添加回船的动作,则可以:
let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!
ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))
如何强制渲染?
更新 为了澄清GameViewControler.swift的完整代码,删除了不需要的内容。
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)))
}
}
答案 0 :(得分:1)
在viewDidLoad底部简单添加:
scnView.play(nil)
答案 1 :(得分:0)
[scene.view setNeedsDisplay:YES] 要么 [node.scene.view setNeedsDisplay:YES]
答案 2 :(得分:0)
我使用以下方法解决了类似问题:
self.gameView!.playing = true;
self.gameView!.loops = true;