1

My ball (SKSpriteNode) is suppose to bounce of the wall that is around the screen, but it simply past through it, and disappeared from the screen.

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
}

The output for NSLog(String(self.frame.width) + ":" + String(self.frame.height), nil) is 1024.0:768.0, ran on iPhone 5S, IOS8.

How do i solve this problem? I believe that self.frame is the problem, but i already changed my the controller view from viewDidLoad to viewWillLayoutSubview.

EDIT 1: I noticed that the ball is now bouncing after setting the boundary's restitution to 1.0. BUT, I realised the ball is bouncing in an unknown region. It is bouncing out of the screen, then return back. The weird thing is it can bounce once it hit the top of the screen, but it won't hit side of the screen

EDIT 2: I realised another problem, UIDevice.currentDevice().orientation.isPortrait returns false, but yet I'm running my game in portrait mode

EDIT 3: If I changed orientation to landscape, the ball can escape out of the screen from the top and bottom by a little then bounce back, the ball bounces off the side of the screen correctly. But now the paddle is below the screen.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
junyi00
  • 792
  • 3
  • 8
  • 28
  • 1
    It's difficult to tell as there's quite a bit of your code missing in the example, but I think your categories are a bit screwed up. You seem to have three separate categories "borderCategory", "bCategory" and "pCategory"... is that truly what you intended? – Grimxn Jun 17 '14 at 10:53
  • Thanks - will try it out shortly... – Grimxn Jun 17 '14 at 11:10
  • Try setting the contactTestBitMask for the ball and the boundary to bCategory | borderCategory | pCategory. Also the restitution of the boundary isn't set... – Grimxn Jun 17 '14 at 11:49
  • It still does not bounce out after setting the restitution of boundary to 1.0 I realised the problem, i am going to change the question – junyi00 Jun 17 '14 at 12:22
  • I have re-created this independently, and it's baffling. I suspect that the answer is in something new that I haven't tried out yet, like auto-layout. – Grimxn Jun 18 '14 at 08:09
  • I'm feel that this is probably a iOS 8 beta bug. I tried changing lots of potential values but gives extremely, weird results – junyi00 Jun 18 '14 at 08:14
  • according to @fuzzygoat answer at http://stackoverflow.com/a/24170460/1879382 ,it worked for me, but my ball does not react to any impulse at all. ball is still dynamic though – junyi00 Jun 18 '14 at 10:44
  • Your code looks correct. I think the problem lies with the 1024 by 768 in your log. Which is an iPad ratio, not an iPhone ratio. And that's only important because I've seen a rash of questions lately where people can't get coordinates to work correctly in their scene in ios8. And it's normally because the setting in the sks file is wrong. If you get it set to the correct iPhone size/shape/pixel density, I suspect your code will work. – Cooper Buckingham Jun 19 '14 at 07:19

0 Answers0