Generating multiple versions of the same enemy.

Started by
5 comments, last by phil_t 8 years, 6 months ago

I need help generating multiple versions of the same enemy. Here's all the necessary code I used to try and do just that, but it only ever generates one enemy on screen. The below code already has a Timer (enemyGen having an Interval = 2000, and eneMove having an Interval = 40) I'll put my thoughts on each line of code I think has a problem:


public FormDesign()
{
     eneGen = new int[3];
            EnAIaX = new int[3];
            EnAIaY = new int[3];
            for (int a = 0; a < 3; a++) //Will the random numbers all be different per array object?
            {
                EnAIaX[a] = new Random().Next(20, 500);
                EnAIaY[a] = new Random().Next(0, 0);
            }
}





 private void enemyGen_Tick(object sender, EventArgs e)
        {  
            
             
            if (eneCount < 3)//Enemy Activation: creates an enemy if there are less than 3 enemies
            {
                for (int a = 0; a <= 2; a++)
                {


                    eneGen[a] = 1;

                    if (a > 2)
                    { a = 0; }
                    eneCount++;

                }
            }
            
            
            for (int a = 0; a < 2; a++)
            {
                if (EnAIaY
                    [a] > 600)
                {
                    EnAIaX[a] = new Random().Next(20, 500);//Will the random number generated be different per array object?
                    EnAIaY[a] = new Random().Next(0, 0);
                    eneCount--;
                }

                if (a > 1)
                { a = 0; }
            }
        }

        private void eneMove_Tick(object sender, EventArgs e)
        {
            for (int a = 0; a < 2; a++)
            {
                if (eneGen[a] == 1)//creates BLUE ENEMY
                {

                    eneInA++;
                    EnAIaY[a] += 6;
                    eneCount++;
                    if (a >= 2)
                    { a = 0; }
                    if (eneInA > 2)
                    { eneInA = 0; }
                }
            }
            pbArea.Refresh();
        }

