180 likes | 224 Views
Learn about GameComponents, DrawableGameComponents, adding components, creating interfaces, and using ServiceCollection for communication between components in XNA.
E N D
XNA Basic Component And ServiceCollection
GameComponent • Component is a class that developer design for apportion code. • There are two type of Component. • GameComponent • DrawableGameComponent
GameComponent • There are Initialize(),Update() and Consturctor. • You can add GameComponent in your solution by choosing template from Project menu.
First choose project menu • Second choose Add New Item. • Third choose XNA Game Studio. • Fourth choose game component.
DrawableGameComponent • There are Initialize(),Update(),LoadContent(),Draw() and Consturctor. • Unfortunately, there is not DrawableGameComponent template. • You must write it.
Creating DrawableGameComponent • Choose add class in solution.Assume class’s name is MyClass. class Myclass : Microsoft.Xna.Framework.DrawableGameComponent { }
Its constructor must be public MyClass(GameTime game):base(game) { }
Remain is writing method. Their modifier is public but LoadContent’s modifier is protected. • You must use XNA’s Library too. • It’s called inheritance. We don’t talk detial about it but you can find more information in wiki.
How to add component • You must add component in constructor. Assume my component is Getting_Input. private Getting_Input input; public Game1() { input = new Getting_Input(this); this.Component.Add(input); }
How to communicate between components? • The answer is ServiceCollection. • We’ll use ServiceCollection for call property from the other component. • The most service is interface. We’ll don’t talk detail about it but you can find more information in wiki.
How to create service • Assume there are component Graphic, component Display. • Component Display want model from component Graphic. • Therefor, I’ll create interface IGraphic for communicating with Display.
Pseudo Code interface IGraphic { model GetModeling { get;} }; class Graphic: Microsoft.Xna.Framework.GameComponent, IGraphic {
Model player; Model IGraphic.GetModeling { get { return player; } } public Graphic(GameTime game):base(game) { game.Service.Add(typeof(IGraphic),this); }
How to Display using service? Class Display : Microsoft.Xna.Framework.DrawableGameComponent { IGraphic graphic; Model player;
public Display(GameTime game):base(game) { graphic = (IGraphic)game.Service.GetService(typeof(Graphic)); player = graphic.GetModeling; }
Let’s practice more • Let improve program changeWithSound by component and service.
Vibration Keyboard Display GamePad sound This is how class work. SetVibration SetSpeedChangeColor SetSpeedChangeColor GetSound