我是 XNA 4.0 的新手,我正在尝试制作一款超级马里奥兄弟类型的游戏,玩家可以跳下敌人杀死他们。但是我遇到了杀死敌人的问题。我在我的角色 ( rectangleBox
) 下方制作了一个 15px 的矩形,这样如果它与敌人的矩形相交,就会导致enemy.alive = false
. 如果这样enemy.alive = false
,它就不会吸引敌人。然而,这只适用于矩形相交的时间。一旦敌人离开边界,rectangleBox
它就会再次出现。如何永久删除敌人,使其在我重新开始游戏之前不会再次重生?
敌人等级代码:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace notDMIG
{
class Enemy
{
public Texture2D texture;
public Rectangle bounds;
public Vector2 position;
public Vector2 velocity;
public float timer = 0.0f;
public int spriteNum = 1;
public int maxSpriteNum;
public bool alive;
public Enemy(Texture2D Texture, Vector2 Position, Vector2 Velocity, int maxsprites)
{
texture = Texture;
position = Position;
velocity = Velocity;
maxSpriteNum = maxsprites;
alive = false;
}
}
}
Game1.cs 敌人相关代码
protected override void Update(GameTime gameTime)
{
foreach (Enemy enemy in Enemies)
{
enemy.alive = true;
Rectangle rectangleBox = new Rectangle((int)player.position.X, (int)player.position.Y + player.sprite.Height + 15, player.sprite.Width, 1);
Rectangle enemyBox = new Rectangle((int)enemy.position.X, (int)enemy.position.Y, enemy.texture.Width, enemy.texture.Height);
if (enemy.alive == true)
{
if (rectangleBox.Intersects(enemyBox))
{
enemy.alive = false;
continue;
}
}
}
}
protected override void Draw(GameTime gameTime)
{
foreach (Enemy enemy in Enemies)
{
if (enemy.alive == true)
{
spriteBatch.Draw(enemy.texture, enemy.position, Color.White);
}
}
}