그래서 저는 XNA에서 2D 슈팅 게임을 만들고 있는데 약간의 걸림돌이 생겼습니다. 나는 10 초마다 화면 밖에서 적을 스폰 한 다음, 위, 오른쪽, 왼쪽 또는 아래로 무작위로 움직일 수있는 적 클래스를 만들려고합니다. 인터넷을 수색하는 4 시간의 더 나은 부분을 보낸 후 내가 찾은 것은 적을 한 방향으로 움직이고 한 지점에서 스폰하는 등의 예입니다.
어떤 도움을 주시면 감사하겠습니다. 적군은 너무 많은 코드를 끌어 와서 작동시키기 위해 약간 엉망입니다. 여기에 그대로 있습니다.
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Shark_Wars_2
{
class Enemy
{
List<Vector2> sharkPosition = new List<Vector2>(); //is there a better way for having more than one of the same enemy?
public Vector2 sharkDirection;
public int sharkWidth;
public int sharkHeight;
public int sharkSpeed;
float sharkSpawnProbability;
public Random randomSharkPosition;
public Random direction;
double timer = 0;
float interval = 1000;
int speed = 1;
public Enemy()
{
sharkPosition = new Vector2(0, 0); //brings up error - see above
sharkWidth = 64;
sharkHeight = 44;
sharkSpeed = 3;
sharkSpawnProbability = 1 / 10000f;
randomSharkPosition = new Random(3); //left, right, up, down
direction = new Random(3);
}
public void update(GameTime gameTime)
{
//enemy movement
for (int i = 0; i < sharkPosition.Count; i++)
{
//enemy position increases by speed
sharkPosition[i] = new Vector2(sharkPosition[i].X + sharkDirection.X, sharkPosition[i].Y + sharkDirection.Y);
}
timer += gameTime.ElapsedGameTime.TotalMilliseconds;
//aniamtion
if (timer >= interval / speed)
{
timer = 0;
}
//spawn new enemy
if (randomSharkPosition.NextDouble() < sharkSpawnProbability)
{
float spawn = (float)randomSharkPosition.NextDouble() * (1280 - sharkHeight); sharkPosition.Add(new Vector2(sharkDirection.X, sharkDirection.Y));
}
}
// ill worry about drawing after spawning and movement, should be no problem
public void draw()//(SpriteBatch spritebatch, Texture2D wave)
{
for (int i = 0; i < sharkPosition.Count; i++)
{
//spritebatch.Draw(shark, new Rectangle((int)sharkPosition[i].X, (int)sharkPosition[i].Y, sharkWidth, sharkHeight);
}
}
}
}
머리가 정말 막혀서 움직이는 배경 물체와 총알 목록을 만드는 방법을 연구하는 데 많은 시간을 보냈습니다.
나는 이것을 너무 단순화하고 싶습니다. 당신이 일을하는 더 쉬운 방법을 알고 있다면 나는 모두 귀입니다!
미리 감사드립니다!
기본적인 문제는 관리 / 로직 클래스를 인스턴스 객체에서 분리하지 않았다는 것입니다. 다음과 같이 더 많은 분리를 원합니다.
public class EnemyManager
{
List<Shark> activeSharks = new List<Shark>();
Timer spawnTimer;
//Just one random instance per class, this is considered best practice to improve
//the randomness of the generated numbers
Random rng = new Random(); //Don't use a specific seed, that reduces randomness!
public EnemyManager()
{
//Min/max time for the next enemy to appear, in milliseconds
spawnTimer = new Timer(rng.Next(500, 2000));
spawnTimer.Elapsed += SpawnShark;
spawnTimer.Start();
}
public void SpawnShark(object sender, ElapasedEventArgs e)
{
Vector2 newSharkPosition = Vector2.Zero;
newSharkPosition.X = rng.Next(0, 1280); //Whatever parameters you want
newSharkPosition.Y = rng.Next(0, 1280); //Whatever parameters you want
Vector2 newSharkDirection.... //Repeat above for direction
Shark spawnedShark = new Shark(newSharkPosition, newSharkDirection);
activeSharks.Add(spawnedShark);
//Min/max time for the next enemy to appear, in milliseconds
spawnTimer = new Timer(rng.Next(500, 2000));
}
public void update(GameTime gameTime)
{
foreach(Shark s in activeSharks)
s.Update(gameTime);
}
public void draw(SpriteBatch spritebatch, Texture2D wave)
{
foreach(Shark s in activeSharks)
s.Draw(spritebatch, wave)
}
}
public class Shark
{
Vector2 position;
Vector2 direction;
const float speed = 3;
const int height = 44;
const int width = 64;
public Shark(Vector3 initPosition, Vector3 direction)
{
position = initPosition;
this.direction = direction;
}
//Draw/Update using the local variables.
}
분명히 모든 코드를 작성하지는 않았지만 시작해야합니다. 먼저 모든 "인스턴스"특정 데이터를 가져 와서 자체 개체에 넣은 다음 "관리자"가 이러한 개체의 컬렉션을 보유하고 관리합니다. 또한 RNG를 리팩터링하여 인스턴스가 하나만 있고 (모범 사례) 기본 시간 기반 시드를 사용했습니다. 직접 시드하면 프로그램을 실행할 때마다 숫자가 똑같이 나옵니다 (아마 당신이하려는 것이 아닙니다 :)).
이해가되지 않는 부분이 있으면 알려주세요. 추가로 도움을 드릴 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다