Displaying text letter by letter

Posted by Evi on Game Development See other posts from Game Development or by Evi
Published on 2012-10-11T20:03:39Z Indexed on 2012/10/11 21:49 UTC
Read the original article Hit count: 318

Filed under:
|
|

I am planing to Write a Text adventure and I don't know how to make the text draw letter by letter in any other way than changing the variable from h to he to hel to hell to hello That would be a terrible amount of work since there are tons of dialogue.

Here is the source code so far

{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D sampleBG;
        Texture2D TextBG;
        SpriteFont defaultfont;
        KeyboardState keyboardstate;

        public bool spacepress = false;
        public bool mspress = false;

        public int textheight = 425;
        public int rowspace = 40;

        public string namebox = "(null)";

        public string Row1 = "(null)";
        public string Row2 = "(null)";
        public string Row3 = "(null)";
        public string Row4 = "(null)";

        public int Dialogue = 0;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;
            IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            sampleBG = Content.Load <Texture2D>("SampleBG");
            defaultfont = Content.Load<SpriteFont>("SpriteFont1");
            TextBG = Content.Load<Texture2D>("textbg");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboardstate = Keyboard.GetState();
            MouseState mousestate = Mouse.GetState();

            // Changes Dialgue by pressing Left Mouse Button or Space
            #region Dialogue changer
            if (mousestate.LeftButton == ButtonState.Pressed && mspress == false)
            {
                mspress = true;
                Dialogue = Dialogue + 1;
            }
            if (mousestate.LeftButton == ButtonState.Released && mspress == true)
            {
                mspress = false;
            }

            if (keyboardstate.IsKeyDown(Keys.Space) && spacepress == false)
            {
                spacepress = true;
                Dialogue = Dialogue + 1;
            }

            if (keyboardstate.IsKeyUp(Keys.Space) && spacepress == true)
            {
                spacepress = false;
            }
            #endregion
            // ------------------------------------------------------

            // Dialgue Content
            #region Dialgue



            if (Dialogue == 1)
            {
                Row1 = "Input Text 1 Here.";
                Row2 = "Input Text 2 Here.";
                Row3 = "Input Text 3 Here.";
                Row4 = "Input Text 4 Here.";
            }

            if (Dialogue == 2)
            {
                Row1 = "Text 1";
                Row2 = "Text 2";
                Row3 = "Text 3";
                Row4 = "Text 4";
            }

            #endregion
            // ------------------------------------------------------

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(sampleBG, new Rectangle(0, 0, 800, 600), Color.White);
            spriteBatch.Draw(TextBG, new Rectangle(0, 400, 800, 200), Color.White);
            spriteBatch.DrawString(defaultfont, Row1, new Vector2(10, (textheight + (rowspace * 0))), Color.Black);
            spriteBatch.DrawString(defaultfont, Row2, new Vector2(10, (textheight + (rowspace * 1))), Color.Black);
            spriteBatch.DrawString(defaultfont, Row3, new Vector2(10, (textheight + (rowspace * 2))), Color.Black);
            spriteBatch.DrawString(defaultfont, Row4, new Vector2(10, (textheight + (rowspace * 3))), Color.Black);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

© Game Development or respective owner

Related posts about XNA

Related posts about c#