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

2014/1/21

Enemy isn't spawning

GreenAnthony GreenAnthony

2014/1/21

#
Hi the boss enemy in my game is not spawning for some reason...
        setLocation(getX(),getY()+7);  
       
 if (changeSize == 1)  
        {  
            GreenfootImage image = getImage();  
            image.scale(image.getWidth() - 35, image.getHeight() - 35);  
            setImage(image);  
            changeSize = 0;  
        }        
        if (atWorldEdge())  
        {  
            getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) );  
            getWorld().removeObject(this);
            return;
        }  
        if (canSee(EnemyRocket.class))  
        {  
            getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) );  
            getWorld().removeObject(this);  
            return; 
        }   
        if (BossSpawn==50)
        {
            removeObject(EnemyRocket.class);
            getWorld().addObject(new BossEnemy(), 400, 50);
        }
        counter++;  
        BossSpawn++;
    }
can you guys see why it isn't? If you guys can't find a mistake then it could also be in the boss enemies' class so just tell me and I will post its codings...!
davmac davmac

2014/1/21

#
This looks like a partial bit of code. What method is this? Could you post the whole method? What is line 3 - "ngeSize == 1) " - supposed to be?
danpost danpost

2014/1/21

#
If I am correct in presuming that the code given is in the EnemyRocket class, then the problem stems from creating new ones and removing the original one(s). When you create a new object, newly initialized non-static fields are assigned to them to use for that object, and that object only (the value of a non-static field in one object of that class can hold a different value from another of that class). Either make the BossSpawn field a 'static' field or move it to the world class. If making it 'static', make sure you set its value to zero in the world constructor (its value will be retained between resets, only initializing when compiled).
danpost danpost

2014/1/21

#
davmac wrote...
This looks like a partial bit of code. What method is this? Could you post the whole method? What is line 3 - "ngeSize == 1) " - supposed to be?
I presumed it was in the act method of the EnemyRocket class and line 3 was a cut-off 'if (changeSize == 1)'.
You need to login to post a reply.