我的球(SKSpriteNode)假设屏幕周围的墙壁反弹,但它只是通过屏幕而消失,并从屏幕上消失。
GameScene.swift
let borderCategory: UInt32 = 1
let ball = SKShapeNode(circleOfRadius: 30)
let bCategoryName = "Ball"
let bCategory: UInt32 = 2
let paddle = SKShapeNode(rectOfSize: CGSize(width: 90, height: 35))
let pCategoryName = "Paddle"
let pCategory: UInt32 = 3
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.backgroundColor = SKColor.whiteColor()
self.physicsWorld.gravity = CGVectorMake(0, 0)
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
NSLog(String(self.frame.width) + ":" + String(self.frame.height), nil)
self.physicsBody = borderBody
self.physicsBody.friction = 0.0
self.physicsBody.categoryBitMask = borderCategory
self.physicsBody.restitution = 1.0
setupPaddle()
setupBall()
}
func setupPaddle() {
paddle.name = pCategoryName
paddle.position = CGPointMake(CGRectGetMidX(self.frame), paddle.frame.size.height * 0.6)
paddle.fillColor = SKColor.blackColor()
self.addChild(paddle)
paddle.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 90, height: 35))
paddle.physicsBody.restitution = 0.1
paddle.physicsBody.friction = 0.4
paddle.physicsBody.dynamic = false
paddle.physicsBody.categoryBitMask = pCategory
paddle.physicsBody.collisionBitMask = bCategory
}
func setupBall() {
ball.name = bCategoryName
ball.position = CGPointMake(self.frame.size.width/2, paddle.position.y+paddle.frame.height+10)
ball.fillColor = SKColor.blackColor()
self.addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 30)
ball.physicsBody.friction = 0.0
ball.physicsBody.restitution = 1.0
ball.physicsBody.linearDamping = 0.0
ball.physicsBody.angularDamping = 0.0
ball.physicsBody.allowsRotation = false
ball.physicsBody.dynamic = true
ball.physicsBody.categoryBitMask = bCategory
ball.physicsBody.collisionBitMask = borderCategory
}
NSLog(String(self.frame.width) + ":" + String(self.frame.height), nil)
的输出
是1024.0:768.0
,在iPhone 5S,IOS8上运行。
我该如何解决这个问题?我相信self.frame是问题,但我已经将控制器视图从viewDidLoad
更改为viewWillLayoutSubview
。
编辑1:我注意到在将边界恢复到1.0之后,球现在正在弹跳。但是,我意识到球在一个未知区域弹跳。它从屏幕弹出,然后返回。奇怪的是它一旦到达屏幕顶部就会反弹,但它不会碰到屏幕的一侧
编辑2:我意识到另一个问题,UIDevice.currentDevice().orientation.isPortrait
返回false,但我还是以纵向模式运行游戏
编辑3:如果我将方向更改为横向,则球可以从顶部和底部从屏幕中弹出一点然后反弹,球正确地从屏幕侧面反弹。但现在桨在屏幕下方。