SKShapeNode SKPhysicsBody Problems - ios

I have a SKShapeNode that I want to add a SKPhysicsBody to. Here's the code for the SKShapeNode:
let width: CGFloat = 200//self.frame.width
let height: CGFloat = self.frame.height
rightBlocker.path = UIBezierPath(roundedRect: CGRect(x:-width/2, y: -height/2, width: width, height: height), cornerRadius: 50).cgPath
rightBlocker.position = CGPoint(x: -75, y: self.frame.height / 2)
rightBlocker.fillColor = UIColor.white
rightBlocker.strokeColor = UIColor.white
rightBlocker.lineWidth = 10
rightBlocker.physicsBody?.affectedByGravity = false
rightBlocker.physicsBody?.isDynamic = false
addChild(rightBlocker)
I've tried to add a SKPhysicsBody using this line of code:
rightBlocker.physicsBody = SKPhysicsBody(rectangleOf: (rightBlocker.frame.size))
but whenever I do, it completely disappears. What am I doing wrong?

Related

Swift - How to apply the drop shadow only on UIButton Bottom Shadow but not for its image and its title label?

I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
And here is what I want to have:
This is my current code:
button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false
Please help. Thanks in advance.
Checkout below lines of code
btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)
btn.imageView?.layer.shadowColor = UIColor.blue.cgColor
btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.imageView?.layer.shadowOpacity = 1.0
btn.imageView?.layer.shadowRadius = 5
btn.imageView?.layer.masksToBounds = false
btn.setTitle(" hello", for: .normal)
btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor
btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)
btn.titleLabel?.layer.shadowOpacity = 1.0
btn.titleLabel?.layer.shadowRadius = 3
btn.titleLabel?.layer.masksToBounds = false
btn.backgroundColor = UIColor.red
Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.
Like I have done below:
yourButtonView.layer.borderWidth = 0.5
yourButtonView.layer.borderColor = UIColor.gray.cgColor
yourButtonView.layer.shadowColor = UIColor.black.cgColor
yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)
yourButtonView.layer.shadowOpacity = 1.0
yourButtonView.layer.shadowRadius = 0
yourButtonView.layer.masksToBounds = false
Please use the below extension for creating shadow
extension UIView {
func addshadow(top: Bool,
left: Bool,
bottom: Bool,
right: Bool
) {
let shadowRadius: CGFloat = 2.0
self.layer.masksToBounds = false
self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
self.layer.shadowRadius = shadowRadius
self.layer.shadowOpacity = 0.3
let path = UIBezierPath()
var x: CGFloat = 0
var y: CGFloat = 0
var viewWidth = self.frame.width
var viewHeight = self.frame.height
if (!top) {
y+=(shadowRadius+1)
}
if (!bottom) {
viewHeight-=(shadowRadius+1)
}
if (!left) {
x+=(shadowRadius+1)
}
if (!right) {
viewWidth-=(shadowRadius+1)
}
path.move(to: CGPoint(x: x, y: y))
path.addLine(to: CGPoint(x: x, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
path.addLine(to: CGPoint(x: viewWidth, y: y))
path.close()
self.layer.shadowPath = path.cgPath
}
}
use -
button.addshadow(top: false, left: false, bottom: true, right: false)

Adding a background to a SKLabelNode

I have been trying to add a background around my SKLabelNode, but I can't figure out how to do it programmatically in swift 3. I see some old posts that use .background, but that doesn't work for me.
let background = SKSpriteNode(color: UIColor.white, size: CGSize(width: CGFloat((title.frame.size.width)), height:CGFloat((title.frame.size.height))))
background.zPosition = -1
background.position = CGPoint(x: self.frame.width / 12 * 6, y: self.frame.height / 12 * 11); title.addChild(background)
Check this new code, I think this suited more to your problem
let shape = SKShapeNode()
shape.path = UIBezierPath(roundedRect: CGRect(x:(label?.frame.origin.x)! - 15, y: (label?.frame.origin.y)! - 15, width: ((label?.frame.size.width)!+30), height: ((label?.frame.size.height)! + 50 )), cornerRadius: 64).cgPath
shape.position = CGPoint(x: frame.midX, y: frame.midY)
shape.fillColor = UIColor.red
shape.strokeColor = UIColor.blue
shape.lineWidth = 5
label?.addChild(shape)

Sprite Kit Won't stop adding nodes

In my project everything is going smoothly and working great except for when I want to add 1 instance of a SKLabelNode when a certain event happens in my game.
Then problem is when the event happens it adds the SKLabelNode more than once and it keeps on doing it into the thousands...
Here is my code:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate{
var isTouched: Bool = false
let lavaRoom = SKSpriteNode(imageNamed: "LavaRom")
let tileStone1 = SKSpriteNode(imageNamed: "TileStone")
let tileStone2 = SKSpriteNode(imageNamed: "TileStone")
let tileStone3 = SKSpriteNode(imageNamed: "TileStone")
let tileStone4 = SKSpriteNode(imageNamed: "TileStone")
let tileStone5 = SKSpriteNode(imageNamed: "TileStone")
let tileStone6 = SKSpriteNode(imageNamed: "TileStone")
let tileStone7 = SKSpriteNode(imageNamed: "TileStone")
let tileStone8 = SKSpriteNode(imageNamed: "TileStone")
let tileStone9 = SKSpriteNode(imageNamed: "TileStone")
let tileStone10 = SKSpriteNode(imageNamed: "TileStone")
let tileStone11 = SKSpriteNode(imageNamed: "TileStone")
let tileStone12 = SKSpriteNode(imageNamed: "TileStone")
let tileStone13 = SKSpriteNode(imageNamed: "TileStone")
let tileStone14 = SKSpriteNode(imageNamed: "TileStone")
let tileStone15 = SKSpriteNode(imageNamed: "TileStone")
let tileStone16 = SKSpriteNode(imageNamed: "TileStone")
let tileStone17 = SKSpriteNode(imageNamed: "TileStone")
let tileStone18 = SKSpriteNode(imageNamed: "TileStone")
let tileStone19 = SKSpriteNode(imageNamed: "TileStone")
let tileStone20 = SKSpriteNode(imageNamed: "TileStone")
let tileStone21 = SKSpriteNode(imageNamed: "TileStone")
let tileStone22 = SKSpriteNode(imageNamed: "TileStone")
let tileStone23 = SKSpriteNode(imageNamed: "TileStone")
let tileStone24 = SKSpriteNode(imageNamed: "TileStone")
let tileStone25 = SKSpriteNode(imageNamed: "TileStone")
let tileStone26 = SKSpriteNode(imageNamed: "TileStone")
let tileStone27 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop1 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop2 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop3 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop4 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop5 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop6 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop7 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop8 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop9 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop10 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop11 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop12 = SKSpriteNode(imageNamed: "TileStone")
let tileStoneTop13 = SKSpriteNode(imageNamed: "TileStone")
let player = SKSpriteNode(imageNamed: "PlayerBox")
let enemyBox = SKSpriteNode(imageNamed: "TileStone")
let playerInt : UInt32 = 0
let enemyInt : UInt32 = 1
override func didMoveToView(view: SKView) {
let invisibleNode = SKShapeNode(rect: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
invisibleNode.name = "box"
invisibleNode.fillColor = SKColor.redColor()
invisibleNode.strokeColor = SKColor.clearColor()
self.addChild(invisibleNode)
self.backgroundColor = UIColor(hue: 0.0194, saturation: 0.66, brightness: 0.89, alpha: 1.0)
player.size = CGSize(width: 50, height: 50)
player.position = CGPointMake(self.frame.size.width / 2 - 350, 190)
player.anchorPoint = CGPointZero
player.zPosition = 2
addChild(player)
enemyBox.size = CGSize(width: 50, height: 50)
enemyBox.position = CGPointMake(self.frame.size.width / 2, 215)
enemyBox.zPosition = 2
addChild(enemyBox)
tileStone1.size = CGSize(width: 100, height: 100)
tileStone1.anchorPoint = CGPointZero
tileStone1.position = CGPoint(x: 0, y: 90)
tileStone1.zPosition = 2
addChild(tileStone1)
tileStone2.size = CGSize(width: 100, height: 100)
tileStone2.zPosition = 2
tileStone2.anchorPoint = CGPointZero
tileStone2.position = CGPoint(x: 100, y: 90)
addChild(tileStone2)
tileStone3.size = CGSize(width: 100, height: 100)
tileStone3.zPosition = 2
tileStone3.anchorPoint = CGPointZero
tileStone3.position = CGPoint(x: 200, y: 90)
addChild(tileStone3)
tileStone4.size = CGSize(width: 100, height: 100)
tileStone4.zPosition = 2
tileStone4.anchorPoint = CGPointZero
tileStone4.position = CGPoint(x: 300, y: 90)
addChild(tileStone4)
tileStone5.size = CGSize(width: 100, height: 100)
tileStone5.anchorPoint = CGPointZero
tileStone5.position = CGPoint(x: 400, y: 90)
tileStone5.zPosition = 2
addChild(tileStone5)
tileStone6.size = CGSize(width: 100, height: 100)
tileStone6.anchorPoint = CGPointZero
tileStone6.position = CGPoint(x: 500, y: 90)
tileStone6.zPosition = 2
addChild(tileStone6)
tileStone7.size = CGSize(width: 100, height: 100)
tileStone7.anchorPoint = CGPointZero
tileStone7.position = CGPoint(x: 600, y: 90)
tileStone7.zPosition = 2
addChild(tileStone7)
tileStone8.size = CGSize(width: 100, height: 100)
tileStone8.anchorPoint = CGPointZero
tileStone8.position = CGPoint(x: 700, y: 90)
tileStone8.zPosition = 2
addChild(tileStone8)
tileStone9.size = CGSize(width: 100, height: 100)
tileStone9.anchorPoint = CGPointZero
tileStone9.position = CGPoint(x: 800, y: 90)
tileStone9.zPosition = 2
addChild(tileStone9)
tileStone10.size = CGSize(width: 100, height: 100)
tileStone10.anchorPoint = CGPointZero
tileStone10.position = CGPoint(x: 900, y: 90)
tileStone10.zPosition = 2
addChild(tileStone10)
addChild(tileStone11)
tileStone11.size = CGSize(width: 100, height: 100)
tileStone11.anchorPoint = CGPointZero
tileStone11.position = CGPoint(x: 1000, y: 90)
tileStone11.zPosition = 2
tileStone12.size = CGSize(width: 100, height: 100)
tileStone12.anchorPoint = CGPointZero
tileStone12.position = CGPoint(x: 1100, y: 90)
tileStone12.zPosition = 2
addChild(tileStone12)
tileStone13.size = CGSize(width: 100, height: 100)
tileStone13.anchorPoint = CGPointZero
tileStone13.position = CGPoint(x: 1200, y: 90)
tileStone13.zPosition = 2
addChild(tileStone13)
tileStone14.size = CGSize(width: 100, height: 100)
tileStone14.anchorPoint = CGPointZero
tileStone14.position = CGPoint(x: 1300, y: 90)
tileStone14.zPosition = 2
addChild(tileStone14)
tileStone15.size = CGSize(width: 100, height: 100)
tileStone15.anchorPoint = CGPointZero
tileStone15.position = CGPoint(x: 1400, y: 90)
tileStone15.zPosition = 2
addChild(tileStone15)
tileStone16.size = CGSize(width: 100, height: 100)
tileStone16.anchorPoint = CGPointZero
tileStone16.position = CGPoint(x: 1500, y: 90)
tileStone16.zPosition = 2
addChild(tileStone16)
tileStone17.size = CGSize(width: 100, height: 100)
tileStone17.anchorPoint = CGPointZero
tileStone17.position = CGPoint(x: 1600, y: 90)
tileStone17.zPosition = 2
addChild(tileStone17)
tileStone18.size = CGSize(width: 100, height: 100)
tileStone18.anchorPoint = CGPointZero
tileStone18.position = CGPoint(x: 1700, y: 90)
tileStone18.zPosition = 2
addChild(tileStone18)
tileStone19.size = CGSize(width: 100, height: 100)
tileStone19.anchorPoint = CGPointZero
tileStone19.position = CGPoint(x: 1800, y: 90)
tileStone19.zPosition = 2
addChild(tileStone19)
tileStone20.size = CGSize(width: 100, height: 100)
tileStone20.anchorPoint = CGPointZero
tileStone20.position = CGPoint(x: 1900, y: 90)
tileStone20.zPosition = 2
addChild(tileStone20)
tileStone21.size = CGSize(width: 100, height: 100)
tileStone21.anchorPoint = CGPointZero
tileStone21.position = CGPoint(x: 2000, y: 90)
tileStone21.zPosition = 2
addChild(tileStone21)
tileStone22.size = CGSize(width: 100, height: 100)
tileStone22.anchorPoint = CGPointZero
tileStone22.position = CGPoint(x: 2100, y: 90)
tileStone22.zPosition = 2
addChild(tileStone22)
tileStoneTop1.size = CGSize(width: 100, height: 100)
tileStoneTop1.anchorPoint = CGPointZero
tileStoneTop1.position = CGPoint(x: 0, y: 580)
tileStoneTop1.zPosition = 2
addChild(tileStoneTop1)
tileStoneTop2.size = CGSize(width: 100, height: 100)
tileStoneTop2.zPosition = 2
tileStoneTop2.anchorPoint = CGPointZero
tileStoneTop2.position = CGPoint(x: 100, y: 580)
addChild(tileStoneTop2)
tileStoneTop3.size = CGSize(width: 100, height: 100)
tileStoneTop3.zPosition = 2
tileStoneTop3.anchorPoint = CGPointZero
tileStoneTop3.position = CGPoint(x: 200, y: 580)
addChild(tileStoneTop3)
tileStoneTop4.size = CGSize(width: 100, height: 100)
tileStoneTop4.zPosition = 2
tileStoneTop4.anchorPoint = CGPointZero
tileStoneTop4.position = CGPoint(x: 300, y: 580)
addChild(tileStoneTop4)
tileStoneTop5.size = CGSize(width: 100, height: 100)
tileStoneTop5.anchorPoint = CGPointZero
tileStoneTop5.position = CGPoint(x: 400, y: 580)
tileStoneTop5.zPosition = 2
addChild(tileStoneTop5)
tileStoneTop6.size = CGSize(width: 100, height: 100)
tileStoneTop6.anchorPoint = CGPointZero
tileStoneTop6.position = CGPoint(x: 500, y: 580)
tileStoneTop6.zPosition = 2
addChild(tileStoneTop6)
tileStoneTop7.size = CGSize(width: 100, height: 100)
tileStoneTop7.anchorPoint = CGPointZero
tileStoneTop7.position = CGPoint(x: 600, y: 580)
tileStoneTop7.zPosition = 2
addChild(tileStoneTop7)
tileStoneTop8.size = CGSize(width: 100, height: 100)
tileStoneTop8.anchorPoint = CGPointZero
tileStoneTop8.position = CGPoint(x: 700, y: 580)
tileStoneTop8.zPosition = 2
addChild(tileStoneTop8)
tileStoneTop9.size = CGSize(width: 100, height: 100)
tileStoneTop9.anchorPoint = CGPointZero
tileStoneTop9.position = CGPoint(x: 800, y: 580)
tileStoneTop9.zPosition = 2
addChild(tileStoneTop9)
tileStoneTop10.size = CGSize(width: 100, height: 100)
tileStoneTop10.anchorPoint = CGPointZero
tileStoneTop10.position = CGPoint(x: 900, y: 580)
tileStoneTop10.zPosition = 2
addChild(tileStoneTop10)
tileStoneTop11.size = CGSize(width: 150, height: 100)
tileStoneTop11.anchorPoint = CGPointZero
tileStoneTop11.position = CGPoint(x: 1000, y: 580)
tileStoneTop11.zPosition = 2
addChild(tileStoneTop11)
tileStoneTop12.size = CGSize(width: 200, height: 100)
tileStoneTop12.anchorPoint = CGPointZero
tileStoneTop12.position = CGPoint(x: 1100, y: 5780)
tileStoneTop12.zPosition = 2
addChild(tileStoneTop12)
tileStoneTop13.size = CGSize(width: 100, height: 100)
tileStoneTop13.anchorPoint = CGPointZero
tileStoneTop13.position = CGPoint(x: 1100, y: 5780)
tileStoneTop13.zPosition = 2
// addChild(tileStoneTop13)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let touchLocation = touch.locationInNode(self)
let touchedNode = nodeAtPoint(touchLocation)
if touchedNode.name == "box" {
isTouched = true
}
if touchedNode.name == "rerun"{
for node in self.nodesAtPoint(touchLocation) {
if node.name == "rerun" {
isTouched = false
node.removeFromParent()
}
}
}
}
}
let moveFactor:CGFloat = 0.5
override func update(currentTime: CFTimeInterval) {
var tileStoneArray = [tileStone1, tileStone2, tileStone3, tileStone4, tileStone5, tileStone6, tileStone7, tileStone8, tileStone9, tileStone10, tileStone11, tileStone12, tileStone13, tileStone14, tileStone15, tileStone16, tileStone17, tileStone18, tileStone19, tileStone20, tileStone21, tileStone22, tileStoneTop1, tileStoneTop2, tileStoneTop3, tileStoneTop4, tileStoneTop5, tileStoneTop6, tileStoneTop7, tileStoneTop8, tileStoneTop9, tileStoneTop10, tileStoneTop11, tileStoneTop12, tileStoneTop13]
if isTouched == false {
enemyBox.position = CGPoint(x: enemyBox.position.x - 10, y: 215)
}
if enemyBox.position.x < self.frame.minX - 100 {
enemyBox.position = CGPoint(x: self.frame.maxX, y: 215)
}
for tileStone1 in tileStoneArray {
if isTouched == false {
tileStone1.position = CGPoint(x: tileStone1.position.x - 10, y: tileStone1.position.y)
}
if (tileStone1.position.x < self.frame.minX - 100){
tileStone1.anchorPoint = CGPointZero
tileStone1.position = CGPoint(x: self.frame.maxX, y: tileStone1.position.y)
}
}
var score = 0;
func addGreatNode(){
let congratsLabel = SKLabelNode(fontNamed: "")
congratsLabel.name = "rerun"
congratsLabel.text = "Great!"
congratsLabel.fontSize = 65
congratsLabel.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
congratsLabel.fontColor = SKColor.yellowColor()
addChild(congratsLabel)
}
if isTouched == true {
var xValuesEnemy = enemyBox.position.x
var xValuesPlayer = player.position.x
if xValuesPlayer < xValuesEnemy + 120 && xValuesPlayer > xValuesEnemy - 120 {
addGreatNode()
//This is where it adds the node infinitely
}
else {
}
}
}
}
This is a image of my node count climbing while in my game.
I know that I could improve my code to be more efficient but I'm very inexperienced, but I'm working on it.
Your addGreatNode() function is being called within update: which is called by the scene up to 60 times per second.
A few more tips:
Keep your TileStones in a collection
When adding TileStones to your Scene, use either a for or while loop. This will compact your code. Within each iteration, adjust the position for each Node.
If you want to add a SKLabelNode on touch, that logic should be within touchesBegan:, and not associated with update:

Building my first game in XCODE 7(swift 2)-CGPOINTMAKE?

I want to build a game similar to snooker, and at the beginning I already have some problems. I want to build four walls first (that will be the size of the screen-self.frame), and the first one I made like this:
let ground = SKNode()
ground.position = CGPointMake ( 0, 0)
ground.physicsBody = SKPhysicsBody( rectangleOfSize:CGSizeMake(self.frame.size.width * 3, 1))
ground.physicsBody!.dynamic = false
self.addChild(ground)
However, I don't known how to manipulate the values of CGPointMake to do the left/right/up walls. I first imagined that the (0,0) point was the left down corner, but it seems to not be like that. Can someone please help me with this or just explain how this works? (since in https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/func/CGPointMake they dont explain very much =/)
Here is a basic example of how you can restrict the ball from leaving the screen:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let BallCategory : UInt32 = 0x1 << 1
let WallCategory : UInt32 = 0x1 << 2
let ball = SKShapeNode(circleOfRadius: 40)
override func didMoveToView(view: SKView) {
/* Setup your scene here */
physicsWorld.contactDelegate = self
setupWalls()
setupBall()
}
func setupWalls(){
physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
physicsBody?.categoryBitMask = WallCategory
physicsBody?.contactTestBitMask = BallCategory
physicsBody?.collisionBitMask = BallCategory
}
func setupBall(){
ball.physicsBody = SKPhysicsBody(circleOfRadius: 40)
ball.fillColor = SKColor.whiteColor()
ball.name = "ball"
ball.physicsBody?.categoryBitMask = BallCategory
ball.physicsBody?.contactTestBitMask = WallCategory
ball.physicsBody?.collisionBitMask = WallCategory
ball.physicsBody?.dynamic = true //In order to detect contact between two bodies, at least on body has to be dynamic
ball.physicsBody?.affectedByGravity = false
ball.physicsBody?.restitution = 0.5
ball.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)) // placing to ball in the middle of the screen
addChild(ball)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
ball.physicsBody?.applyImpulse(CGVector(dx: 200, dy: 200))
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
Try this for adding four walls with a thickness of twenty:
// Create walls
let leftWall = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 20, height: self.frame.height))
leftWall.fillColor = UIColor.whiteColor()
let physicsBodyLW = SKPhysicsBody(rectangleOfSize: CGSize(width: 20, height: self.frame.size.height), center: CGPoint(x:10, y:self.frame.height/2))
physicsBodyLW.affectedByGravity = false
physicsBodyLW.dynamic = false
leftWall.physicsBody = physicsBodyLW
self.addChild(leftWall)
let rightWall = SKShapeNode(rect: CGRect(x: self.frame.width - 20, y: 0, width: 20, height: self.frame.height))
rightWall.fillColor = UIColor.whiteColor()
let physicsBodyRW = SKPhysicsBody(rectangleOfSize: CGSize(width: 20, height: self.frame.size.height), center: CGPoint(x:10, y:self.frame.height/2))
physicsBodyRW.affectedByGravity = false
physicsBodyRW.dynamic = false
rightWall.physicsBody = physicsBodyRW
self.addChild(rightWall)
let topWall = SKShapeNode(rect: CGRect(x: 0, y: self.frame.height-20, width: self.frame.width, height: 20))
topWall.fillColor = UIColor.whiteColor()
let physicsBodyTW = SKPhysicsBody(rectangleOfSize: CGSize(width: self.frame.size.width, height: 20), center: CGPoint(x:self.frame.width/2, y:10))
physicsBodyTW.affectedByGravity = false
physicsBodyTW.dynamic = false
topWall.physicsBody = physicsBodyTW
self.addChild(topWall)
let bottomWall = SKShapeNode(rect: CGRect(x: 0, y: 0, width: self.frame.width, height: 20))
print(bottomWall.frame)
bottomWall.fillColor = UIColor.whiteColor()
let physicsBodyBW = SKPhysicsBody(rectangleOfSize: CGSize(width: self.frame.size.width, height: 20), center: CGPoint(x:self.frame.width/2, y:10))
physicsBodyBW.affectedByGravity = false
physicsBodyBW.dynamic = false
bottomWall.physicsBody = physicsBodyBW
self.addChild(bottomWall)

