SKSpriteNode ball does not collide with the border

My ball (SKSpriteNode) is supposed to bounce a wall that is around the screen, but it just went 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
}

      

Output for NSLog(String(self.frame.width) + ":" + String(self.frame.height), nil)

1024.0:768.0

, works on iPhone 5S, IOS8.

How to solve this problem? I believe the problem is self.frame, but I have already changed the controller view from viewDidLoad

to viewWillLayoutSubview

.

EDIT 1: I noticed that the ball now bounces after setting the border restriction to 1.0. BUT, I realized that the ball is bouncing in an unknown region. It bounces off the screen and then comes back. It's weird that it can bounce as soon as it hits the top of the screen, but it won't hit the side of the screen.

EDIT 2: I figured out another problem, UIDevice.currentDevice().orientation.isPortrait

returns false, but still I run my game in portrait mode

EDIT 3: If I change the orientation to landscape, the ball might pop out of the screen from the top and bottom and then bounce back, the ball bounces off the side of the screen correctly. But now the paddle is under the screen.

+1
ios swift sprite-kit skspritenode


source to share


No one has answered this question yet

See similar questions:

five
Problems understanding SpriteKit coordinate system using Swift

or similar:

37
Swift - must call the designated error initializer of the superclass SKSpriteNode
2
SkSpriteNode does not have a membership center
2
Custom SKSpriteNode addChild not working
2
Strange thing happens with SKSpriteNode with transparent borders
1
SpriteKit: gap between SKSpriteNodes
1
removeFromParent doesn't work with SKSpriteNode
1
How can I prevent the SKSpriteNode from moving with physical motion when colliding with other sprites?
1
How to join two conflicting SKSpriteNodes
0
SKSpriteNode with color does not show
0
Make SKSpriteNode trigger an action on the edge of the screen



All Articles
Loading...
X
Show
Funny
Dev
Pics