How to create a rectangle in a Sprite set using Swift

I am trying to create a rectangle using swift on a Sprite set, since the rectangle should be used as an object in the scene, I assumed that I need to create a SkSpriteNode and they give it size, but it did not work, here is how I did it:

var barra = SKSpriteNode()
barra.name = "bar"
barra.size = CGSizeMake(300, 100)
barra.color = SKColor.whiteColor()
barra.position = CGPoint(x: 100, y: 100)
self.addChild(barra)

Adding barra to the screen does not change the node count.

Any help would be appreciated!

+4
source share
2 answers

You can use SKShapeNode, for example:

var barra = SKShapeNode(rectOfSize: CGSize(width: 300, height: 100))
barra.name = "bar"
barra.fillColor = SKColor.whiteColor()
barra.position = location

self.addChild(barra)

Here's the Apple documentation on SKShapeNode.

+16
source

The way to create the rectangle was not the best, but the problem was in the position that I inserted into the game. Here is how I fixed it:

var barra = SKSpriteNode(color: SKColor.blackColor(), size: CGSizeMake(200, 200))
barra.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
barra.zPosition = 9 // zPosition to change in which layer the barra appears.

self.addChild(barra)
+4
source

All Articles