1

I wanna a pattern who move my enemy randomly at all because when i have 3 or 4 enemys they seem like the same enemy movement. this is my code for the random :

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

And here the code for process the move:

   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);
    }

but this code just do the same pattern for all enemys on the room. thx for help me with it .

2 Answers2

0

I belive the problem lays on this

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

Check if you are calling it multiple times too close in time. If thats so you'll get the same random number on

rand.Next(0, 3)

Every time you call it, you'll get the same number, therefore you'll get the same pattern on every enemy.

Similar question

Community
  • 1
  • 1
0

Okay i solve this one putting a counter on the for reader for do the seed like this (this is on the for map reader) :

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));

            }

And after that for the constructor of the enemy something like this :

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