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

2016/5/23

Get the nuber of Objects of one Class

1
2
BloodMate BloodMate

2016/5/23

#
I'm trying to stop the Level when there are no Zombies left in the world. That means I need the number of the object Zombie, but I don't know how to do that. For now I managed it like this:
if(level == 1){
            if(level_1_complete == false){
                if(getWorld().numberOfObjects() == 2){
                    level_1_complete = true;                    
                }                
            }
        }

        if(level == 2){
            if(start_level_2 == true){
                if(level_2_complete == false){
                    if(getWorld().numberOfObjects() == 2){
                        level_2_complete = true;                    
                    }
                }   
            }
        }
This code ends the Level as soon as there are only two Objects (The Player and the class which manages the Levels) left in the World, which means that all Zombies must be dead. This Way has worked for now, but I am planig to add some objects during th game, so it won't work anymore. Any Ideas how that could work? (Like already Mentioned, the code for the Level management is in a subclass of Actor (Mostly, because i'm not so familiar with World codes), so it would be great, if any new code could replace this one in the same class)
danpost danpost

2016/5/23

#
The 'getObjects' method can be used to acquire a list of all a specific type of Actor. Its size (or its state of being empty) can be ascertained by List class methods ('size' and 'isEmpty'). It is not too difficult to grasp the idea here. You refer to the World class API on the 'getObjects' method to see its description (what parameters are required and what type of object it returns); then, you refer to the List class API to see about its methods and how they are used.
BloodMate BloodMate

2016/5/23

#
Thanks, it works fine
BloodMate BloodMate

2016/5/23

#
I just ran in another issue, where I tried to shoten the code from
if(level == 1){  
            if(level_1_progress < 5){
                if(difficulty == 1){
                    getWorld().addObject(new Zombie1(), 100, 300);
                    getWorld().addObject(new Zombie1(), 100, 100);
                    getWorld().addObject(new Zombie1(), 425, 100);
                    getWorld().addObject(new Zombie1(), 750, 100);
                    getWorld().addObject(new Zombie1(), 750, 300);
                    level_1_progress = level_1_progress+5;
                }
                if(difficulty == 2){
                    getWorld().addObject(new Zombie2(), 100, 300);
                    getWorld().addObject(new Zombie2(), 100, 100);
                    getWorld().addObject(new Zombie2(), 425, 100);
                    getWorld().addObject(new Zombie2(), 750, 100);
                    getWorld().addObject(new Zombie2(), 750, 300);
                    level_1_progress = level_1_progress+5;
                }
                if(difficulty == 3){
                    getWorld().addObject(new Zombie3(), 100, 300);
                    getWorld().addObject(new Zombie3(), 100, 100);
                    getWorld().addObject(new Zombie3(), 425, 100);
                    getWorld().addObject(new Zombie3(), 750, 100);
                    getWorld().addObject(new Zombie3(), 750, 300);
                    level_1_progress = level_1_progress+5;
                }
            }
        }
to
if(difficulty == 1){Zombie = new Zombie1();}
        if (difficulty == 2){Zombie = new Zombie2();}
        if (difficulty == 3){Zombie = new Zombie3();}

        if(level == 1){            
            if(level_1_progress < 5){                
                getWorld().addObject(Zombie, 100, 300);
                getWorld().addObject(Zombie, 100, 100);
                getWorld().addObject(Zombie, 425, 100);
                getWorld().addObject(Zombie, 750, 100);
                getWorld().addObject(Zombie, 750, 300);
                level_1_progress = level_1_progress+5;                  
            }                
        }
For any reason there is only one zombie (the first one) created when using the new code why?
SPower SPower

2016/5/23

