1

I have two background images: background.png (1920 x 1280) and background@2x.png (3840 x 2560). Also, app works only on landscape mode.

At run time the size of my background image is at its max.

PROBLEM: I try to print to the console the height and with of my background image using the pinch gesture method but on each print, both values remain at 4001.999.. x 2668.0, even though, the size of my background image does gets resize while pinching in and out.

What I'm trying to accomplish is: Access the width and height of my SKSpriteNode containing my background image so that I can check if its height is equal to that of the screen. If it is, it disables the effect of the pinch gesture of further zoom in. I would also like to manage the same effect but for zooming out.

If someone can suggest a better algorithm for doing this, please, feel free to share. Thanks!!

This is my code:

    // Background
    backgroundNode = createBackground()
    self.addChild(backgroundNode)
    println("anchorPoint = \(self.anchorPoint)")

}

override func didMoveToView(view: SKView) {

    let pinchRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("pinchView:"))
    pinchRecognizer.delegate = self
    view.addGestureRecognizer(pinchRecognizer)

}

// NODES

func createBackground() -> SKNode {

    let backgroundNode = SKNode()
    let node = SKSpriteNode(imageNamed: "background/background")
    node.name = "bg"
    node.setScale(scaleFactor)

    backgroundNode.addChild(node)

    return backgroundNode

}

// GESTURES

func pinchView(sender: UIPinchGestureRecognizer) {

    let minScale: CGFloat = 0.15
    let maxScale: CGFloat = 3
    let tempScale = backgroundNode.xScale * sender.scale

    let pinch: SKAction = SKAction.scaleBy(sender.scale, duration: 0.0)

    if tempScale > minScale && tempScale < maxScale {

        backgroundNode.runAction(pinch)
        sender.scale = 1.0

    }

    var width = backgroundNode.childNodeWithName("bg")?.self.frame.size.width
    var height = backgroundNode.childNodeWithName("bg")?.self.frame.size.height
        println("width: \(width)") // 4001.999...
        println("height: \(height)") // 2668.0

}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

    if touches.count == 1 {

        for touch: AnyObject in touches {

            let prevLoc: CGPoint = touch.previousLocationInView(view)
            let curentLoc: CGPoint = touch.locationInView(view)

            let deltaX: CGFloat = curentLoc.x - prevLoc.x
            let deltaY: CGFloat = curentLoc.y - prevLoc.y

            backgroundNode.position.x += deltaX
            backgroundNode.position.y -= deltaY

        }

    }

}
Zapato33
  • 53
  • 8
  • If I understand correctly, you're getting a scaling effect, but the size of the sprite node is not changing. This appears to be because you're scaling the sprite's parent node rather than the sprite itself. – Kevin Owens May 29 '15 at 19:56
  • Yes, I'm trying to scale the background image. If I change the width and height in pinchView method to: var width = backgroundNode.frame.size.width, var height = backgroundNode.frame.size.height, the result is 0.0 for every print. I'm very confused about this. I have a SKNode backgroundNode that contains the SKSpriteNode node that has the image 'background'. Why are these values so off? – Zapato33 May 29 '15 at 20:05
  • Look at "Characteristics of Nodes" in the SKNode documentation for a discussion of a node's frame. Your background node doesn't itself have any visual content, so its frame size will remain (0, 0) (see also calculateAccumulatedFrame). It's the sprite that has content; try scaling it instead and see if its size doesn't update accordingly. – Kevin Owens May 29 '15 at 21:26
  • @kevinOwens thanks for your help. I assumed that if I scaled the parent node, the child node's width and height would be changed as well. I've fixed it and is working better now. :) – Zapato33 May 30 '15 at 01:41
  • You're welcome ... something I learned through my own trial and error. – Kevin Owens May 30 '15 at 02:13

1 Answers1

1

The answer discussed in the comments to the question is to scale the sprite node itself, rather than its parent SKNode. Since the SKNode does not have any visual content (see Apple's discussion of the SKNode's frame property under "Characteristics of Nodes"), its frame size will be (width: 0, height: 0). Note that should you need the bounding rectangle for all visual nodes within an SKNode, the calculateAccumulatedFrame() method is available.

Kevin Owens
  • 538
  • 5
  • 12