blue
blue

Reputation: 7395

Xcode 6 Swift: place 2 blocks on either side of the screen?

I need to place 2 blocks on either side of the screen in the upper left and right hand corners. This seems simple and I've made several attempts, yet the blocks are no where to be seen. I've tried using both CGPoint and CGPointMake and just using number coordinates but the blocks are not appearing in the GameScene.

These blocks are going to be moving down in position later. I initialize everything here:

class GameScene: SKScene {

var savior = SKSpriteNode()

var block1Texture = SKTexture()
var block2Texture = SKTexture()
var blocksMoveRemove = SKAction()
let blockGap = 150
var block1 = SKSpriteNode()
var block2 = SKSpriteNode()

Here is where I need to set the position of the blocks:

  //Create blocks

    block1Texture = SKTexture(imageNamed: "block1")
    block2Texture = SKTexture(imageNamed: "block2")
    block1 = SKSpriteNode(texture: block1Texture)
    block2 = SKSpriteNode(texture: block2Texture)

    block1.setScale(1)
    block2.setScale(1)
    block1.position = CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.1)
    block2.position = CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.1)

    self.addChild(block1)
    self.addChild(block2)

Upvotes: 0

Views: 72

Answers (1)

Mundi
Mundi

Reputation: 80271

You need to add the blocks to your node with addChild:.

More remarks:

You do not have to initialize your variables when declaring them if you anyway intend to overwrite them. Instead, use force unwrapped variables.

var block1 : SKSpriteNode!

If you want the blocks in the upper corners, your y should be the height times 0.1, not 0.9.

Also, you might not see anything because the image is not found (check the name) or does not contain visible elements.

Also, note that the frame property might have an unexpected value. From the docs:

The frame is the smallest rectangle that contains the node’s content, taking into account the node’s xScale, yScale, and zRotation properties. Not all nodes contain content of their own.

Upvotes: 1

Related Questions