我已在我的Sprite Kit游戏中添加SKView
作为subview
来显示游戏节点和内容。
let skView = SKView()
class GameViewController: UIViewController, CLLocationManagerDelegate {
self.view.addSubview(skView)
SKView
被添加为subview
,而不是Sprite Kit项目中建议的默认方式(下面的评论代码),因为我还有一个MKMapView
显示在大多数是透明SKView
,否则MapView
会遮挡游戏场景。
//let skView = self.view as! SKView
然后在SKView上展示游戏场景:
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
skView.allowsTransparency = true
skView.showsFPS = false
skView.showsNodeCount = false
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
scene.backgroundColor = .clearColor()
skView.presentScene(scene)
}
在主游戏场景中,我有一个主世界节点,我在一个单独的函数中添加了一堆字符节点,在场景出现时调用(下面的代码)。此功能还将旋转和移动动作应用于角色节点。如果我使用默认的Sprite Kit方法(SKView
)设置呈现场景的let skView = self.view as! SKView
,这可以很好地工作,但是如果我将呈现场景作为subview
添加到视图控制器中(let skView = SKView()
和self.view.addSubview(skView)
),然后动作无法正常工作 - 角色节点抽搐而不是正确旋转,根本不动。
任何人都可以帮助我理解为什么当场景出现在SKView
视图控制器的subview
时,动作似乎无法正常工作? (但是SKView
设置为self.view as! SKView
时功能正常吗?我已经用hasActions()
(他们这样做)检查了节点,并且所有内容似乎都在正确的视图层次结构中,所以我有点难过。
我正在运行最新的iOS 9.0测试版和Xcode 7.0测试版。
感谢。
func moveCharacters() {
if characterDic.count > 0 {
for i in 1...characterDic.count {
if let characterKey = characterDic["P" + "\(i)"] {
let characterStatus = Int(characterKey["active"] as! NSNumber)
if characterStatus == 1 || characterStatus == 2 {
if worldNode.childNodeWithName("P" + "\(i)") != nil {
worldNode.childNodeWithName("P" + "\(i)")?.removeFromParent()
}
let characterXPos = CGFloat(characterDic["P" + "\(i)"]!["curX"] as! Double)
let characterYPos = CGFloat(characterDic["P" + "\(i)"]!["curY"] as! Double)
let characterTexture = SKTexture(imageNamed: "character1.png")
let character = SKSpriteNode(texture: characterTexture)
character.position = CGPoint(x: characterXPos, y: characterYPos)
character.xScale = 0.2
character.yScale = 0.2
worldNode.addChild(character)
/////////
let characterXDest = CGFloat(characterDic["P" + "\(i)"]!["destX"] as! Double)
let characterYDest = CGFloat(characterDic["P" + "\(i)"]!["destY"] as! Double)
let destLoc = CGPoint(x: characterXDest, y: characterYDest)
let distToGo = sqrt(pow((characterXDest - characterXPos),2) + pow((characterYDest - characterYPos),2))
let moveNode = SKAction.moveTo(destLoc, duration: Double(distToGo/50))
let angle = atan2(((destLoc.y - (character.position.y))), ((destLoc.x - (character.position.x))))
let Pi = CGFloat(M_PI)
let DegreesToRadians = Pi / 180
let rotate = SKAction.rotateToAngle(angle - 90 * DegreesToRadians, duration: 0)
character.runAction(rotate)
character.runAction(moveNode)
}
}
}
}
}