The singular enemy that comes out really does generate in random locations along the X-axis, but my end goal is to generate at least 3 of them at the same time. Please help a beginner out. :(

Advertisement

some annotations:


public FormDesign()
{
     eneGen = new int[3];
            EnAIaX = new int[3];
            EnAIaY = new int[3];
            for (int a = 0; a < 3; a++) //Will the random numbers all be different per array object?
            {
                EnAIaX[a] = new Random().Next(20, 500);
                EnAIaY[a] = new Random().Next(0, 0);
            }
}





 private void enemyGen_Tick(object sender, EventArgs e)
        {  
            
             
            if (eneCount < 3)//Enemy Activation: creates an enemy if there are less than 3 enemies
            {
                for (int a = 0; a <= 2; a++)
                {


                    eneGen[a] = 1;

                    if (a > 2) // What it this? Won't ever happen if a <= 2 as above
                    { a = 0; } //
                    eneCount++;

                }
            }
            
            
            for (int a = 0; a < 2; a++)
            {
                if (EnAIaY
                    [a] > 600)
                {
                    EnAIaX[a] = new Random().Next(20, 500);//Will the random number generated be different per array object?
                    EnAIaY[a] = new Random().Next(0, 0);
                    eneCount--;
                }

                if (a > 1) // Again, won't happen, if it would you program would hang, looping forever
                { a = 0; }
            }
        }

        private void eneMove_Tick(object sender, EventArgs e)
        {
            for (int a = 0; a < 2; a++)
            {
                if (eneGen[a] == 1)//creates BLUE ENEMY // does this create enemies? if so it creates only one
                {

                    eneInA++;
                    EnAIaY[a] += 6;
                    eneCount++;
                    if (a >= 2) // And again...
                    { a = 0; }  //
                    if (eneInA > 2)
                    { eneInA = 0; }
                }
            }
            pbArea.Refresh();
        }

That is to restart the loop. Once the for loop reaches that condition, it will start again from zero. Am I doing it wrong? The timer automatically starts as soon as the game starts so I have to keep the loop running indefinitely for it to generate an enemy every 2 seconds.

The code

"if(eneGen[a] == 1)"

along with

"eneGen[a] = 1;"

was supposed to generate enemies randomly based on a random number. For debugging purposes, I have set it to one, which is the required number for the enemy to actually spawn. Once I can spawn multiple enemies, I'll make it random again.


 if (eneCount < 3)//Enemy Activation: creates an enemy if there are less than 3 enemies
            {
                for (int a = 0; a <= 2; a++)
                {


                    eneGen[a] = 1;

                    if (a > 2) // What it this? Won't ever happen if a <= 2 as above
                    { a = 0; } //
                    eneCount++;

                }
            }

looking at this, when the loop runs, a will be 0,1,2 and then the loop exits. a will never be > 2 because in the loop-condition you said it must not be. so if(a>2){a=0;} simply does nothing because it never happens.
it is wrong, but probably not the reason your enemies don't spawn.

also you sometimes use a <= 2 , a < 3 and a < 2 in your for-loops, i don't know but it looks as if you always want to work on 3 items, not sometimes 2 and sometimes 3. in that case i would use a < 3, because it is best to read - when you look at it you know it will run 3 times.

it is not really clear to me what the code is supposed to be doing, seeing how most parts seem to not be doing anything at all. i'd advise you to use a lot of Console.Write()s to output your variables to console and see what your code actually does or doesn't do.

I've already declared

eneGen = new int[3];

I already tried that and yeah it still works. Actually, the game already runs. My problem is just that it spawns only one enemy. I declared an array of 3 enemies specificically because I want to spawn up to 3 of them at a rate of one enemy per second.


if (eneCount < 3)//Enemy Activation: creates an enemy if there are less than 3 enemies
            {
                for (int a = 0; a <= 2; a++)
                {


                    eneGen[a] = 1;

                    if (a > 2) // What it this? Won't ever happen if a <= 2 as above
                    { a = 0; } //
                    eneCount++;

                }
            }

That particular code means that I permanently set eneGen[a] to be 1. In the below code:


private void eneMove_Tick(object sender, EventArgs e)
        {
            for (int a = 0; a < 2; a++)
            {
                if (eneGen[a] == 1)//creates BLUE ENEMY // does this create enemies? if so it creates only one
                {

                    eneInA++;
                    EnAIaY[a] += 6;
                    eneCount++;
                    if (a >= 2) // And again...
                    { a = 0; }  //
                    if (eneInA > 2)
                    { eneInA = 0; }
                }
            }
            pbArea.Refresh();
        }

It says that if eneGen[a] ==1 (which I have already set to always have a value of 1), it will start the process of generating and making the enemy move downwards.

What I don't understand is that if I have a for loop to determine the existence of an enemy per eneGen[a] (which is up to 3), why is there only one enemy spawning? There should be 3 enemies spawning since I specifically randomized their spawn points.

Are you familiar with using a debugger? If you set a breakpoint in eneMove_Tick and step through (looking at variable values as you go), it should rapidly become clear what is going wrong. If you are good with a debugger, this should be a lot easier than trying to figure out what is going wrong by inspection. If you are not familiar with a debugger, this is a perfect time to start! This is a skill that you will need and use often.

I'm not great at understanding code through inspection. If I was to try to understand your code better, I would probably walk through it in a debugger myself.

Good luck!

Geoff


new Random().Next(20, 500);

This is not how you use Random in .net. Create one Randon instance and keep calling Next on that single one. Creating a new ones in close time proximity to each other might generate one with the same seed, and you'll get identical sequences of numbers.

from MSDN:

"Both to improve performance and to avoid inadvertently creating separate random number generators that generate identical numeric sequences, we recommend that you create oneRandom object to generate many random numbers over time, instead of creating newRandom objects to generate one random number."

So have a Random instance as a class member that is initialized just once:


private Random random = new Random();

and then use it:


random.Next(20, 500);

This topic is closed to new replies.

Advertisement