我正在制作一个游戏,玩家会到处收集猕猴桃。当玩家接触到奇异果时,我希望奇异果精灵永久消失。到目前为止,我只能让猕猴桃精灵在玩家触摸时消失,当玩家离开猕猴桃长方形时,猕猴桃精灵会重新出现。
这是我在Kiwi类中创建的交叉函数:
public void Intersect(Rectangle playerRect, SpriteBatch spriteBatch)
{
foreach (Rectangle kiwiRect in kiwiRectangle)
{
if (!kiwiRect.Intersects(playerRect))
{
isCollected = false;
}
else
isCollected = true;
if (!isCollected)
{
spriteBatch.Draw(kiwiTexture, kiwiRect, Color.White);
}
}
}
然后我把这个函数放在主游戏类的Draw函数中。
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
if (gameState == GameState.Playing)
{
GraphicsDevice.Clear(Color.Green);
kiwiClass.Intersect(Sprite.spriteDestinationRectangle, spriteBatch);
Sprite.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
答案 0 :(得分:0)
你应该只检查是否有一个交叉点,直到有一个交叉点。你不想做isCollected = false
,否则奇异鸟就会重新出现。
您的Intersect()
方法应如下所示:
public void Intersect(Rectangle rectangle, SpriteBatch spriteBatch)
{
foreach (Rectangle kiwiRect in kiwiRectangle)
{
if (!isCollected)
{
if (kiwiRect.Intersects(playerRect))
{
isCollected = true;
}
}
if (!isCollected)
{
spriteBatch.Draw(kiwiTexture, kiwiRect, Color.White);
}
}
}
编辑:实际上,您需要分别跟踪每个奇异果的收集状态。您可以将isCollected
更改为列表:
List<Rectangle> collectedKiwis = new List<Rectangle>();
然后将Intersect()
方法更改为:
public void Intersect(Rectangle rectangle, SpriteBatch spriteBatch)
{
foreach (Rectangle kiwiRect in kiwiRectangle.Except(collectedKiwis))
{
if (kiwiRect.Intersects(playerRect))
{
collectedKiwis.Add(kiwiRect);
}
else
{
spriteBatch.Draw(kiwiTexture, kiwiRect, Color.White);
}
}
}