How to make border for a scene in SpriteKit - ios

I am trying to make border for the two sides in a scene but there is some kind of error the here is the code i used
let leftEdge : SKNode = SKNode()
leftEdge.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: CGPointMake(0.0, self.size.height + 100))
leftEdge.position = CGPointZero;
self.addChild(leftEdge)
let rightEdge : SKNode = SKNode()
rightEdge.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: CGPointMake(0.0, self.size.height + 100))
rightEdge.position = CGPointMake(self.size.width, 0.0);
self.addChild(rightEdge)
but the top border and bottom border get the borders and the two sides its seems like they have border outside the the scene cause i shoot object toward them it goes out of the scene and get back so the question how i make borders only for the two sides the left and the right

try this
let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
border.restitution = 1
self.physicsBody = border
obviously you can add your own physics to your border.

Related

Use an SKShapeNode for the mask of an SKCropNode (Swift)

How can I use an SKShapeNode for the mask of an SKCropNode? My code currently creates an SKShapeNode circle and then sets the crop node mask to it
shapeNode = SKShapeNode(circleOfRadius: 50)
shapeNode.fillColor = UIColor.red
shapeNode.lineWidth = 1
shapeNode.position = CGPoint(x: 0, y: 0)
shapeNode.zPosition = 4
cropNode = SKCropNode()
cropNode.maskNode = shapeNode
cropNode.addChild(Node)
But when I add the Node as a child of the cropNode it doesnt draw anything but a red circle (exactly as i set it to do)
Is there any way to use an SKShapeNode as the mask of an SKCropNode?
-Update-
I figured out how to make an SKSpriteNode drawn from the SKShapeNode
shapeNode = SKShapeNode(circleOfRadius: 60)
shapeNode.fillColor = UIColor.red
let shapeTexture = view.texture(from: shapeNode)
textureNode = SKSpriteNode(texture: shapeTexture)
textureNode.position = CGPoint(x: 0, y: 0)
textureNode.zPosition = 3
cropNode.maskNode = textureNode
cropNode.addChild(tileMap2)
self.addChild(cropNode)
But now the SKCropNode isnt working when applied to the SKSpriteNode how do I fix this?

Programmatically create constrained region of physics, SpriteKit

I would like two regions, as shown in the image below, where the yellow region is to contain sprites. For example I'd like to have balls in the yellow region bouncing and reflecting off the boundaries of the yellow region. How can I programmatically do this without using an sks file?
You create an edge based physics body using the +bodyWithEdgeLoopFromRect:
override func didMoveToView(view: SKView) {
//Setup scene's physics body (setup the walls)
physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
let yellowSprite = SKSpriteNode(color: .yellowColor(), size: CGSize(width: 300, height: 300))
yellowSprite.position = CGPoint(x: frame.midX, y: frame.midY)
//Create the rectangle which will represent physics body.
let rect = CGRect(origin: CGPoint(x: -yellowSprite.size.width/2, y: -yellowSprite.size.height/2), size: yellowSprite.size)
yellowSprite.physicsBody = SKPhysicsBody(edgeLoopFromRect: rect)
addChild(yellowSprite)
//Add Red ball "inside" the yellow sprite
let red = SKShapeNode(circleOfRadius: 20)
red.fillColor = .redColor()
red.strokeColor = .clearColor()
red.position = yellowSprite.position
red.physicsBody = SKPhysicsBody(circleOfRadius: 20)
red.physicsBody?.restitution = 1
red.physicsBody?.friction = 0
red.physicsBody?.affectedByGravity = false
addChild(red)
red.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 15))
}
About rect parameter:
The rectangle that defines the edges. The rectangle is specified
relative to the owning node’s origin.
Hope this helps!

Centering an Oval using CGRectMake Swift

I am having trouble trying to center my oval programmatically currently I have this code
func setupLayers(){
let oval = CAShapeLayer()
oval.frame = CGRectMake(137.5, 283.5, 100, 100)
oval.path = ovalPath().CGPath;
self.layer.addSublayer(oval)
layers["oval"] = oval
resetLayerPropertiesForLayerIdentifiers(nil)
}
This code is able to center the oval in an iPhone 6 and 6s, but I want it to be able to be centered in all devices.
I have tried this code too:
func drawCircle(size: CGFloat) {
let circleShape = CAShapeLayer()
circleShape.path = UIBezierPath(ovalInRect: CGRectMake(-size/2, -size/2, size, size)).CGPath
circleShape.fillColor = UIColor.blueColor().CGColor
circleShape.strokeColor = UIColor.redColor().CGColor
circleShape.lineWidth = 1
circleShape.frame.origin = view.center
circleShape.name = "circleShape"
view.layer.addSublayer(circleShape)
}
drawCircle(100)
You need to set the layer's anchorPoint to be its center.
oval.anchorPoint = CGPoint(x: 0.5, y: 0.5)
Then you can set position of layer as the center of view:
oval.position = self.center

How to make two sides edges with keeping scale mode Aspect fill?

I am trying to make edges for the two sides in the scene by using this code
let leftEdge : SKNode = SKNode()
leftEdge.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero , toPoint:CGPointMake(0.0, self.frame.size.height + 100))
leftEdge.position = CGPointZero;
leftEdge.physicsBody!.categoryBitMask = EdgeCategory;
leftEdge.physicsBody!.collisionBitMask = BubblesCategory;
self.addChild(leftEdge)
// right edge
let rightEdge : SKNode = SKNode()
rightEdge.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero , toPoint:CGPointMake(0.0, self.frame.size.height + 100))
rightEdge.position = CGPointMake(self.frame.size.width, 0.0);
rightEdge.physicsBody!.categoryBitMask = EdgeCategory;
rightEdge.physicsBody!.collisionBitMask = BubblesCategory;
self.addChild(rightEdge)
It works fine if i use ResizeFill scale mode but i want to make the mode AspectFill so how i can make the edges with keeping the mode AspectFill?

Swift physicsbody edge recognition issue

The following code recognizes the bottom and top edge of the scene and the ball bounces off as expected. However, the left and right edges of the scene are breached all the time. The ball goes off screen and then eventually returns back if enough force is applied. It is as if the edges of the scene are beyond the edges of the iphone simulator window. Can someone help? Thanks.
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView){
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
backgroundColor = UIColor.cyanColor()
var ball = SKSpriteNode(imageNamed: "ball")
ball.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.height/2)
let push = CGVectorMake(10, 10)
ball.physicsBody.applyImpulse(push)
}
}
You can try to change self.physicsBody size for example;
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRectMake(self.frame.origin.x, self.frame.origin.y, self.size.width, self.size.height))
and change the parameter self.size.width as you wish

Resources