#
First, you should not create variables that start with an uppercase letter - it's confusing syntax wise. And the reason why there's only one zombie appearing is because you're only creating one Zombie instance (be it Zombie1, Zombie2 or Zombie3). You're adding the same object multiple times, but that doesn't mean you're adding multiple objects: you'll have to create a new instance each time. You could try something like this:
Class zombieClass;
if (difficulty == 1) {
    zombieClass = Zombie1.class;
} else if (difficulty == 2) {
    zombieClass = Zombie2.class;
} else {
    zombieClass = Zombie3.class;
}
getWorld().addObject((Actor)zombieClass.getInstance(), 100, 300);
// repeat the last line with different coordinates
Because the constructor for all the zombie classes you show have no arguments (i.e. there's nothing between the brackets), you can call 'getInstance' on its class to get a new instance (i.e. object) of it, which you then have to cast into an actor because getInstance returns an Object, not Actor. We know it's an Actor though (Zombie1, 2 and 3 are all subclasses of Actor), so casting it into an Actor should cause no problems.
danpost danpost

2016/5/23

#
In lines 7 through 11 of the new code, 'Zombie' refers to the same object throughout. You are wanting an array of zombies -- not just one. Fill an array with the appropriate type of zombies (or create five zombies of the same kind and hold them in five different reference variables) depending on the difficulty and then add them into the world depending on level and progress.
BloodMate BloodMate

2016/5/23

#
Trying this I get the Error "cannot find symbol - method getInstance()". Any ideas?
SPower SPower

2016/5/23

#
Edit for my last post, the last line should be this (I think):
getWorld().addObject(zombieClass.newInstance(), 100, 300);
I got the name of the method wrong, and I think it might not be needed to cast anything.
danpost danpost

2016/5/23

#
BloodMate wrote...
Trying this I get the Error "cannot find symbol - method getInstance()". Any ideas?
The method name is 'newInstance' and it should be enclosed within a 'try-catch' structure because it is declared to throw several types of exceptions.
SPower SPower

2016/5/23

#
danpost wrote...
The method name is 'newInstance' and it should be enclosed within a 'try-catch' structure because it is declared to throw several types of exceptions.
Right, forgot about that too. I guess it's been a while since I've used that. Needless to say, your solution is more elegant, but at the same time, mine would take less lines of code. I still think your array-solution would be better for these purposes, though.
BloodMate BloodMate

2016/5/23

#
danpost wrote...
Fill an array with the appropriate type of zombies (or create five zombies of the same kind and hold them in five different reference variables) depending on the difficulty and then add them into the world depending on level and progress.
What do you mean with an array of Zombies? Isn't my older, longer code an array?
danpost danpost

2016/5/23

#
BloodMate wrote...
What do you mean with an array of Zombies? Isn't my older, longer code an array?
Not in the sense of that I mean (or in a programming sense). An array is like a list structure where, instead of multiple different variables, you use one variable name with an index value to point to the different items in the list. The following would create an array of 5 different Zombie1 objects:
Actor[] zombies = new Actor[5]; // allocates 5 locations to store  Actor objects at
for (int i=0; i<5; i++) zombies[i] = new Zombie1(); // fills the array with 5 different Zombie1 objects
danpost danpost

2016/5/23

#
What you could do, instead of using an array, is add the following method to the level manager class:
private Actor getNewZombie(int whichType)
{
    switch (whichType)
    {
        case 1:  return new Zombie1();
        case 2:  return new Zombie2();
        case 3:  return new Zombie3();
    }
    return new Zombie1();
}
Then you can remove the first 3 lines of your revised code and replace 'Zombie' throughout with 'getNewZombie(difficulty)'.
BloodMate BloodMate

2016/5/23

#
Ok... now "zombies" is the variable, right? When I use it like:
getWorld().addObject(zombies, 100, 300);
I get the error "greenfoot.Actor can not be converted to greenfoot.Actor", when I use:
getWorld().addObject(zombies[], 100, 300);
I get the Error " .class expected" and when I use zombie.class , it doesn't help too...
BloodMate BloodMate

2016/5/23

#
"greenfoot.Actor can not be converted to greenfoot.Actor"
I mean ""greenfoot.Actor can not be converted to greenfoot.Actor"
There are more replies on the next page.
1
2