WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Project 11 Challenge 3. Hi did someone figured out how to remove those boxes when they're hit by balls?

Forums > 100 Days of Swift

I kinda struggling to sort this problem out, I have created same methods to detect which is the box but somthing is still ain't right here this is my code:

If someone could help me and share some code it will be much appreciated

Where I create the box I have added " box.physicsBody?.contactTestBitMask = box.physicsBody!.collisionBitMask"

I kind of declared same methods but is still not working.

let size = CGSize(width: Int.random(in: 16...128), height: 16) let box = SKSpriteNode(color: UIColor(red: CGFloat.random(in: 0...1), green: CGFloat.random(in: 0...1), blue: CGFloat.random(in: 0...1), alpha: 1), size: size) box.physicsBody?.contactTestBitMask = box.physicsBody!.collisionBitMask box.zRotation = CGFloat.random(in: 0...3) box.position = location // add the box object were the user taps box.name = "box"

            box.physicsBody = SKPhysicsBody(rectangleOf: box.size)
            box.physicsBody?.isDynamic = false // box object will not move when impact happens
            addChild(box) // box object will be added to the screen

              func boxandballCollision(box: SKNode, ball: SKNode) {
    if ball.name == "ball" {
        destroyObject(box: box)
    } else if ball.name == "ball" {
        destroyObject(box: box)
    }
}

func destroyObject(box: SKNode) {
    if let fireParticles = SKEmitterNode(fileNamed: "FireParticles") {
        fireParticles.position = box.position
        addChild(fireParticles)
    }

    box.removeFromParent() // box should be removed when a ball will hit it
}

func begin(_ contact: SKPhysicsContact) {
    guard let nodeA = contact.bodyA.node else { return }
    guard let nodeB = contact.bodyB.node else { return }

    if nodeA.name == "box" {
        boxandballCollision(box: nodeB, ball: nodeB)
    } else if nodeB.name == "box" {
        boxandballCollision(box: nodeA, ball: nodeA)
      }
   }
}

1      

func boxandballCollision(box: SKNode, ball: SKNode) {
if ball.name == "ball" {
destroyObject(box: box)
} else if ball.name == "ball" {
destroyObject(box: box)
}
}

In your codes,this function has only one activity in the closure actually,so why do you write with if and else if ?

1      

@little87 I kno is pointless it will do the same thing regardless, I have removed that else if statement, but still nothing is happening when a ball hits a box object. I still didn't figure this out. I don't understand why is not detecting the collision between box and ball.

1      

After adding the name "box" to the boxes as they are created, add something like this in your collision function:

func collisionBetween(ball: SKNode, object: SKNode) {
        if object.name == "good" {
            destroy(ball: ball)
            score += 1
        } else if object.name == "bad" {
            destroy(ball: ball)
            score -= 1
        } else if object.name == "box" {
            object.removeFromParent()
        }
 }

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.