Tony
Tony

Reputation: 1611

SKCropNode of SpriteKit: relationship between positions and scales?

I have started programming with SKCropNode. My purpose is to split a given image into smaller pieces (they may be not rectangles later) for a game (in which users have to move those pieces into right positions).

The code for studying is simple as the following:

    let center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)
    let imageNamed = "image1.jpg"
    let sprite1 = SKSpriteNode(imageNamed: imageNamed)
    sprite1.position = center

    let mask1 = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 400, height: 400))
    mask1.position = center

    let crop1 = SKCropNode()
    crop1.maskNode = mask1
    crop1.addChild(sprite1)

    crop1.position = CGPointZero
    self.addChild(crop1)

The code runs well and show a cropped image and I could understand when changing few things such as size or a position, say of sprite1.

However, I totally got lost when changing few positions of sprite1, mask1 and crop1, specially when adding scale to sprite1 or crop1. For example, if the given image is scaled down into 0.35 then divided into 5 x 5 pieces, I don't know how get/display the piece {0, 0} or {4, 3} then display them in original positions. In other words, I cannot control the content and position of the crop.

Can someone give me a tip or a short tutor about the relationship between those positions (of sprite1, mask1 and crop1) including their scales, regarding the purpose of the game (e.g., I don't need to scale everything but scale and position only what is needed)? Or is there any other simpler way to implement?

Upvotes: 1

Views: 138

Answers (1)

Tony
Tony

Reputation: 1611

I have to answer myself. The answer here is not about the relationship between positions, scales of SKCropNode, SKSpriteNode and masks since it is still complicated for me, plus class SKCropNode is not subclassed of SKSpriteNode thus it is harder to program their instances later. It may be the solution for the problem: split a given image into smaller ones.

The solution is quite simple, create smaller SKTexture from whole one:

let wholeTex = SKTexture(imageNamed: "myimage.jpg")
let tex1 = SKTexture(rect: CGRectMake(0.25, 0.25, 0.5, 0.5), inTexture: wholeTex)
let sprite1 = SKSpriteNode(texture: tex1)

Upvotes: 0

Related Questions