无法让我的视口在XNA Game中运行

时间:2013-11-26 11:55:28

标签: c# xna viewport

我正在尝试学习C#和XNA,同时制作一个简单的平台游戏(Mario Type游戏)。 我试图用视口实现滚动背景,我从一个准备工作的例子中借用并调整了一些位代码,但由于奇怪的原因,它被卡在一个点上。 我认为问题在于我指向视口中心的方式。你能帮帮我们吗??

这是我的Level级

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace JumpMan
{
   public class Level
    {
        //game content manager
        private ContentManager content;
        public ContentManager Content
            {
                get { return content; }
            }

            // level of the ground in the game
            private int groundLevel = 192;
            public int GroundLevel
            {
                get { return groundLevel; }
            }

            //Gravitation in the game - pulling JumpMan down
            private float gravitation = 1.4f;
        public float Gravitation
        {
            get { return gravitation; }
        }

        //Current game to which this level belongs
        private Game1 game;

        // Player
        private JumpMan jumpMan;
        //Amount of lives
        public int Live;

        /background texture for this level
        private Texture2D backgroundTexture;
        public Texture2D BackgroundTexture
        {
            get { return backgroundTexture; }
        }

        //viewport size and centre
        Vector2 viewPortCentre;
        Vector2 viewPortSize;

        //Prepare the level
        public Level(Game1 game)
        {
            //store the game referance
            this.game = game;
            content = game.Content;


            //build the level
            LoadContent();
            Initialize();

        }

        //Load game level content
        private void LoadContent()
        {
            backgroundTexture = Content.Load<Texture2D>(@"Background");


        }  

        //Initialise level 
        private void Initialize()
        { 
            // Resize the back buffer to match the loaded level size
            game.Graphics.PreferredBackBufferWidth = backgroundTexture.Width;
            game.Graphics.PreferredBackBufferHeight = backgroundTexture.Height;
            game.Graphics.ApplyChanges();


            viewPortSize = new Vector2(
                game.Graphics.GraphicsDevice.Viewport.Width,
                game.Graphics.GraphicsDevice.Viewport.Height);
            viewPortCentre = new Vector2(0.0f, viewPortSize.Y / 2.0f);


            //Set Jumpmans number of lives
            Live = 3;
            //Initialise the JumpMan class (create JumpMan)
            jumpMan = new JumpMan(this);


        }

        //Update game level


        /// Update the game level
        public void Update(GameTime gameTime)
        {
            //Update JumpMan
            jumpMan.Update(gameTime);
        }


        //Draw the level
        public void Draw(GameTime gameTime)
        {

            //Determine the width of the viewport
           // int viewPortSize = game.Graphics.GraphicsDevice.Viewport.Width;

            // Draw level textures
            SpriteBatch spriteBatch = game.SpriteBatch;
            spriteBatch.Begin();

            viewPortCentre.X = jumpMan.JumpManPosition.X;
            if (viewPortCentre.X < viewPortSize.X / 2)
                viewPortCentre.X = viewPortSize.X / 2;

            // Determine the start location of the background relative to the viewport
            int backgroundStartX = (int)(viewPortSize.X - viewPortSize.X / 2.0f) % backgroundTexture.Width;

            // Draw out the first slices
            //spriteBatch.Draw(backgroundTexture, new Vector2(-backgroundStartX, 0.0f), Color.White);

            // If needed draw out a second slice
            if (backgroundTexture.Width - backgroundStartX < viewPortSize.X)
                spriteBatch.Draw(backgroundTexture, new Vector2(backgroundTexture.Width - backgroundStartX, 0.0f), Color.White);

            // Draw JumpMan by calling JumpMan class
            jumpMan.Draw(spriteBatch, viewPortSize, viewPortCentre, gameTime);

            spriteBatch.End();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

当您的viewPortCenter.X为负数时会发生什么?假设您的viewPortSize.X等于800,并且您的viewPortCenter.X为-1000。以下if语句将评估为true:

if (viewPortCentre.X < viewPortSize.X / 2)
    viewPortCentre.X = viewPortSize.X / 2;

基本上,如果你向左走得足够远,你最终会看到你的背景“坚持”。

您可以使用以下方法快速解决此问题:

if (Math.Abs(viewPortCenter.X) < viewPortSize.X / 2)
    viewPortCentre.X = viewPortSize.X / 2;