Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Creating objects bug??  (Read 239 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Devvie


Projects: 1



« Posted 2018-12-02 12:37:40 »

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

1  
2  
3  
4  
                        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:

1  
2  
3  
4  
5  
                       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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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;
            }
        }


Offline KaiHH

JGO Kernel


Medals: 650



« Reply #1 - Posted 2018-12-02 12:41:15 »

The problem is that this code:
1  
2  
3  
4  
5  
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:
1  
2  
3  
4  
5  
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:
1  
2  
3  
4  
5  
6  
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);
Offline hadezbladez
« Reply #2 - Posted 2018-12-02 14:31:51 »

first thing first :
Try to always use this "{}" <<  Pointing
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  Cool
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GustavXIII

Junior Devvie


Projects: 1



« Reply #3 - Posted 2018-12-02 15:04:41 »

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

1  
2  
3  
4  
5  
6  
 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.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1357
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2018-12-02 17:32:04 »

1  
2  
3  
4  
                        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



1  
2  
3  
4  
5  
                       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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (288 views)
2018-11-16 13:46:03

hadezbladez (157 views)
2018-11-16 13:41:33

hadezbladez (289 views)
2018-11-16 13:35:35

hadezbladez (70 views)
2018-11-16 13:32:03

EgonOlsen (2133 views)
2018-06-10 19:43:48

EgonOlsen (2151 views)
2018-06-10 19:43:44

EgonOlsen (1363 views)
2018-06-10 19:43:20

DesertCoockie (1966 views)
2018-05-13 18:23:11

nelsongames (1599 views)
2018-04-24 18:15:36

nelsongames (2247 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!