ARTICLE

Movement of Text In Windows Phone 7

Posted by Mahak Gupta Articles | Windows Phone 8 November 02, 2012
In this example, we will be discussing the movement of text in both directions in Windows Phone 7.
Reader Level:
Download Files:
 

In this example, we will be discussing the movement of text in both directions in Windows Phone 7. Use the following to do that.

Step 1

First we create a new project in Windows Phone 7:

New Project -> XNA Game Studio 4.0 -> Windows Phone Game (4.0)

After that we select a name for the project.

Step 2

Now we add a SpriteFont Class to our project. For this first we right-click on the MovementOfTextInWP7Content (Content) like this:

MovementOfTextInWP7Content.jpg

Add -> New Item -> Sprite Font

After that a SpriteFont (SpriteFont1) class will be added.

Step 3

Now we will write the program in the Game1.cs page. Here we write the following code:

Public int x=0;

        public int y = 0;

        public int z = 0;

        public int c = 0;

        const float textSpeed= 500f;         

        const string TEXT = "Mahak Gupta";

 

        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;

        SpriteFont segoe14;

        Viewport viewport;

        Vector2 textSize;

        Vector2 textPosition;

        bool DirectionIsUp = false;

 

        public Game1()

        {

            graphics = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";

 

            // Frame rate is 30 fps by default for Windows Phone.

            TargetElapsedTime = TimeSpan.FromTicks(333333);

        }

Here x, z and c are variables used to fulfill some conditions. textSpeed is used for the speed measurement of the text.

Now we write the code for LoadContent like this:
 

protected override void LoadContent()

        {

            spriteBatch = new SpriteBatch(GraphicsDevice);

            viewport = this.GraphicsDevice.Viewport;

            segoe14 = this.Content.Load<SpriteFont>("SpriteFont1");

            textSize = segoe14.MeasureString(TEXT);

            textPosition = new Vector2((viewport.Width - textSize.X) / 2, (viewport.Height - textSize.Y) / 2);

        }
 

Here we describe the TextSize and the position of the text Like this:

Movement-of-Text-In-Windows-Phone7.jpg

Step 4

Now we write the Update Method. Here we describe the text in both directions (X and Y) like this:

 

protected override void Update(GameTime gameTime)

        {

            if (x < 3)

            {

                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

                    this.Exit();

 

 

                if (!DirectionIsUp)

                {

                    textPosition.X += textSpeed* (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (textPosition.X + textSize.X > viewport.Width)

                    {

                        float excess = textPosition.X + textSize.X - viewport.Width;

                        textPosition.X += 4* excess;

                        DirectionIsUp = true;

                        x += 1;

                        z += 1;

                    }

 

                }

                else

                {

                    textPosition.X -= textSpeed* (float)gameTime.ElapsedGameTime.TotalSeconds;

 

                    if (textPosition.X < 0)

                    {

                        float excess = -textPosition.X;

                        textPosition.X += 4 * excess;

                        DirectionIsUp = false;

                       

                    }

 

                }

             

            }

 

            else if (x >= 3)

            {

                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

                    this.Exit();

 

 

                if (!DirectionIsUp)

                {

                    textPosition.Y += textSpeed* (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (textPosition.Y + textSize.Y > viewport.Width)

                    {

                        float excess = textPosition.Y + textSize.Y - viewport.Width;

                        textPosition.Y += 4 * excess;

                        DirectionIsUp = true;

                       

                    }

 

                }

                else

                {

                    textPosition.Y -= textSpeed* (float)gameTime.ElapsedGameTime.TotalSeconds;

 

                    if (textPosition.Y < 0)

                    {

                        float excess = -textPosition.Y;

                        textPosition.Y += 4 * excess;

                        DirectionIsUp = false;

                        y += 1;

                    }

                    if (y == 3)

                    {

                        x = 0;

                    }

 

                }

 

 

            }

            base.Update(gameTime);

        }


Now we write the Draw Method, here we change the color of the text like this:
 

protected override void Draw(GameTime gameTime)

        {

            GraphicsDevice.Clear(Color.BurlyWood);

 

            spriteBatch.Begin();

           

            if (z <= 3)

            {

                spriteBatch.DrawString(segoe14, TEXT, textPosition, Color.Chocolate);

            }

            else if (z == 3)

            {

                z =0;

            }

 

            else

            {

                spriteBatch.DrawString(segoe14, TEXT, textPosition, Color.Cornsilk);

            }

                spriteBatch.End();

 

            base.Draw(gameTime);

        }

 output-Movement-of-Text-In-Windows-Phone7.jpg

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.