In the name of the sprite...

May 11, 2011 at 9:07 AM

Hi all,

I'm trying to set collisiongroups via code. The problem I have is to get the name of the sprite as opposed to the name of the control.

XAML: 
<Ellipse Height="100" Name="elluniquename" Canvas.Left="270" Canvas.Top="183" Width="25" StrokeThickness="10" CacheMode="BitmapCache">
            <i:Interaction.Behaviors>
                <pb:PhysicsObjectBehavior CollisionGroup="5"/>
            </i:Interaction.Behaviors>
            <Ellipse.Fill>
                <RadialGradientBrush>
                    <GradientStop Color="#FF474956" Offset="1"/>
                    <GradientStop Color="#FFDCC9CF"/>
                </RadialGradientBrush>
            </Ellipse.Fill>
            <Ellipse.Effect>
                <DropShadowEffect Direction="0" ShadowDepth="0" BlurRadius="100" Color="#FF00FFF8" Opacity="20"/>
            </Ellipse.Effect>
        </Ellipse>
      

CODE:
PhysicsObjectMain physObject = ctl.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain;
                    if (Variables.lvl == 6)
                    {

                        if (ctl.Name.StartsWith("elluniquename"))
                        {
                            PhysicsSprite sprite1 = GameControllerBehavior._physicsController.PhysicsObjects[ctl.Name];
                            foreach (Fixture f in sprite1.BodyObject.FixtureList)
                                f.CollisionFilter.CollisionGroup = 5;
                        }
                  
On the line starting with "PhysicsSprite sprite1 =" I get an error msg saying "The given key was not present in the dictionary"
I've been told to use the name of the sprite, and not the control. Given the code above, what is the name of the sprite?

Thanks a lot for any replies,
Pettrer

Coordinator
May 11, 2011 at 1:44 PM

When you get the error, go to the Watch Window and check the _physicsController.PhysicsObjects dictionary (you can look at the keys like so):

_physicsController.PhysicsObjects.Keys.ElementAt(0)

... just change the (0) to 1,2,3, etc. Or better yet add a debug method that writes them out to the debug window.

This will show you all of your sprite key values that are valid to access.

May 13, 2011 at 9:16 AM

Hi,

Thanks, but I get an error msg saying that there is no ElementAt for _physicsController.PhysicsObjects.Keys in .KeyCollection.

This seems true to me when I manually go to the Keys. The only things I get are Count and non-public members.

Sorry if this is more of a Visual Studio issue than anything else, but as I said, I can't find an ElementAt property.

Pettrer

Coordinator
May 13, 2011 at 2:10 PM

You should wait until the PhysicsController.Initialized event fires...

_physicsController.Initialized += new PhysicsControllerMain.InitializedHandler(_physicsController_Initialized);

 

 

 

void _physicsController_Initialized(object source)

{

// here you should be able to access the sprite dictionary.

}

 

 

May 14, 2011 at 9:09 AM

Wow, it actually DID work! I was just awazed when I looked at it and the object passed through the other object!

Here's my code in case anyone needs it (I'm sure there are better ways to write it though):



   //collisiongroups, level 6
            if (Variables.lvl == 6)
            for (int i = _cnvGame.Children.Count - 1; i >= 0; i--)
            {
                FrameworkElement ctl = _cnvGame.Children[i] as FrameworkElement;


                if (ctl.Name.StartsWith("ell"))  //the ellipses
                {
                    PhysicsSprite sprite1 = GameControllerBehavior._physicsController.PhysicsObjects[ctl.Name];
                    foreach (Fixture f in sprite1.BodyObject.FixtureList)
                        f.CollisionFilter.CollisionGroup = 5;
                }
                if (ctl.Name.StartsWith("iObject")) //the object that should be able to run through an ellipse
                {
                    PhysicsSprite sprite1 = GameControllerBehavior._physicsController.PhysicsObjects["iObject"];
                    foreach (Fixture f in sprite1.BodyObject.FixtureList)
                        f.CollisionFilter.CollisionGroup = 5;
                }
            }