How to CGPath or SKShapeNode Rotation

I want to add rotated elliptical paths to one center point. Like image below but random rotated elliptical.
Draw these elliptical orbits with,
roadShape = SKShapeNode(ellipseInRect: centeredRect)
roadShape.strokeColor = UIColor.greenColor()
self.parent!.addChild(roadShape)
need to make small particles follow those elliptical orbits, thats why I need a path.
var followPath = SKAction.followPath(roadShape.path, asOffset: false, orientToPath: true, duration: 10);
particle.runAction(followPath)
I used SKShapeNode to draw and get the path property. But unfortunately SKShapeNode doesn't have any origin or anchorPoint property so I couldn't decent rotate to SKShapeNode. Do you have any suggestion to make it rotate or different way.
Thanks in advance.
let roadShape1 = SKShapeNode(ellipseInRect: CGRectMake(-100, -25, 200, 50))
roadShape1.strokeColor = UIColor.greenColor()
roadShape1.lineWidth = 3
roadShape1.position = CGPoint(x:frame.midX, y: frame.midY)
addChild(roadShape1)
let roadShape2 = SKShapeNode(ellipseInRect: CGRectMake(-100, -25, 200, 50))
roadShape2.strokeColor = UIColor.greenColor()
roadShape2.lineWidth = 3
let action2 = SKAction.rotateByAngle(CGFloat(M_PI)*0.75, duration:0)
roadShape2.runAction(action2)
roadShape2.position = CGPoint(x:frame.midX, y: frame.midY)
addChild(roadShape2)
let roadShape3 = SKShapeNode(ellipseInRect: CGRectMake(-100, -25, 200, 50))
roadShape3.strokeColor = UIColor.greenColor()
roadShape3.lineWidth = 3
let action3 = SKAction.rotateByAngle(CGFloat(M_PI)*0.25, duration:0)
roadShape3.runAction(action3)
roadShape3.position = CGPoint(x:frame.midX, y: frame.midY)
addChild(roadShape3)
You can also use applyTransform to apply the rotation to the bezierPath
instead of the SKShapeNode as follow:
let roadPath1 = UIBezierPath(ovalInRect: CGRect(x: -100, y: -25, width: 200, height: 50))
let roadPath2 = UIBezierPath(ovalInRect: CGRect(x: -100, y: -25, width: 200, height: 50))
roadPath2.applyTransform(CGAffineTransformMakeRotation(45.0 * CGFloat(M_PI) / 180))
let roadPath3 = UIBezierPath(ovalInRect: CGRect(x: -100, y: -25, width: 200, height: 50))
roadPath3.applyTransform(CGAffineTransformMakeRotation(135.0 * CGFloat(M_PI) / 180))
let roadShape1 = SKShapeNode(ellipseInRect: CGRectMake(-100, -25, 200, 50))
roadShape1.path = roadPath1.CGPath
roadPath1.stroke()
roadShape1.lineWidth = 3
roadShape1.position = CGPoint(x:frame.midX, y: frame.midY)
addChild(roadShape1)
let roadShape2 = SKShapeNode(ellipseInRect: CGRectMake(-100, -25, 200, 50))
roadShape2.path = roadPath2.CGPath
roadPath2.stroke()
roadShape2.lineWidth = 3
roadShape2.position = CGPoint(x:frame.midX, y: frame.midY)
addChild(roadShape2)
let roadShape3 = SKShapeNode(ellipseInRect: CGRectMake(-100, -25, 200, 50))
roadShape3.path = roadPath3.CGPath
roadPath3.stroke()
roadShape3.lineWidth = 3
roadShape3.position = CGPoint(x:frame.midX, y: frame.midY)
addChild(roadShape3)

Resources