Swift SpriteKit edgeLoopFromRect Issue

浪尽此生 提交于 2019-12-06 06:14:31

问题


The following code recognizes the bottom and top edges 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.

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)

    }
}

I think it has something to do with the .sks file since dimensions are set there and if I change those dimensions it reflects on the game. Anyone know how to make it so this line of code takes precedence over the .sks file dimensions? This way it will be dynamic and could work on different devices.

self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

I found the same question here but was never answered: Swift physicsbody edge recognition issue

Also, I am new to iOS app dev and swift so forgive me if the answer is obvious and I missed it.


回答1:


You are probably on the right track about .sks file. When scene is loaded from the .sks file the default size is 1024x768. Now you can dynamically set that based on the view size. So, swap your viewDidLoad with viewWillLayoutSubviews like this:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    if let scene = GameScene(fileNamed:"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

        //Because viewWillLayoutSubviews might be called multiple times, check if scene is already initialized
         if(skView.scene == nil){

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

            skView.presentScene(scene)
        }
    }
}

There are many posts on SO about differences between viewDidLoad and viewWillLayoutSubviews, so I will skip that in my answer.

Hope this helps.



来源:https://stackoverflow.com/questions/30835728/swift-spritekit-edgeloopfromrect-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!