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

2019/4/19

Issues with respawning

ErasedBlade ErasedBlade

2019/4/19

#
Hi my issue is mostly sceen when ever you crear a level by destroying an enemy they are supposed to respawn once on certain lvels or not at all. howver if you play taht level again, they respawn an extra time. sometimes this ccures on the first time, but its not as often. does anyone have a solution to this? my Respawn code for my world level 3 is as fallow. in this level the enemy is not supposed to respawn, however does when played again. then it functions normally. the respawn method is called form this worlds act method. s
    /**
     * Respawn Enemy at a random location after 250 cycles only if there is no enemys
     */
    private void respawn()
    {
        if (getObjects(Ufo.class).size() !=1)
        {
            changeValue();
            hpt++; 
            if(hpt ==250)
            {
                addObject(new Ufo (95,2), 563,Greenfoot.getRandomNumber(325)+75);
                hpt =0;
                hpE = 4;
            }
        }
    }
danpost danpost

2019/4/19

#
If you do not want to spawn more than one extra enemy, make sure the condition on line 10 becomes true only once. You can remove line 13 to prevent that condition from becoming true a second time.
ErasedBlade ErasedBlade

2019/4/19

#
correct however the changevalues method is supposed to fix that.
    /**
     * changes Enemy hp and damage if the enemy hp reaches 0, then respawn
     */
    public void changeValue()
    {
        if (hpE <=0 && hpt ==0)
        {
            //enemy's remaining for level 3
            er3 = er3-1;
        }
    }
there is also another method that returns you to the title screen if the variable er3 is 0.
danpost danpost

2019/4/19

#
ErasedBlade wrote...
correct however the changevalues method is supposed to fix that. << Code Omitted >> there is also another method that returns you to the title screen if the variable er3 is 0.
If er3 is tracking enemies remaining, then it should be decreased at the time you create an enemy. I do not understand why it has all those conditions applied to it or why changeValue is called before you confirm that an enemy should be (re)spawned.
N0way N0way

2019/4/20

#
on line 6th should be or instead of and(&&) is it? that may be a problem
You need to login to post a reply.