I've added buttons and SKCameraNode for my player. I got problem. When player moving left/right/jump all buttons dont follow for him.
UPDATE
class GameScene: SKScene, SKPhysicsContactDelegate {
var player: SKSpriteNode?
let buttonDirUp = SKSpriteNode (imageNamed: "button_dir_up")
let buttonDirLeft = SKSpriteNode (imageNamed: "button_dir_left")
let buttonDirDown = SKSpriteNode (imageNamed: "button_dir_down")
let buttonDirRight = SKSpriteNode (imageNamed: "button_dir_right")
var pressedButtons = [SKSpriteNode]()
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -5)
userInteractionEnabled = true
player = self.childNodeWithName("player") as? SKSpriteNode
buttonDirUp.position = CGPoint(x: 100, y: 150)
buttonDirUp.zPosition = 2
buttonDirUp.setScale(2.0)
buttonDirUp.alpha = 0.2
self.addChild(buttonDirUp)
buttonDirLeft.position = CGPoint(x: 50, y: 100)
buttonDirLeft.zPosition = 2
buttonDirLeft.setScale(2.0)
buttonDirLeft.alpha = 0.2
self.addChild(buttonDirLeft)
buttonDirDown.position = CGPoint(x: 100, y: 50)
buttonDirDown.zPosition = 2
buttonDirDown.setScale(2.0)
buttonDirDown.alpha = 0.2
self.addChild(buttonDirDown)
buttonDirRight.position = CGPoint(x: 150, y: 100)
buttonDirRight.zPosition = 2
buttonDirRight.setScale(2.0)
buttonDirRight.alpha = 0.2
self.addChild(buttonDirRight)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
if button.containsPoint(location) && pressedButtons.indexOf(button) == nil {
pressedButtons.append(button)
}
}
}
for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
if pressedButtons.indexOf(button) == nil {
button.alpha = 0.2
}
else {
button.alpha = 0.8
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
if button.containsPoint(previousLocation)
&& !button.containsPoint(location) {
let index = pressedButtons.indexOf(button)
if index != nil {
pressedButtons.removeAtIndex(index!)
}
}
else if !button.containsPoint(previousLocation)
&& button.containsPoint(location)
&& pressedButtons.indexOf(button) == nil {
pressedButtons.append(button)
}
}
}
for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
if pressedButtons.indexOf(button) == nil {
button.alpha = 0.2
}
else {
button.alpha = 0.8
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
if button.containsPoint(location) {
let index = pressedButtons.indexOf(button)
if index != nil {
pressedButtons.removeAtIndex(index!)
}
}
else if (button.containsPoint(previousLocation)) {
let index = pressedButtons.indexOf(button)
if index != nil {
pressedButtons.removeAtIndex(index!)
}
}
}
}
for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
if pressedButtons.indexOf(button) == nil {
button.alpha = 0.2
}
else {
button.alpha = 0.8
}
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
for touch: AnyObject in touches! {
let location = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
if button.containsPoint(location) {
let index = pressedButtons.indexOf(button)
if index != nil {
pressedButtons.removeAtIndex(index!)
}
}
else if (button.containsPoint(previousLocation)) {
let index = pressedButtons.indexOf(button)
if index != nil {
pressedButtons.removeAtIndex(index!)
}
}
}
}
for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
if pressedButtons.indexOf(button) == nil {
button.alpha = 0.2
}
else {
button.alpha = 0.8
}
}
}
override func update(currentTime: CFTimeInterval) {
if pressedButtons.indexOf(buttonDirUp) != nil {
player!.position.y += 9.0
}
if pressedButtons.indexOf(buttonDirDown) != nil {
player!.position.y -= 9.0
}
if pressedButtons.indexOf(buttonDirLeft) != nil {
player!.position.x -= 9.0
}
if pressedButtons.indexOf(buttonDirRight) != nil {
player!.position.x += 9.0
}
}
}