6
votes

I'm learning both Swift & SpriteKit. I created new SpriteKit & Swift game project in Xcode 6 and I edited file GameScene.swift. I didn't touch other files. The problem: When the ball bounces around, It's limited by top and down side but when it goes to left or right side it's not limited and goes out but it seems that the right and left limits are there but they aren't on screen edge because ball comes back after a little while. This is my GameScene.swift:

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        self.backgroundColor = SKColor.whiteColor()

        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsWorld.gravity = CGVectorMake(0, 0)

        let ball = SKSpriteNode(imageNamed: "ball")
        ball.position = CGPointMake(self.size.width/2, self.size.height/2)

        ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width/2)

        self.addChild(ball)

        var myVector = CGVectorMake(20, 20)
        ball.physicsBody.applyImpulse(myVector)
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

I tried to find out the issue and I found that if I comment the line 42 or scene.scaleMode = .AspectFill in default GameViewController.swift then edges work correct but it seems the screen is scaled.

I think we have a real frame of shape square and the side size is equal to height of portrait iPhone in iOS Simulator. How I can change the frame size to set screen edges as my frame edge?

2
Don't do this in didMoveToView, do it while initing. - duci9y
Try adding an NSLog to determine the dimensions of self.frame. - 0x141E
It shouldn't matter if he did it in didMoveToView or init in this case. I'm guessing wherever you're initializing your GameScene() you're probably giving it the wrong size. If you want your scene to be the size of your view then you should have something like GameScene(self.view.frame.size) somewhere in your code. - Literphor
@Literphor It worked! Thanks! I removed scene created by default and added line scene = GameScene(self.view.frame.size) and It worked! - Potter

2 Answers

6
votes

As mentioned above, setting the size worked for me too. I set it in the viewDidLoad() function of the GameViewController, just after the line that sets

/* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

by setting:

scene.size = self.view.frame.size
1
votes

I removed scene created by default and added line scene = GameScene(size: self.view.frame.size) and It worked! Thanks to @Literphor