问题描述
我将所有绘制方法封装到单独的类中-每种颜色一个.颜色应该在每个级别后都会改变.例如,级别1将为红色Lvl. 2橙色,Lvl. 3琥珀色(金),依此类推.
我的Game1类或Draw_Red,Draw_Orange ...类中没有错误,但是每当我运行游戏时,都会收到NullReferenceException未处理的错误.
我没有继承所有Game1变量(这是我在旧代码中所做的工作),而是将与绘图相关的所有变量复制并粘贴到了颜色类中.我还添加了LoadContent()方法,以便游戏在调用该类后将加载图形.我不知道缺少NullReferenceException,但是我开始怀疑我是否在所有Draw()方法的括号中都需要"SpriteBatch spriteBatch".如果可以,为什么需要在代码顶部调用SpriteBatch类?任何见解都很棒!
谢谢!
I encapsulated all of my drawing methods into separate classes - one for each color. The color is supposed to change after every level. For example, Level 1 would be red, Lvl. 2 orange, Lvl. 3 amber (gold) and so on.
I have no errors in my Game1 class or my Draw_Red, Draw_Orange... classes, but whenever I run the game, I get a NullReferenceException Unhandled error.
Rather than inheriting all Game1 variables, which is what I was doing in my old code, I copied and pasted any variables relative to drawing into the color classes. I also added a LoadContent() method so the game would load the graphics after calling on the class. I don''t know what I''m missing to get a NullReferenceException, but I''m starting to wonder if I need "SpriteBatch spriteBatch" in the parentheses of all my Draw() methods. If I do, why do I need to call the SpriteBatch class in at the top of my code? Any insight would be awesome!
Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Isaak_FloodControl
{
class Draw_Red:Microsoft.Xna.Framework.Game
{
SpriteBatch spriteBatch;
//The size of the game window
const int windowWidth = 800;
const int windowHeight = 600;
Texture2D Background;
Texture2D Pieces;
//Call, but do not create a new GameBoard
GameBoard gameBoard;
//Creates spaces where the game pieces will go
public Rectangle EmptyPiece = new Rectangle(0, 288, 48, 48);
//The size in pixels for the water graphic
public const int MaxWaterHeight = 192;
public const int WaterWidth = 192;
//The position of the filled water image
public Vector2 waterOverlayStart = new Vector2(112, 320);
//The location where the water will be drawn
public Vector2 waterPosition = new Vector2(576, 336);
//Stores information about current flood
float floodCount = 0.0f;
public void LoadContent(ContentManager myContent)
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Background = myContent.Load<texture2d>
(@"Textures\Backgrounds\Bkg_Red");
Pieces = myContent.Load<texture2d>
(@"Textures\Tile_Sheets\Ts_Red");
}
public void DrawBackground()
{
//Red
spriteBatch.Draw(Background,
new Rectangle(0, 0,
windowWidth, windowHeight),
Color.White);
}
public void DrawAnimatedWater()
{
int waterHeight = (int)(MaxWaterHeight * (floodCount / 100));
//Animate the Red water
spriteBatch.Draw(Background,
new Rectangle(
(int)waterPosition.X,
(int)waterPosition.Y + (MaxWaterHeight - waterHeight),
WaterWidth, waterHeight),
new Rectangle(
(int)waterOverlayStart.X,
(int)waterOverlayStart.Y + (MaxWaterHeight - waterHeight),
WaterWidth, waterHeight), Color.White);
}
//Draws an empty piece at a specific x and y location
public void DrawEmptyPiece(int pixelX, int pixelY)
{
//Red
spriteBatch.Draw(
Pieces,
new Rectangle(pixelX, pixelY,
GamePiece.PieceWidth, GamePiece.PieceHeight),
EmptyPiece, Color.White);
}
//Draws static unanimated piece at a specific x and y location
public void DrawStandardPiece(int x, int y, int pixelX, int pixelY)
{
//Red
spriteBatch.Draw(
Pieces, new Rectangle(pixelX, pixelY,
GamePiece.PieceWidth, GamePiece.PieceHeight),
gameBoard.GetSourceRect(x, y),
Color.White);
}
//Draws a falling piece at a specific x and y location
public void DrawFallingPiece(int pixelX, int pixelY, string positionName)
{
//Red
spriteBatch.Draw(
Pieces,
new Rectangle(pixelX, pixelY -
gameBoard.fallingPieces[positionName].VerticalOffset,
GamePiece.PieceWidth, GamePiece.PieceHeight),
gameBoard.fallingPieces[positionName].GetSourceRect(),
Color.White);
}
//Draws a fading piece at a specific x and y location
public void DrawFadingPiece(int pixelX, int pixelY, string positionName)
{
//Red
spriteBatch.Draw(
Pieces,
new Rectangle(pixelX, pixelY,
GamePiece.PieceWidth, GamePiece.PieceHeight),
gameBoard.fadingPieces[positionName].GetSourceRect(),
Color.White *
gameBoard.fadingPieces[positionName].alphaLevel);
}
//Draws a rotating piece at a specific x and y location
public void DrawRotatingPiece(int pixelX, int pixelY, string positionName)
{
//Red
spriteBatch.Draw(
Pieces,
new Rectangle(pixelX + (GamePiece.PieceWidth / 2),
pixelY + (GamePiece.PieceHeight / 2),
GamePiece.PieceWidth, GamePiece.PieceHeight),
gameBoard.rotatingPieces[positionName].GetSourceRect(),
Color.White,
gameBoard.rotatingPieces[positionName].RotationAmount,
new Vector2(GamePiece.PieceWidth / 2,
GamePiece.PieceHeight / 2),
SpriteEffects.None, 0.0f);
}
}
}
</texture2d></texture2d>
推荐答案
这篇关于NullReferenceException用于在单独的类中绘制精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!