Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a rectangle in Sprite kit using Swift

I'm trying to create a rectangle using swift on Sprite kit, since the rectangle needs to be used as an object in the scene, i assumed that i needed to create a SkSpriteNode, and them give it a size, but it did not worked, this 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 counting.

Any help will be appreciated!

like image 796
André Oliveira Avatar asked Nov 14 '14 23:11

André Oliveira


2 Answers

You may want to use a SKShapeNode, like this:

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

self.addChild(barra)

Here's Apple's documentation on SKShapeNode.

like image 154
Tiago Marinho Avatar answered Nov 16 '22 07:11

Tiago Marinho


The way I was creating the rectangle was not the best, but the problem was the position I was inserting it on the game. This is how I've 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)
like image 23
André Oliveira Avatar answered Nov 16 '22 06:11

André Oliveira