C# 消失的物体
因此,对于我的第一个MonoGame项目,我决定制作一个俄罗斯方块克隆,但我有一个问题,我不知道如何解决 目前,我的代码正在生成一个块并将其向下移动,直到它到达特定的y位置。该区块需要停留在该位置,并生成一个新区块。我用一个包含block类对象的列表来做这个,然后画出这个列表中的所有块 我取出了我认为与问题无关的零件:C# 消失的物体,c#,monogame,C#,Monogame,因此,对于我的第一个MonoGame项目,我决定制作一个俄罗斯方块克隆,但我有一个问题,我不知道如何解决 目前,我的代码正在生成一个块并将其向下移动,直到它到达特定的y位置。该区块需要停留在该位置,并生成一个新区块。我用一个包含block类对象的列表来做这个,然后画出这个列表中的所有块 我取出了我认为与问题无关的零件: public class PlayField : DrawableGameComponent { private Game1 gameRef;
public class PlayField : DrawableGameComponent
{
private Game1 gameRef;
private Texture2D fieldTexture;
private BlockGenerator blockGenerator;
private Texture2D[] allBlocks;
private Block currentBlock;
public bool[,] fieldFilled;
private int down_Blocks = 22;
private int side_Blocks = 10;
public List<Block> placedBlocks;
public PlayField(Game game) : base(game)
{
placedBlocks = new List<Block>();
allBlocks = new Texture2D[4];
blockGenerator = new BlockGenerator(allBlocks,gameRef);
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
try
{
if (currentBlock.isMoving == false)
{
placedBlocks.Add(currentBlock);
currentBlock = null;
currentBlock = blockGenerator.GenerateBlock();
}
else
{
currentBlock.UpdatePosition(gameTime);
if (InputManager.CheckForKeyBoardRelease(Keys.A))
{
currentBlock.MoveLeft();
}
if (InputManager.CheckForKeyBoardRelease(Keys.D))
{
currentBlock.MoveRight();
}
}
}
catch(NullReferenceException e)
{
currentBlock = blockGenerator.GenerateBlock();
}
}
public override void Draw(GameTime gameTime)
{
gameRef.SpriteBatch.Begin();
if(currentBlock != null)
{
currentBlock.DrawBlocks();
}
foreach(Block b in placedBlocks)
{
b.DrawBlocks();
}
gameRef.SpriteBatch.End();
base.Draw(gameTime);
}
然后将初始化代码从构造函数移到GenerateBlock(addvar)中
那么它应该会起作用。每次创建块时都会创建新向量,但每次都会重复使用相同的块位置实例,因此两个块确实存在,但始终具有完全相同的位置
我还没有时间测试,但我认为它是对的……让我知道:)
编辑:我还建议将blockPositions完全移动到Block类中。我认为(在你发布的代码中,也许你以后会有计划)在真正拥有它的类之外拥有这种逻辑没有任何价值……如果它一开始就包含在类中,你就可以避免这个问题。只是一个建议:)我想你也需要发布BlockGenerator类,我怀疑GenerateBlock的实现是相关的。这个
捕获(NullReferenceException e)
看起来很奇怪。如果编程不小心,则会发生NullReferenceException
。您必须在null
上检查它,而不是使用磁带。我还在努力掌握你的密码。太可惜了,我踩不动;-)我已将代码发布到BlockGenerator@Jeroen van Langen:是的,我只在第一次迭代中使用try/catch,因为创建PlayField时currentBlock将为null,因此我甚至在生成单个块之前都会得到异常^^^旧对象是否消失?新块不会显示吗?@Wikked如果(currentBlock==null)currentBlock=blockGenerator.GenerateBlock(),您可以添加代码>在方法的开头。然后您将摆脱try/catchThank(:进行了一些调整
public class Block : DrawableGameComponent
{
Game1 gameRef;
public Texture2D blockTexture;
public Vector2[] blockPositions;
TimeSpan lastMove;
TimeSpan blockMove = TimeSpan.FromMilliseconds(500);
public bool isMoving;
public Block(Game game, Texture2D _blockTexture, Vector2[] _blockPositions) : base(game)
{
gameRef = (Game1)game;
blockTexture = _blockTexture;
blockPositions = _blockPositions;
isMoving = true;
}
public void UpdatePosition(GameTime gameTime)
{
Vector2 bottomBlockPositon = FindBottomBlock();
if(bottomBlockPositon.Y < 550)
{
if (WaitTillMove(gameTime))
{
for (int i = 0; i < blockPositions.Length; i++)
{
blockPositions[i] = new Vector2(blockPositions[i].X, blockPositions[i].Y + 25);
}
}
}
else
{
isMoving = false;
Console.WriteLine("X: " +blockPositions[0].X + " Y:" + blockPositions[0].Y);
}
}
public Vector2 FindBottomBlock()
{
Vector2 result = new Vector2(0, 0);
for(int i = 0; i < blockPositions.Length; i++)
{
if(blockPositions[i].Y > result.Y)
{
result = blockPositions[i];
}
}
return result;
}
public bool WaitTillMove(GameTime gameTime)
{
if (lastMove + blockMove < gameTime.TotalGameTime)
{
lastMove = gameTime.TotalGameTime;
return true;
}
return false;
}
public void DrawBlocks()
{
gameRef.SpriteBatch.Draw(blockTexture, blockPositions[0], Color.White);
gameRef.SpriteBatch.Draw(blockTexture, blockPositions[1], Color.White);
gameRef.SpriteBatch.Draw(blockTexture, blockPositions[2], Color.White);
gameRef.SpriteBatch.Draw(blockTexture, blockPositions[3], Color.White);
}
}
public class BlockGenerator
{
Random random;
Texture2D[] allBlocks;
Vector2[] blockPositions;
Texture2D currentBlock;
BlockEnums currentBlockEnum;
Game1 gameRef;
public BlockGenerator(Texture2D[] blocks, Game1 game)
{
gameRef = (Game1)game;
allBlocks = blocks;
currentBlock = allBlocks[1];
blockPositions = new Vector2[4];
random = new Random();
}
public Block GenerateBlock()
{
int colorValue = random.Next(0, 4); //0 = blue, 1 = green, 2 = red, 3 = yellow
currentBlock = allBlocks[colorValue];
currentBlockEnum = BlockEnums.Line;
blockPositions[0] = new Vector2(100, 0);
blockPositions[1] = new Vector2(125, 0);
blockPositions[2] = new Vector2(150, 0);
blockPositions[3] = new Vector2(175, 0);
Block generatedBlock = new Block(gameRef,currentBlock, blockPositions);
return generatedBlock;
}
public class BlockGenerator
{
Random random;
Texture2D[] allBlocks;
Vector2[] blockPositions; // delete this
var blockPositions = new Vector2[4];