Swift SpriteKit edgeLoopF​​romRect问题 [英] Swift SpriteKit edgeLoopFromRect Issue

查看:48
本文介绍了Swift SpriteKit edgeLoopF​​romRect问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可识别场景的底部和顶部边缘,并且球会按预期反弹.但是,场景的左右边缘一直被破坏.球离开屏幕,然后如果施加足够的力最终返回.好像场景的边缘超出了iPhone仿真器窗口的边缘.

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)

    }
}

我认为这与.sks文件有关,因为在那里设置了尺寸,如果我更改了这些尺寸,它将反映在游戏中.任何人都知道如何做到这一点,因此这一行代码优先于.sks文件尺寸?这样,它将是动态的,并且可以在不同的设备上运行.

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)

我在这里找到了相同的问题,但从未得到回答: Swift Physicalbody边缘识别问题

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

此外,我是iOS应用开发人员的新手,请迅速回答,如果答案很明显并且我错过了,请原谅我.

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

推荐答案

您可能对.sks文件的了解是正确的.从.sks文件加载场景时,默认大小为1024x768.现在,您可以根据视图大小动态设置该值.因此,将viewDidLoad与viewWillLayoutSubviews交换如下:

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)
        }
    }
}

SO上有很多关于 viewDidLoad和viewWillLayoutSubviews之间的差异的帖子,因此我将在回答中略过.

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

希望这会有所帮助.

这篇关于Swift SpriteKit edgeLoopF​​romRect问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