Creating objects bug??

I wanna spawn an enemy, if I do it like this everything works fine:


                        if(a>=10 && a<=80) golom = new Enemy(1);
                       else if(a>80 && a<=100) golom = new Enemy(2);
                       else if(a>=0 && a<10) golom = new Enemy(3);                        
                        enemiesList.add(golom);

If I do it like this it dont show the enemy on the map:


                       if(a>=10 && a<=80) race=0;
                       else if(a>80 && a<=100) race=1;
                       else if(a>=0 && a<10) race=2;                         
                         golom = new Enemy(race);
                        enemiesList.add(golom);

This is how I draw the enemy:


for(int i=0;i<enemiesList.size;i++){    

            switch (enemiesList.get(i).id) {
                case 1:
                    batch.draw(golomChar,enemiesList.get(i).x, enemiesList.get(i).y);
                    break;
                case 2:
                    batch.draw(golomChar2,enemiesList.get(i).x, enemiesList.get(i).y);
                    break;
                case 3:
                    batch.draw(golomChar3,enemiesList.get(i).x, enemiesList.get(i).y);
                    break;
            }
        }

The problem is that this code:


if(a>=10 && a<=80) golom = new Enemy(1);
else if(a>80 && a<=100) golom = new Enemy(2);
else if(a>=0 && a<10) golom = new Enemy(3);
golom = new Enemy(race); // <-- You instantiate Enemy AGAIN here and overwrite golem with it!!!
enemiesList.add(golom);

is NOT functionally equivalent to this code:


if(a>=10 && a<=80) race=0;
else if(a>80 && a<=100) race=1;
else if(a>=0 && a<10) race=2;                         
golom = new Enemy(race);
enemiesList.add(golom);

The first code is more like this:


int whateverRaceWasBefore = race;
if(a>=10 && a<=80) race=0;
else if(a>80 && a<=100) race=1;
else if(a>=0 && a<10) race=2;                         
golom = new Enemy(whateverRaceWasBefore);
enemiesList.add(golom);

first thing first :
Try to always use this “{}” << :point:
after if statement or whatever keywords java that needs to use that
its not really that important but it lets you see the code what is on that or not

second :
if(a>=10 && a<=80) race=0;
else if(a>80 && a<=100) race=1;
else if(a>=0 && a<10) race=2;
golom = new Enemy(race);
enemiesList.add(golom);

this piece of code u should declare it like this
int race = 0; //this zero means is initial u might be need or might not depends on the error
if(a>=10 && a<=80) race=0;
else if(a>80 && a<=100) race=1;
else if(a>=0 && a<10) race=2;
golom = new Enemy(race);
enemiesList.add(golom);

                   //quick note you might be have declared on class (not on method) in this case
                   if(a>=10 && a<=80) this.race=0;
                   else if(a>80 && a<=100) this.race=1;
                   else if(a>=0 && a<10) this.race=2;                         
                     golom = new Enemy(this.race);
                    enemiesList.add(golom);

third :

int whateverRaceWasBefore = race;
if(a>=10 && a<=80) race=0;
else if(a>80 && a<=100) race=1;
else if(a>=0 && a<10) race=2;
golom = new Enemy(whateverRaceWasBefore);
enemiesList.add(golom);

u cant do it like that u need to do it like this

int whateverRaceWasBefore = 0;
if(a>=10 && a<=80) whateverRaceWasBefore =0;
else if(a>80 && a<=100) whateverRaceWasBefore =1;
else if(a>=0 && a<10) whateverRaceWasBefore =2;
golom = new Enemy(whateverRaceWasBefore);
enemiesList.add(golom);

i do know what your mean is
if i do something like this “int whateverRaceWasBefore = race;” it doesnt mean race =/= whateverRaceWasBefore

yeah ‘=’ here doesnt mean equal it means adding something to variable remember that

fourth : ah this is too long !

if(a>=10 && a<=80) golom = new Enemy(1);
else if(a>80 && a<=100) golom = new Enemy(2);
else if(a>=0 && a<10) golom = new Enemy(3);
golom = new Enemy(race); // <-- You instantiate Enemy AGAIN here and overwrite golem with it!!!
enemiesList.add(golom);

you dont need this code “golom = new Enemy(race);”

last : pick your poison that can work in your code 8)

Sorry I made a error in the code above, i do not add twice enemy object lol
But yeah if I do it like this it is still not working


 int race = 0; //this zero means is initial u might be need or might not depends on the error
                       if(a>=10 && a<=80) race=0;
                       else if(a>80 && a<=100) race=1;
                       else if(a>=0 && a<10) race=2;                         
                         golom = new Enemy(race);
                        enemiesList.add(golom);

I guess I just use the other method, but I was just wondering why its not working the other way.


                        if(a>=10 && a<=80) golom = new Enemy(1);
                       else if(a>80 && a<=100) golom = new Enemy(2);
                       else if(a>=0 && a<10) golom = new Enemy(3);                        
                        enemiesList.add(golom);

You’re using these values: 1,2,3


                       if(a>=10 && a<=80) race=0;
                       else if(a>80 && a<=100) race=1;
                       else if(a>=0 && a<10) race=2;                         
                         golom = new Enemy(race);
                        enemiesList.add(golom);

You’re using these values: 0,1,2