2015-08-13 68 views
1

我想要一個隨機移動我的敵人的模式,因爲當我有3或4個敵人時,他們看起來像是同一個敵人的移動。 這是我的隨機碼:隨機敵人模式

  int seed = unchecked(System.DateTime.Now.Ticks.GetHashCode()); 
     rand = new Random(seed); 

這裏過程的代碼,此舉:

public void move(GameTime time, WorldScreen screen) 
    { 
     timer += (float)time.ElapsedGameTime.TotalMilliseconds; 
     if (timer > 2000) 
     { 
      switch (rand.Next(0, 3)) 
      { 
       case 0: 
        base.direction = Enemy.Direction.down; 
        break; 
       case 1: 
        base.direction = Enemy.Direction.right; 
        break; 
       case 2: 
        base.direction = Enemy.Direction.left; 
        break; 
       case 3: 
        base.direction = Enemy.Direction.up; 
        break; 

      } 
      timer = 0; 
     } 
     timer1 += (float)time.ElapsedGameTime.TotalMilliseconds; 
     if (timer1 > 50) 
     { 
      timer1 = 0; 
      if (direction == Enemy.Direction.down) 
      { 
       if (!MoveCollision(new Vector2(position.X, position.Y + speed), screen)) 
       { 
        base.position.Y += speed; 
        ActualAnimation = getAnimation("rundown"); 
       } 
       else 
       { 
        base.direction = Enemy.Direction.right; 
       } 
      } 
      if (direction == Enemy.Direction.up) 
      { 
       if (!MoveCollision(new Vector2(position.X, position.Y - speed), screen)) 
       { 
        base.position.Y -= speed; 
        ActualAnimation = getAnimation("runup"); 
       } 
       else 
       { 
        base.direction = Enemy.Direction.down; 
       } 
      } 
      if (direction == Enemy.Direction.right) 
      { 
       if (!MoveCollision(new Vector2(position.X + speed, position.Y), screen)) 
       { 
        base.position.X += speed; 
        ActualAnimation = getAnimation("runright"); 
       } 
       else 
       { 
        base.direction = Enemy.Direction.left; 
       } 
      } 
      if (direction == Enemy.Direction.left) 
      { 
       if (!MoveCollision(new Vector2(position.X - speed, position.Y), screen)) 
       { 
        base.position.X -= speed; 
        ActualAnimation = getAnimation("runleft"); 
       } 
       else 
       { 
        base.direction = Enemy.Direction.up; 
       } 

      } 
     } 
     ActualAnimation.update(time, position); 
    } 

但是這個代碼只是做了在房間內的所有敵人相同的模式。 thx幫助我。

+1

'rand = new Random(seed)'line? – Mephy

+0

因爲第二個參數是**獨佔**上限,所以將'rand.Next(0,3)'更改爲'rand.Next(0,4)'可能會很有用。換句話說,'rand.Next(0,3)'只返回來自'{0,1,2}'的值。 – Enigmativity

+0

你可以嘗試實例化一個'新Random()'(沒有不必要的種子代碼)作爲'static'字段嗎? – Enigmativity

回答

0

我相信在這個

int seed = unchecked(System.DateTime.Now.Ticks.GetHashCode()); rand = new Random(seed);` 

檢查問題的規定,如果你多次調用它太接近的時間。如果那這樣你會得到

rand.Next(0, 3) 

每次調用時相同的隨機數,你會得到相同的號碼,因此,你會得到每一個敵人相同的模式。

Similar question

0

好吧,我解決了這一個把一個櫃檯上的閱讀器做這樣的種子(這是對在地圖讀者):

int id = map.Layers[1].Tiles[i].Gid; 
      if (id == 172) 
      { 
       enemys.Add(new Hero.World.Entitys.Enemys.Octorock(new Vector2((int)position.X + map.Layers[1].Tiles[i].X * 16, (int)position.Y + map.Layers[1].Tiles[i].Y * 16),i)); 

      } 

而在此之後的構造的敵人是這樣的:

public Octorock(Vector2 position,int i) :base(position) 
    { 
     int seed = unchecked(System.DateTime.Now.Millisecond); 
     rand = new Random(seed*i);}