0
\$\begingroup\$

Im trying to create a collision between to objects. one is a SKSpriteNode and the other is a SKShapenode. the Code I have so far for 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 extremly appreciated. Im just a young developer trying to learn the basics of sprite kit. Thank you and God Bless.

\$\endgroup\$
  • \$\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 Aug 29 '16 at 2:28
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.