1

I want to know how to get the contact delegate working. I do have contact being made, but I would like the individual bricks to disappear whenever they get hit by the ball.

I've been hitting a brick wall with this for far too long. Tutorial after tutorial doesn't seem to be getting me anywhere. I may need to take baby steps but I'm unsure how to start.

I've tried many tutorials on Youtube and any I can find online.

enum CollisionType: UInt32 {

   case ball = 1
   case paddle = 2
   case walls = 4
   case brick = 8

}

class Brick: SKSpriteNode  { }


class GameScene: SKScene, SKPhysicsContactDelegate {

   let bricks = ["brickPurple","brickGreen", "brickRed"]
   let brick = SKSpriteNode(imageNamed: "brickPurple")

   let ball = SKSpriteNode(imageNamed: "ball")
   let paddle = SKSpriteNode(imageNamed: "paddle")



    override func didMove(to view: SKView) {

      physicsWorld.contactDelegate = self


      physicsBody = SKPhysicsBody(edgeLoopFrom: frame.inset(by: UIEdgeInsets(top: 0, left: 0 , bottom: 0 , right: 0 )))


      ball.name = "ball"
      ball.size = CGSize(width: 20, height: 20)
      ball.position.x = frame.maxX/2
      ball.position.y = 70
     addChild(ball)


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

      ball.physicsBody?.restitution = 1
      ball.physicsBody?.linearDamping = 0
      ball.physicsBody?.friction = 0
      ball.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 20))
      ball.physicsBody?.contactTestBitMask = ball.physicsBody!.collisionBitMask


      paddle.name = "paddle"
      paddle.size = CGSize(width: 100, height: 20)
      paddle.position.x = frame.maxX/2
      paddle.position.y = 50
      paddle.physicsBody = SKPhysicsBody(rectangleOf: paddle.size)
      paddle.physicsBody?.isDynamic = false



      let brickWidth = brick.frame.width
      let brickHeight = brick.frame.height

      for i in stride(from: view.bounds.minX + brickWidth, to: view.bounds.maxX - brickWidth, by: brickWidth ) {

         for j in stride(from: view.bounds.minX  + brickHeight + 375, to: view.bounds.maxX + 200, by: brick.frame.height + 0) {

            let brickType = bricks.randomElement()!

            let brick = Brick(imageNamed: brickType)

            brick.position = CGPoint(x: i, y: j)

            brick.physicsBody = SKPhysicsBody (rectangleOf: brick.size)


            brick.physicsBody?.isDynamic = false

            brick.name = "brick"

            addChild(brick)


         }


         }

      }}

I previously had some code for func didBegin(_ contact: SKPhysicsContact), but I've just decided to start over.

  • Hi Travis and welcome to SO. Can you please update your question to match the requirements of this site by including a description of what you'd *like* to happen and what is *actually* happening. – Steve Ives Oct 21 '19 at 13:17
  • Please put back in your `didBegin` function, as nothing will happen without that. – Steve Ives Oct 21 '19 at 13:18
  • 1
    You are not following any of your tutorials correctly. You do not set any of your bitmasks up except for the ball contacttestbitmask – Knight0fDragon Oct 21 '19 at 13:22

1 Answers1

0

This isn't a specific answer to your problem, but if you follow the steps in the answer below (especially the first one), I think you'll get your code working:

My step-by-step guide for collisions and contacts: https://stackoverflow.com/a/51041474/1430420

And a guide to collision and contactTest bit masks: https://stackoverflow.com/a/40596890/1430420

Manipulating bit masks to turn individual collision and contacts off and on. https://stackoverflow.com/a/46495864/1430420

Steve Ives
  • 7,894
  • 3
  • 24
  • 55