存在SK3DNode时SKShapeNode未显示

时间:2017-06-28 14:22:27

标签: ios swift sprite-kit scenekit skshapenode

当场景中存在SK3DNode时,我遇到显示SKShapeNode的问题。在添加SK3DNode之前将SKShapeNode添加到场景中时,SKShapeNode显示正常但在SK3DNode之后添加SKShapeNode,SKShapeNode不会显示。

此外,如果SK3DNode被隐藏,例如通过点击屏幕,之后添加的SKShapeNodes可见。有谁有这种问题?下面是GameScene.swift的代码我使用的是两个由路径定义的SKShapeNodes和两个圆圈以及一个SK3DNode。当用户点击屏幕时,SK3DNode会隐藏/可见,因此您可以复制我上面解释的问题。

请注意,问题仅出现在iOS 10.3.2的真实设备上(在iPhone 7上测试过)。在模拟器上,一切似乎都像魅力一样,iPhone 4S与iOS 9.3一样。

import SpriteKit
import GameplayKit
import SceneKit

class GameScene: SKScene {

    var cylinder: SK3DNode!

    override func didMove(to view: SKView) {
        let label = SKLabelNode()
        label.text = "test"
        label.fontSize = 40
        label.position = CGPoint(x: 0, y: 100)
        self.addChild(label)

        self.addLine(yPosition: self.frame.height/4, color: SKColor.green)
        self.addCircle(xPosition: self.frame.width/4)
        self.addCylinder()
        self.addLine(yPosition: -self.frame.height/4, color: SKColor.blue)
        self.addCircle(xPosition: -self.frame.width/4)
    }

    private func addLine(yPosition: CGFloat, color: SKColor) {
        let bezierPath = UIBezierPath()
        let startPoint = CGPoint(x: -self.frame.width + 10, y: 0)
        let endPoint = CGPoint(x: self.frame.width - 10, y: 0)
        bezierPath.move(to: startPoint)
        bezierPath.addLine(to: endPoint)

        let shapeNode = SKShapeNode(path: bezierPath.cgPath)
        shapeNode.position = CGPoint(x: 0, y: yPosition)
        shapeNode.strokeColor = color
        self.addChild(shapeNode)
    }

    private func addCylinder() {
        let cylinderGeometry = SCNCylinder(radius: 100, height: 40)
        let cylinderNode = SCNNode(geometry: cylinderGeometry)

        self.cylinder = SK3DNode(viewportSize: CGSize(width: 300, height: 300))
        self.cylinder.scnScene = SCNScene()
        self.cylinder.scnScene?.rootNode.addChildNode(cylinderNode)

        self.addChild(self.cylinder)
    }

    private func addCircle(xPosition: CGFloat) {
        let shapeNode = SKShapeNode(circleOfRadius: 40)
        shapeNode.position = CGPoint(x: xPosition, y: 0)
        self.addChild(shapeNode)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.cylinder.isHidden = !self.cylinder.isHidden
    }
}

0 个答案:

没有答案