This site requires JavaScript, please enable it in your browser!
Greenfoot back
loxtn10
loxtn10 wrote ...

2016/8/26

Add multiple same objects(fail)

loxtn10 loxtn10

2016/8/26

#
What I want the game to do is that when a miniboss is killed multiple coins should appear. but this just doesn't happen, help me please
 else if (isTouching(enemy2.class))
        {
            shotenemy2 ++; //adds damage while shotenemy2 doesn't equal to 8;
            if (shotenemy2b) //shotenemy2b means shotenemy2 == 8
            {
                removeTouching(enemy2.class);   //death of enemy when shotenemy2b turns true
                coin x = new coin();
                getWorld().addObject(x, getX(),getY());  //this is where the coins should be added.
                getWorld().addObject(x, getX()+7,getY());    //but only one coin is produced.
                getWorld().addObject(x, getX()+3,getY());   //this is the code when an enemy disappears or dies.
                getWorld().addObject(x, getX()+5,getY());                
                getWorld().addObject(x, getX()+9,getY());
                
                shotenemy2 = 0;
            }
            
            
                Scene a = (Scene)getWorld();
                getWorld().removeObject(this);
            
        }
HELP please. thanks
danpost danpost

2016/8/26

#
loxtn10 wrote...
What I want the game to do is that when a miniboss is killed multiple coins should appear. but this just doesn't happen
You are only creating one coin (see line 7). Each time the expression 'new coin()' is executed, exactly one coin is created (provided it is created successfully). Only by executing 'new coin()' again (whether with the same line via a loop or by separate lines) will multiple coins be created. Remove line 7 and replace all 'x' (lines 8 through 12) with 'new coin()'.
loxtn10 loxtn10

2016/8/26

#
 if (shotenemyy2b)
            {
                removeTouching(enemy2.class);
                for(int i = 0 ; i < 5 ; i++){
                getWorld().addObject(new coin(), getX()+i+5,getY());
                }  
                enemy2.shotenemyy2 = 0;
            }
So far, Thanks a lot !!!! you're a big help, for you to know THANKS ! :D so what happened to the previous code is that it made only one coin giving new coin(), which means the x holds the new coin() , and not that processes to produce new coin().. please clarify if I was able to get the point. so far no problem, did what I wanted thanks !!!! :D
danpost danpost

2016/8/26

#
loxtn10 wrote...
so what happened to the previous code is that it made only one coin giving new coin(), which means the x holds the new coin() , and not that processes to produce new coin().. please clarify if I was able to get the point. so far no problem, did what I wanted thanks !!!! :D
To clarify, you created one coin and kept a reference to that coin in the 'x' variable. Since you never changed to assigned value of the 'x' variable, it referenced the same coin throughout. Meaning you were trying to add the same coin into the world multiple times.
loxtn10 loxtn10

2016/8/26

#
Meaning you were trying to add the same coin into the world multiple times.
ok I got it now this is what happens the code is changed to
for(int i = 0 ; i < 5 ; i++){
               getWorld().addObject(new coin(), getX()+i+30,getY());
               } 
the solo coin is dropped by other kind of enemy. the other coin is dropped by the mini boss, it is added multiple times that happens when the code is executed, I want the coins to have at least a space from each other, but the space is so small.
Super_Hippo Super_Hippo

2016/8/26

#
You can multiply 'i' by something to have a bigger gap.
loxtn10 loxtn10

2016/8/26

#
thanks
Super_Hippo wrote...
You can multiply 'i' by something to have a bigger gap.
got it . problem solved, thanks
You need to login to post a reply.