나는 하향식 슈팅 게임을하고 있으며, 적들은 아무렇지도 않게 돌아 다니 길 원합니다. 이 코드에서는 메인 클래스의 무작위 (rnd)를 포함한 모든 변수 (회전, 위치 등)가있는 Enemy 클래스가 있습니다.무작위 적의 이동 방향
내 문제는 적들이 화면의 오른쪽 상단에서 왼쪽 하단으로 계속 이동한다는 것입니다. 그래서 그들은 주위를 방황하지는 않지만 항상 어떤 방향을 취합니다. 왜 그런지 알고 싶습니다.
refresh += gameTime.ElapsedGameTime.TotalSeconds;
if (refresh >= 3)
{
refresh -= 3;
int rndDirection = rnd.Next(4);
if (rndDirection == 1) vektor = new Vector2(0, -1);
if (rndDirection == 2) vektor = new Vector2(0, 1);
if (rndDirection == 3) vektor = new Vector2(1, 0);
if (rndDirection == 4) vektor = new Vector2(-1, 0);
foreach (Enemy enemy in enemies)
{
Vector2 rndDirVec = vektor;
enemy.directionX = enemy.position.X - rndDirVec.X;
enemy.directionY = enemy.position.Y + rndDirVec.Y;
enemy.rotation = (float)Math.Atan2(enemy.directionY, enemy.directionX);
}
}
foreach (Enemy enemy in enemies)
{
enemy.position -= new Vector2((float)(enemy.speed/2 * Math.Cos(enemy.rotation)), (float)(enemy.speed/2 * Math.Sin(enemy.rotation)));
}
나는 내가 거기에 어떻게 가지 기능에 대한 지식이 부족 수있는 C#에 비교적 새로운 해요, 그래서 누군가는 아마도 올바른 방향으로 날 지점 수 있을까?
어디에서 rnd를 선언합니까? 메인 클래스의 – Jay
, 나의 나쁜 점은 그 덧붙인 것을 잊어 버렸다. – Renet