Xna Samples Project how to reference input from advancedDemo1

Topics: Developer Forum, User Forum
Nov 14, 2012 at 3:22 AM
Edited Nov 14, 2012 at 3:27 AM

How do you reference the main inputhelper from loadcontent in advancedDemo1

The reason I ask is that I am trying to make the cursor dissapear once I launch advencedDemo1 and so far the only way I can do this is adding input.ShowCursor = false; to advancedDemo1 handleinput.

This seems a bad way to do it as I figure I should be able to just set it off once rather then set it off every single time handleinput is called.

 

Second Question

How do I access components I add in FarseerPhysicsGame.cs public FarseerPhysicsGame() or publics in that class for that matter? (Components.Add)

 

Thanks

Developer
Nov 14, 2012 at 2:13 PM

Hi,

each FarseerPhysicsGameScreen has some properties. One is "HasCursor". Set it to false in the screen's constructor and it won't show up. Take a look at GameScreen1's constructor which does exactly that.

Regarding your second question: The Game object holds all components in Game.Components. A reference to the Screenmanager is passed to each game screen from which the FarseerPhysicsGameScreen is derived. So from within any screen class you can access other components via "ScreenManager.Game.Components", which returns a list of all components.

Nov 15, 2012 at 12:12 AM
Edited Nov 15, 2012 at 12:14 AM

First off thank you for responding. I greatly appreciate the help.

each FarseerPhysicsGameScreen has some properties. One is "HasCursor". Set it to false in the screen's constructor and it won't show up. Take a look at GameScreen1's constructor which does exactly that.

YOU ARE AMAZING!!! This is exactly what I was looking for!!!

Regarding your second question: The Game object holds all components in Game.Components. A reference to the Screenmanager is passed to each game screen from which the FarseerPhysicsGameScreen is derived. So from within any screen class you can access other components via "ScreenManager.Game.Components", which returns a list of all components.

I will look into this, i don't fully understand but I will spend some more time on it. Obviously I can seen where to get ScreenManager.Game.Components, what I don't get yet is how do I access something from it. forexample, still using the samples xna, if I added a public int Testingint = 0; to farseerPhysicsGame.cs. How do reference that? Thanks again

Nov 15, 2012 at 12:28 AM

Interesting so when I loop through

foreach (var list in ScreenManager.Game.Components)
            {
                //I can see Screenmanager and framerateCounter in the components list
            }
However I still cant see a way to get a specific component.

Since, when I try

foreach (var list in ScreenManager.Game.Components)
{
        if(typeof(list) == typeof(thingimlookingfor)
             {
                  //stuff
               }
}

I get an error the type or namespace of list could not be found.

Am I doing this the totally wrong way?

Nov 16, 2012 at 2:47 AM
Edited Nov 16, 2012 at 2:55 AM

OK so I found the solution http://xboxforums.create.msdn.com/forums/t/46117.aspx

In 

    public class FarseerPhysicsGame : Game
    {
        //Add

        private static FarseerPhysicsGame game_object;
In
        public FarseerPhysicsGame()
        {
	//add
            game_object = this;
Finally at the end of FarseerPhysicsGame: Game add
        public static T GetComponent<T>() where T : GameComponent
        {
            foreach (GameComponent gc in game_object.Components) // game_object is my static singleton, assigned in the Game constructor
            {
                if (gc.GetType().Equals(typeof(T)))
                {
                    return (T)gc;
                }
            }

            return null;
        }

        public static T GetService<T>()
        {
            return (T)game_object.Services.GetService(typeof(T));
        }

Then just call it from advancedDemo1

 //example

FarseerPhysicsGame

.GetComponent<Krypton.KryptonEngine>().AmbientColor = new Color (200, 200, 200);