Problem adding PhysicsKeyTrigger programmatically

Feb 16, 2010 at 1:34 PM

I'm trying to set up a trigger in code using this code snippet:

PhysicsKeyTrigger frontTrigger = new PhysicsKeyTrigger();
frontTrigger.Key = Key.Right;
PhysicsApplyTorqueBehavior frontTorque = new PhysicsApplyTorqueBehavior();
frontTorque.TorqueValue = 5000;
frontTrigger.Actions.Add(frontTorque);
frontWheel.Triggers.Add(frontTrigger);

However, the last line of code throws a compiler error:

"cannot convert from 'Spritehand.PhysicsBehaviors.PhysicsKeyTrigger' to 'System.Windows.TriggerBase' 

What other way could you possibly do it?

-Mike

 

Coordinator
Feb 16, 2010 at 2:22 PM
Hi Mike,

Although I think we can fix your code sample (below), I think we will still be stuck :( because the PhysicsKeyTrigger does its work when the AssociatedObject is loaded. Since your object is likely already loaded at the time your code executes, it won't attach to the proper events. Unless you are already adding the object dynamically as well...

PhysicsApplyTorqueBehavior frontTorque = new PhysicsApplyTorqueBehavior();
frontTorque.TorqueValue = 5000;

PhysicsKeyTrigger frontTrigger = new PhysicsKeyTrigger();
frontTrigger.Key = Key.Right;
frontTrigger.Actions.Add(frontTorque);

System.Windows.Interactivity.TriggerCollection triggerCollection = Interaction.GetTriggers(ball);
triggerCollection.Add(frontTrigger);



On Feb 16, 2010, kindohm <notifications@codeplex.com> wrote:

From: kindohm

I'm trying to set up a trigger in code using this code snippet:

PhysicsKeyTrigger frontTrigger = new PhysicsKeyTrigger();
frontTrigger.Key = Key.Right;
PhysicsApplyTorqueBehavior frontTorque = new PhysicsApplyTorqueBehavior();
frontTorque.TorqueValue = 5000;
frontTrigger.Actions.Add(frontTorque);
frontWheel.Triggers.Add(frontTrigger);

However, the last line of code throws a compiler error:

"cannot convert from 'Spritehand.PhysicsBehaviors.PhysicsKeyTrigger' to 'System.Windows.TriggerBase'

What other way could you possibly do it?

-Mike