Collision handling not working?

Jun 10, 2011 at 11:10 AM

Hi

I've used the suggested code below to intercept a collision in order to do some custom handling. However, it generates the error "No overload for '_physicsController_Collision' matches delegate 'Spritehand.FarseerHelper.PhysicsControllerMain.CollisionHandler'". Has something changed in the handler and, if so, what's the new method call?

Many thanks,

Jerry

 

public partial class MainPage : UserControl
{
    PhysicsControllerMain _physicsController;

    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        _physicsController = canvas.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
        _physicsController.Collision += new PhysicsControllerMain.CollisionHandler(_physicsController_Collision); // THIS LINE GENERATES THE ERROR SHOWN ABOVE
    }

    void _physicsController_Collision(string sprite1, string sprite2)
    {
        if (sprite1 == "cnvShip" && sprite2.StartsWith("cnvBug"))
        {
            // your collision behavior here
        }
    }
}
Jun 10, 2011 at 3:17 PM

Think your args on the event handler might be wrong:

_physicsController_Collision(PhysicsSprite sprite1, PhysicsSprite sprite2)

Jun 10, 2011 at 4:48 PM

Spruce

Ace, thanks for that and your prompt response. Sorted.

So for anyone else looking at the original code sample (at http://physicshelper.codeplex.com/discussions/63761), the updated version that works is:

public partial class MainPage : UserControl
{
    PhysicsControllerMain _physicsController;

    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        _physicsController = canvas.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
        _physicsController.Collision += new PhysicsControllerMain.CollisionHandler(_physicsController_Collision); 
    }

    void _physicsController_Collision(PhysicsSprite sprite1, PhysicsSprite sprite2)
    {
        if (sprite1.Name == "cnvShip" && sprite2.Name.StartsWith("cnvBug"))
        {
            // your collision behavior here
        }
    }
}