0

I would like to override the didBeginContact function so as to implement custom logic when two physics objects in my scene collide.

I understand how this would be done in Swift by first setting the contact delegate to be the scene object itself and then setting up the didBeginContact function as below:

self.physicsWorld.contactDelegate = self

func didBeginContact(contact: SKPhysicsContact) {
    //logic goes here
}

I am unable to work out how to implement such a system when using SpriteKit with Xamarin in C#. I have tried creating a custom Contact Delegate class which is a subclass of SKPhysicsContactDelegate and setting the scene's PhysicsWorld.ContactDelegate property to be an instance of this class but I noticed no change when running my application.

public class CollisionDelegate : SKPhysicsContactDelegate
{
    public override void DidBeginContact(SKPhysicsContact contact)
    {
        if (contact.BodyA.CategoryBitMask == 1 && 
contact.BodyB.CategoryBitMask == 3)
        {
            contact.BodyB.Node.RemoveFromParent();
        }
        else if (contact.BodyA.CategoryBitMask == 3 && 
contact.BodyB.CategoryBitMask == 1)
        {
            contact.BodyA.Node.RemoveFromParent();
        }
    }
}

Then in my SKScene class:

public override void DidMoveToView(SKView view)
{
    PhysicsWorld.ContactDelegate = new CollisionDelegate();
    //Other scene initilaisation 
}
Rafi Levy
  • 123
  • 1
  • 9
  • Delete your older question, it is never recommended to ask the same question twice. Nothing is wrong with your code, which means the problems like with how you set up the physics body. You need to show that info. – Knight0fDragon Jan 09 '18 at 01:42
  • No problem with the delegate, refer to [here](https://stackoverflow.com/questions/19675967/didbegincontactskphysicscontact-contact-not-invoked) – ColeX Jan 09 '18 at 06:46

0 Answers0