1
\$\begingroup\$

I'm trying to create a collision between two objects. One is a SKSpriteNode and the other is a SKShapeNode. The code I have so far for determining contact is:

 func didBeginContact( contact: SKPhysicsContact) {
        let firstBody  : SKPhysicsBody = contact.bodyA
        let SecondBody : SKPhysicsBody = contact.bodyB

        if ((firstBody.categoryBitMask == PhysicsCatagory.Orange) && (SecondBody.categoryBitMask == PhysicsCatagory.Red)) || (firstBody.categoryBitMask == PhysicsCatagory.Red) && (SecondBody.categoryBitMask == PhysicsCatagory.Orange) {

            CollisionWithRed(firstBody.node as! SKSpriteNode, Red: SecondBody.node as! SKShapeNode)
        }


    }

    func CollisionWithRed (Orange: SKSpriteNode, Red:SKShapeNode){
       Orange.removeAllChildren()
       Red.removeAllChildren()
    }

The problem is that after the collision, the Orange and Red are not removed from the screen. They are still there.

The code for Orange is :

 func SpawnOranges () {
        let Orange = SKSpriteNode(imageNamed: "Orange.png")
        Orange.size = CGSize (width: 80, height: 80)
        let MinValue = self.size.width / 8
        let MaxValue = self.size.width - 20
        Orange.zPosition = 7
        let SpawnPoint = UInt32 (MaxValue - MinValue)
        Orange.position = CGPoint (x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
        Orange.physicsBody = SKPhysicsBody(rectangleOfSize:Orange.size)
        Orange.physicsBody?.categoryBitMask = PhysicsCatagory.Orange
        Orange.physicsBody?.affectedByGravity = false
        Orange.physicsBody?.dynamic = true
        Orange.physicsBody?.contactTestBitMask = PhysicsCatagory.red
        let action = SKAction.moveToY(-150, duration: 2.0)
        let actionDone = SKAction.removeFromParent()
        Orange.runAction(SKAction.sequence([action, actionDone]))

        addChild(Orange)  

The code for Red is :

class Red: SKShapeNode{
    convenience init(path: UIBezierPath, color: UIColor, borderColor:UIColor) {
        self.init()
        self.path = path.CGPath
        self.fillColor = color
        self.strokeColor = borderColor



        Red = Red(path: projectilePath, color: UIColor.redColor(), 
          borderColor: UIColor.blackColor())
        Red.position = Settings.Metrics.projectileRestPosition
        Red.zPosition = 7
        Red.name = "Square"
        Red.physicsBody = SKPhysicsBody()
        Red.physicsBody?.categoryBitMask = PhysicsCatagory.Orange
        Red.physicsBody?.affectedByGravity = false
        Red.physicsBody?.contactTestBitMask = PhysicsCatagory.Orange
        Red.physicsBody?.collisionBitMask = PhysicsCatagory.Orange
        Red.physicsBody?.dynamic = true
        Red.physicsBody?.affectedByGravity = false
        addChild(Red)

Any help would be extremely appreciated!

\$\endgroup\$
1
  • \$\begingroup\$ What about if the SKShapeNode is part of an array? Can you get collision detection between the SKSpriteNode and array of SKShapenodes? \$\endgroup\$
    – spapp
    Commented Aug 29, 2016 at 2:28

1 Answer 1

1
\$\begingroup\$

You are removing their children which they don't have and to remove them you have to remove them from Parent. You can find more about children and parent Here

func CollisionWithRed (Orange: SKSpriteNode, Red:SKShapeNode){
       Orange.removeFromParent()
       Red.removeFromParent()
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .