1
我正在嘗試學習C#和XNA,同時製作一個簡單的平臺遊戲(馬里奧式遊戲)。 我試圖用viewport實現滾動背景,我借用並調整了一個準備好的工作示例中的一些位代碼,但出於奇怪的原因,它陷入了一個點。 我認爲問題在於我指向視口中心的方式。你能幫我個忙嗎?無法讓我的視口在XNA遊戲中工作
這就是我的等級類
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();
}
}
}
很抱歉,但它並不完全是我的意思 - 我的問題是,當你旅行的權利。首先背景從屏幕中間開始,一旦我擊中它,玩家就會陷入困境,不得不離開去看他再次移動 – Swav
那就是截圖的鏈接:[link](http:// i901.photobucket的.com /專輯/ ac214/Swav1984/Jumpman_zps3a664992.jpg) – Swav