Reputation: 1589
I want to add a specific number of squares to a scene. When I do this a part of the square is outside the view. Is it possible to move the whole scene on the y-axis?
Upvotes: 2
Views: 811
Reputation: 5461
I tried and it looks like the scene itself can't be moved. An alternitive approach is to add an SKNode to the scene which represents the whole world. Add all other elements to the world node. Now you can move this node if you want to move the whole scene. I'm working on a short tutorial about that in my Blog. Here's my current code:
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 */
}
}
Upvotes: 1