1
我想为场景添加特定数量的方块。当我这样做时,广场的一部分在视野外。是否有可能在y轴上移动整个场景?SpriteKit:移动整个场景
我想为场景添加特定数量的方块。当我这样做时,广场的一部分在视野外。是否有可能在y轴上移动整个场景?SpriteKit:移动整个场景
我试过了,它看起来像场景本身不能移动。一种替代方法是将SKNode添加到代表整个世界的场景中。将所有其他元素添加到世界节点。现在,如果要移动整个场景,则可以移动此节点。我正在撰写关于我的博客的简短教程。这里是我当前的代码:
import SpriteKit
class GameScene: SKScene {
// Declare the needed nodes
var worldNode: SKNode?
var spriteNode: SKSpriteNode?
var nodeWidth: CGFloat = 0.0
override func didMoveToView(view: SKView) {
// Setup world
worldNode = SKNode()
self.addChild(worldNode!)
// Setup sprite
spriteNode = SKSpriteNode(imageNamed: "Spaceship")
spriteNode?.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
spriteNode?.xScale = 0.1
spriteNode?.yScale = 0.1
spriteNode?.zPosition = 10
self.addChild(spriteNode!)
// Setup backgrounds
// Image of left and right node must be identical
let leftNode = SKSpriteNode(imageNamed: "left")
let middleNode = SKSpriteNode(imageNamed: "right")
let rightNode = SKSpriteNode(imageNamed: "left")
nodeWidth = leftNode.frame.size.width
leftNode.anchorPoint = CGPoint(x: 0, y: 0)
leftNode.position = CGPoint(x: 0, y: 0)
middleNode.anchorPoint = CGPoint(x: 0, y: 0)
middleNode.position = CGPoint(x: nodeWidth, y: 0)
rightNode.anchorPoint = CGPoint(x: 0, y: 0)
rightNode.position = CGPoint(x: nodeWidth * 2, y: 0)
worldNode!.addChild(leftNode)
worldNode!.addChild(rightNode)
worldNode!.addChild(middleNode)
}
var xOrgPosition: CGFloat = 0
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let xTouchPosition = touch.locationInNode(self).x
if xOrgPosition != 0.0 {
let xNewPosition = worldNode!.position.x + (xOrgPosition - xTouchPosition)
if xNewPosition <= -(2 * nodeWidth) {
// right end reached
worldNode!.position = CGPoint(x: 0, y: 0)
print("Right end reached")
} else if xNewPosition >= 0 {
// left end reached
worldNode!.position = CGPoint(x: -(2 * nodeWidth), y: 0)
print("Left end reached")
} else {
worldNode!.position = CGPoint(x: xNewPosition, y: 0)
}
}
xOrgPosition = xTouchPosition
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Reset value for the next swipe gesture
xOrgPosition = 0
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
会是使用像worldNode.addChild(otherNode) –
是。我可以稍后发布示例 – Stefan
是的,移动场景不是你想要做的(尽管可以完成)按照主场景中所有对象的主节点开始,然后移动主节点在场景内部移动所有节点。当你想在游戏中添加HUD时,这是一个很好的方法。您可以将主节点添加到场景中,然后将HUD节点添加到场景中,以便在移动主节点时,只有连接到主节点的对象才会移动,并且HUD中的节点将保持静态 – Knight0fDragon