Úvod HTML CSS PHP MySQL JavaScript
komponenty Komponenta je co nejvíce samostatná část hry s co nejmenším počtem závislostí, která má metody jako samotná třída Game - Initialize(), LoadContent(), Update(), Draw().

Po vytvoření komponenty

(pravým na projekt v Solution Exploreru -> Add -> New Item -> XNA Game Studio 4.0 -> GameComponent) se vygeneruje:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace gamekomponenty.komponenty
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>  
    public class GameComponent1 : Microsoft.Xna.Framework.GameComponent
    {
        public GameComponent1(Game game)
            : base(game)
        {
             // TODO: Construct any child components here 
        }

         /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>  
        public override void Initialize()
        {
             // TODO: Add your initialization code here  

            base.Initialize();
        }

         /// <summary>
        /// Allows the game component to update itself.
        /// </summary> 
        /// <param name="gameTime"> Provides a snapshot of timing values.</param>     
        public override void Update(GameTime gameTime)
        {
             // TODO: Add your update code here   

            base.Update(gameTime);
        }
    }
}
Tato komponenta má metodu Update()v které stejně jako v třídě Game probíhá nějaký kód, ale neumí vykreslovat.

Pokud chceme v komponentě vykreslovat musíme ji doplnit o další metody a dopsat typ komponenty na
public class GameComponent2 : Microsoft.Xna.Framework.DrawableGameComponent.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace gamekomponenty.komponenty
{

    ///<summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>      
    public class GameComponent2 : Microsoft.Xna.Framework.DrawableGameComponent // dopsáno Drawable  
    {    private SpriteFont font;
        private Hra hra;
        public GameComponent2(Hra hra)
            : base(hra)            
        {
            // TODO: Construct any child components here  
             this.hra = hra;  //nastavení na zakladní třídu Hra
          
        }

       /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>            
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

         /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        ///         
        protected override void LoadContent()
        {
            font = hra.Content.Load< SpriteFont >("SpriteFont1"); 
            base.LoadContent();
        }


        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here  

            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            hra.spriteBatch.Begin();
            hra.spriteBatch.DrawString(font, "ahoj", new Vector2(100, 100), Color.White);
            hra.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

 

Druhý způsob nastavení componenty

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Had.components
{
    public class Score :DrawableGameComponent
    {
       public int Value { get; set; }
       ContentManager contentManager;
       SpriteBatch spriteBatch;
       SpriteFont spriteFond;

        public Score(Game game)
           : base(game) 
       {
           
           contentManager = new ContentManager(game.Services);
           contentManager.RootDirectory = "Content";
           

       }

       protected override void LoadContent()
       {
           spriteBatch = new SpriteBatch(Game.GraphicsDevice);
           spriteFond = contentManager.Load < SpriteFont >("ScoreFont");


       }

       protected override void UnloadContent()
       {
           contentManager.Unload();
          
       }
       public override void Draw(GameTime gameTime)
       {
           spriteBatch.Begin();

           spriteBatch.DrawString(spriteFond,Value.ToString(),new Vector2(10f,0f),Color.White);
            
           spriteBatch.End();

         
          
       }
    }
}

 
 
 
 

Připojení komponent ke hře

V základní třídě Hra v Initialize() vytvoříme komponenty .
Přidáme je v pořadí v jakém se mají obnovovat a vykreslovat.
    
    ...
  
     protected override void Initialize()
        { 
            // TODO: Add your initialization logic here   

            GameComponent1 komponenta1 = new GameComponent1(this);
            GameComponent2 komponenta2 = new GameComponent2(this);
            
            Components.Add(komponenta2);   
            Components.Add(komponenta1); 
            
            base.Initialize();
            
        } 
        
    ...   
  
pravy Ascii tabulka
Barvy
Klávesové zkratky


©2013-2021 Trojklik.8u.cz Autor: Vašek D.