Reputation: 497
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
/* let myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!";
myLabel.fontSize = 45;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(myLabel)*/ let sprite = SKSpriteNode(imageNamed:"Beach Ball-100")
self.addChild(sprite)
let bb = SKPhysicsBody(edgeLoopFromRect: self.frame)
bb.friction = 0
self.physicsBody = bb
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let sprite = SKSpriteNode(imageNamed:"Beach Ball-100")
self.addChild(sprite)
sprite.xScale = 0.15
sprite.yScale = 0.15
sprite.position = location
sprite.color = UIColor.redColor()
//sprite.physicsBody = SKPhysicsBody(edgeLoopFromRect:self.frame)
sprite.physicsBody = SKPhysicsBody(circleOfRadius: 2.5)
//sprite.physicsBody?.usesPreciseCollisionDetection = true
sprite.physicsBody?.affectedByGravity = true
sprite.physicsBody?.allowsRotation = false
sprite.physicsBody?.restitution = 0.5
sprite.physicsBody?.linearDamping = 0
sprite.physicsBody?.friction = 0
//self.physicsWorld.
self.physicsWorld.gravity = CGVectorMake(0,-2.5)
//self.physicsWorld.gravity = CGVectorMake(0.25, -0.25)
//var action2 = SKAction.rotateByAngle(180.0, duration:5)
//sprite.runAction( action2)
//action = SKAction.applyTorque(2, duration: 3)
//sprite.runAction(SKAction.repeatActionForever(action))
//let boundaries = UICollisionBehavior(items: [sprite])
}
}
}
The horizontal boundaries don't seem to be working. If I drop the ball from the same point, since the friction is set to zero, they keep moving out of the screen. Please help me understand what the problem is.
Upvotes: 1
Views: 297
Reputation: 13665
It is probably because your scene size differs from a view's size. By default a scene is loaded from .sks file and it has size of 1024x768.
To fix this you can make these changes in your GameViewController:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
//Make scene's size same as view's size
scene.size = skView.bounds.size
skView.presentScene(scene)
}
}
Hope this helps!
Upvotes: 2