XNA 4.0을 처음 접했을 때 플레이어가 적에게 점프하여 죽일 수있는 슈퍼 마리오 브라더스 유형의 게임을 만들려고합니다. 그러나 나는 적을 죽이는 것에 문제가있다. 내 캐릭터 (rectangleBox
) 아래에 15px 직사각형을 만들어 적의 사각형과 교차하면 enemy.alive = false
이 발생합니다. 이 경우 enemy.alive = false
적을 뽑지 않습니다. 그러나 이것은 직사각형이 교차하는 때에 만 적용됩니다. 적군이 rectangleBox
의 경계를 떠나면 다시 나타납니다. 적을 영구히 삭제하여 게임을 다시 시작할 때까지 다시 부활시키지 않으려면 어떻게합니까?XNA에서 적의 스프라이트를 영구히 삭제하는 방법
적 클래스 코드 :
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);
}
}
}
조언 해 주셔서 감사합니다. Enemy.cs에서 Alive = true로 설정 한 다음 Game1.cs에서 초기화하지 않고 올바른 방향으로 이끌어주었습니다. – FLAVAred