【发布时间】:2014-03-28 19:45:05
【问题描述】:
好吧,我正在 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);
}
}
}
}
我的脑袋真的被堵住了,我花了很多时间研究如何制作移动的背景对象和一个子弹列表。
我真的很想把它简化一下,所以如果你知道一种更简单的做事方式,我会全力以赴!
提前致谢!
【问题讨论】:
-
你的 SharkPosition = new Vector2(0, 0);应该显示错误。这是一个列表,所以你需要这样做。 SharkPosition.Add(new Vector2(0, 0));至于你的产卵。选择一个随机的 x 和 y 值。检查它们是否超出范围。放置单元。检查单位是否小于0。如果是则向右移动。否则检查它是否小于0。向下移动。否则检查它是否大于地图边界宽度。向左移动。否则检查它是否大于地图边界高度。提升。检查会导致单位从界外变为界内。
标签: c# random xna game-physics