UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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)
      }
   }
}

3      

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 ?

3      

@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.

3      

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()
        }
 }

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.