-1

There's a ball and a paddle, the ball should be on the paddle and touching it but the ball never touches the paddle and stops before reaching the paddle, can anyone help me out?

enter image description here

import SpriteKit

class GameScene: SKScene ,SKPhysicsContactDelegate {

    let ballCategory:UInt32 = 0x1 << 0
    let bottomCategory:UInt32 = 0x1 << 1
    let paddleCategory:UInt32 = 0x1 << 2

    override init(size: CGSize){
        super.init(size: size)

        physicsWorld.contactDelegate = self


        let ball = SKSpriteNode(imageNamed: "ball")
        ball.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        addChild(ball)

        ball.physicsBody = SKPhysicsBody(circleOfRadius: self.frame.size.width / 2)
        ball.physicsBody?.dynamic = true
        ball.physicsBody?.allowsRotation = false
        ball.physicsBody?.friction = 0
       // ball.physicsBody?.applyImpulse(CGVectorMake(2,-2))
        ball.physicsBody?.categoryBitMask = ballCategory
        ball.physicsBody?.contactTestBitMask = paddleCategory
        ball.physicsBody?.collisionBitMask = paddleCategory
        ball.physicsBody?.affectedByGravity = true

        let paddle = SKSpriteNode(imageNamed: "paddle")
        paddle.position = CGPointMake(CGRectGetMidX(self.frame), paddle.frame.size.height * 2)
        addChild(paddle)

        paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
        paddle.physicsBody?.affectedByGravity = false
        paddle.physicsBody?.dynamic = false
        paddle.physicsBody?.categoryBitMask = paddleCategory

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    }

    func didBeginContact(contact: SKPhysicsContact) {
            print("touched")

    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • how does the ball even move? Gravity only? It looks like your ball drops, and then once it hits slightly above y plane that your paddle is on, will do some tiny physics, and then just float on that y plane above the paddle. It is floating because you are doing paddleHeight * 2 – Knight0fDragon Feb 29 '16 at 18:45
  • What does the image "ball" look like? Needs to extend all the way to the edge of the frame or you need to change the SKPhysicsBody to fit the image. – beyowulf Feb 29 '16 at 23:32
  • What height of paddle do I use ?,and I have uploaded the screenshot – Qazi Shahood Mar 01 '16 at 08:48

2 Answers2

0

You are experiencing this issue because of this line:

 ball.physicsBody = SKPhysicsBody(circleOfRadius: self.frame.size.width / 2)

self is a scene here.

You need this:

 ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width / 2)

Turn on physics visual representation in your GameViewController like this:

skView.showsPhysics =  true

and you will see what is going on.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
0

The best way to determine where your nodes are making contact is to turn on Physics.

Open GameViewController.swift and enter the following code:

skView.showsPhysics =  true