Hello World example + Gamestate

Topics: User Forum
Dec 30, 2011 at 2:43 PM


To learn FPE I've been trying to port the Hello World example into a WP7 game that uses the MSDN gamestate example. When the level loads the circle just flies straight up and dissapears. The code is almost identical to the example, the only difference is the gamestate. Anyone had the same issue?

This is the code from my gameplay screen:

using System;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using GameStateManagement;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace Test.Screens
    /// <summary>
    /// This screen implements the actual game logic
    /// </summary>
    class GamePlayScreen : GameScreen
        private ContentManager content;
        private float pauseAlpha;
        private readonly World world;
        private Body groundBody;
        private Body circleBody;        
        private Texture2D groundSprite;
        private Texture2D circleSprite;
        // Simple camera controls
        private Matrix view;
        private Vector2 cameraPosition;
        private Vector2 screenCenter;

        private const float MeterInPixels = 64f;

        public GamePlayScreen()
            world = new World(new Vector2(0, 20));

        public override void Activate(bool instancePreserved)
            if (!instancePreserved)
                if (content == null)
                    content = new ContentManager(ScreenManager.Game.Services, "Content");

                // Initialize camera controls
                view = Matrix.Identity;
                cameraPosition = Vector2.Zero;

                screenCenter = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2f, ScreenManager.GraphicsDevice.Viewport.Height / 2f);

                groundSprite = content.Load<Texture2D>("groundSprite");
                circleSprite = content.Load<Texture2D>("circleSprite");

                /* Circle */
                // Convert screen center from pixels to meters
                Vector2 circlePosition = (screenCenter / MeterInPixels) + new Vector2(0, -1.5f);

                // Create the circle fixture
                circleBody = BodyFactory.CreateCircle(world, 96f / (2f * MeterInPixels), 1f, circlePosition);
                circleBody.BodyType = BodyType.Dynamic;

                // Give it some bounce and friction
                circleBody.Restitution = 0.3f;
                circleBody.Friction = 0.5f;

                //create the ground body
                /* Ground */
                Vector2 groundPosition = (screenCenter / MeterInPixels) + new Vector2(0, 1.25f);

                groundBody = BodyFactory.CreateRectangle(world, ScreenManager.GraphicsDevice.Viewport.Width, 62, 1f, groundPosition);
                groundBody.IsStatic = true;
                groundBody.Restitution = 0.3f;
                groundBody.Friction = 0.5f;

                // once the load has finished, we use ResetElapsedTime to tell the game's
                // timing mechanism that we have just finished a very long frame, and that
                // it should not try to catch up.

        public override void Deactivate()

        /// <summary>
        /// Unload graphics content used by the game.
        /// </summary>
        public override void Unload()

        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
            // Gradually fade in or out depending on whether we are covered by the pause screen.
            pauseAlpha = coveredByOtherScreen ? Math.Min(pauseAlpha + 1f / 32, 1) : Math.Max(pauseAlpha - 1f / 32, 0);

            if (IsActive)
                //update the world
                world.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);
            base.Update(gameTime, otherScreenHasFocus, false);

        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
            // This game has a blue background. Why? Because!
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            /* Circle position and rotation */
            // Convert physics position (meters) to screen coordinates (pixels)
            Vector2 circlePos = circleBody.Position * MeterInPixels;
            float circleRotation = circleBody.Rotation;

            /* Ground position and origin */
            Vector2 groundPos = groundBody.Position * MeterInPixels;
            Vector2 groundOrigin = new Vector2(groundSprite.Width / 2f, groundSprite.Height / 2f);

            // Align sprite center to body position
            Vector2 circleOrigin = new Vector2(circleSprite.Width / 2f, circleSprite.Height / 2f);

            spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, view);

            //Draw circle
            spriteBatch.Draw(circleSprite, circlePos, null, Color.White, circleRotation, circleOrigin, 1f, SpriteEffects.None, 0f);

            //Draw ground
            spriteBatch.Draw(groundSprite, groundPos, null, Color.White, 0f, groundOrigin, 1f, SpriteEffects.None, 0f);