Screen's real size is bigger than what I see

2019-04-09 09:54发布

问题:

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?

回答1:

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


回答2:

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