我正在开发一个小型XNA游戏,
for (int birdCount = 0; birdCount < 20; birdCount++)
{
Bird bird = new Bird();
bird.AddSpriteSheet(bird.CurrentState, birdSheet);
BIRDS.Add(bird);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在Load函数中运行,BIRDS是所有Bird的代码.
鸟类构造函数随机定制鸟.如果我通过breakPoint运行代码breakPoint,则随机函数会生成不同的值,但如果我不停止代码并让程序运行,则所有随机值都会变为相同,以便所有的鸟都变得相同.
我怎么解决这个问题 ?
随机和种子的代码:
private void randomize()
{
Random seedRandom = new Random();
Random random = new Random(seedRandom.Next(100));
Random random2 = new Random(seedRandom.Next(150));
this.CurrentFrame = random.Next(0, this.textures[CurrentState].TotalFrameNumber - 1);
float scaleFactor = (float)random2.Next(50, 150) / 100;
this.Scale = new Vector2(scaleFactor, scaleFactor);
// more codes ...
this.Speed = new Vector2(2f * Scale.X, 0);
this.Acceleration = Vector2.Zero;
}
Run Code Online (Sandbox Code Playgroud) 我正在写一个俄罗斯方块克隆,这是我真正做过的最大的项目.在实现行删除代码时,我已经开始收到堆栈溢出异常.我认为这可能与我的大量收藏或Linq的低效率使用有关.以下是导致此问题的具体方法:
void MoveAllAboveDown(int row)
{
List<Block> newBlockList = new List<Block>();
for (int rowCheck = row; rowCheck > _VertOffset; --rowCheck)
{
for (int i = _HorizOffset; i < _HorizOffset + _Width; ++i)
{
//If the spot above this is filled
if (_blockList.Where(block => block.Contains(new Vector2(i, rowCheck - 1))).ToList().Count > 0)
{
//insert block here
newBlockList.Add(new Block(new Vector2(i, rowCheck), new[,] { { true } }, Color.Black, _texture));
}
else
{
var list = _blockList.Where(tempBlock => tempBlock.Contains(new Vector2(i, rowCheck - 1))).ToList();
if (list.Count …Run Code Online (Sandbox Code Playgroud) Windows Phone 7.5应用程序.
我想重构我的应用程序,就像在Visual Studio中创建单独的嵌套文件夹以便更好地管理.例如

这种文件结构会影响应用程序的性能吗?
我有这个代码:
public class Area
{
Texture2D point;
Rectangle rect;
SpriteBatch _sB;
GameTimer _gt;
int xo, yo, xt, yt;
//List<Card> _cards;
public Area(Texture2D point, SpriteBatch sB)
{
this.point = point;
this._sB = sB;
xt = 660;
yt = 180;
xo = 260;
yo = 90;
}
public void Draw(SpriteBatch spriteBatch)
{
rect = new Rectangle(660, 180, 80, 120);
spriteBatch.Draw(point, rect, Color.White);
_gt = new GameTimer();
_gt.UpdateInterval = TimeSpan.FromSeconds(0.1);
_gt.Draw += OnDraw;
}
private void OnDraw(object sender, GameTimerEventArgs e)
{
this.pass(xo, yo); …Run Code Online (Sandbox Code Playgroud) 我有一个方法可以将瓷砖绘制到屏幕上,并且在此方法内部调用一个名为drawFloor的类中的方法,该方法绘制特定的图块.
在外部方法中,我在drawFloor调用的正上方启动了一个秒表,并在调用的正下方停止了,我得到的时间是2.4毫秒.
然后我把秒表放在实际的drawFloor方法本身里面,使得秒表体现了方法中的所有代码,我得到的时间是1.9毫秒.
我认为唯一能及时改变的是参数的传递,我不明白为什么它如此昂贵.它只传递两个参数,一个是名为"camera"的自定义类,另一个是精灵批处理.
继续我在这里的游戏(一个游戏,玩家是一艘船,你击落流星).我目前正在研究射击子弹的船只,并且我试图在被子弹击中时移除流星.所以这就是我所做的.我几乎在那里,但我在编码中发现了一个错误.在我的游戏中,它产生了地图右侧以外的流星,它们向左移动,然后点击它们,如果你以正确的顺序拍摄流星而不是其他方式,这是有效的.让我用图片解释一下.

如果我击落第二颗流星,标有数字1的流星将首先被摧毁
//spawns the enemies.
public void LoadEnemies()
{
int randY = random.Next(100, 500);
if (spawn > 1)
{
spawn = 0;
if (enemies.Count() < 4)
enemies.Add(new Enemies(Content.Load<Texture2D>("meteor"), new Vector2(1110, randY)));
}
//Here's where the error lies because of the bulletcolliding (I think)
for (int i = 0; i < enemies.Count; i++)
{
if (!enemies[i].isVisible || bulletColliding)
{
bulletColliding = false;
enemies.RemoveAt(i);
i--;
}
}
}
Run Code Online (Sandbox Code Playgroud)
碰撞方法.
public void bulletCollision(GameTime gameTime)
{
foreach (var x in bullets)
{
foreach (var …Run Code Online (Sandbox Code Playgroud) 我正在编写一个应用程序,我希望实现一个对象数组,它会定期添加一个新项目.
我总是想知道添加的最后一个对象是什么,并且正在考虑将该对象放在位置0(因此将每个项目按下一个索引到最多130个项目)
使用List使用它很容易实现
items.Insert(0,new item());
items.RemoveAt(130);
Run Code Online (Sandbox Code Playgroud)
然后将自动按下每个项目并在130处移除该项目,但这对于数组来说并不是那么简单
我最初想到的地方
for(int i = 129; i>0;i--)
{
items[i] = items[i-1];
}
items[0] = new item();
Run Code Online (Sandbox Code Playgroud)
然后,这允许我简单地访问最新项目(通过索引[0]),并按创建顺序访问每个前面的项目(1 - > 129);
现在本身就相当简单,我想知道是否还有其他方法可以执行此操作.
编辑:感谢您的快速回复,
我已经对此进行了一些测试(使用了100万次迭代),看起来队列方法在这里是最快的,但只是稍微一点,然后列表然后数组花费了大约50%的时间来处理100万个项目
我想我将探索队列堆栈选项
再次感谢;
有人可以告诉我我在哪里错了吗?我一直收到错误信息:
"ArgumentNullException未处理.此方法不接受此参数的null.参数名称:song"
我无法找到解决办法.
Song BGmusic;
bool songstart = false;
protected override void LoadContent()
{
currentgamescreen = Gamescreen.menuscreen;
if (!songstart)
{
MediaPlayer.Play(BGmusic);
}
BGmusic = Game.Content.Load<Song>("audio/rockTheDragon");
}
Run Code Online (Sandbox Code Playgroud) 我试图这样做,以便当我的角色跳跃时,跳跃键被禁用,直到他的jumpHeight == 0(他又回到了地板上).C#中有一个简单的功能可以做到这一点吗?例如:
if (jumpHeight > 0)
{
keys.space == disabled
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我知道这个问题对那些熟悉平铺和单一游戏的人来说非常具体,所以我会尽量做到尽可能彻底.我已经在这个问题上工作了15个多小时,我很绝望.
我正在尝试使用Tiled的对象层将精灵绘制到屏幕上.我已经创建了一个新的对象图层,为它绘制了一些矩形,并添加了一个我在LoadContent方法中引用的自定义属性.
这是我的LoadContent方法:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
mymap = Content.Load<TiledMap>("Misc/newmap");
enemyShip = Content.Load<Texture2D>("Enemies/enemyship");
//TiledMapObject[] islands = mymap.GetLayer<TiledMapObjectLayer>("Islands").Objects;
ship_sprite = Content.Load<Texture2D>("character/wooden_ship");
Texture2D texture = Content.Load<Texture2D>("character/anima2");
animatedSprite = new AnimatedSprite(texture, 1, 2);
font = Content.Load<SpriteFont>("Misc/londrinasolid");
Song song = Content.Load<Song>("Music/Chad_Crouch_-_Stellars_Jay");
MediaPlayer.Volume = 0.7F;
MediaPlayer.Play(song);
TiledMapObject[] littleBoats = mymap.GetLayer<TiledMapObjectLayer>("SmallBoats").Objects;
foreach (var en in littleBoats)
{
string type;
en.Properties.TryGetValue("type", out type);
if (type == "smallboat")
{
Enemy.enemies.Add(new SmallBoat(en.Position)); …Run Code Online (Sandbox Code Playgroud)