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

2013/3/13

How do i end the game ?

1
2
Nz-Tank Nz-Tank

2013/3/13

#
I want to end the game after "Slenderman" subclass is destroyed 20 times. I have no idea where to start, so i would appreciate any help
ColeoCofer ColeoCofer

2013/3/13

#
I would add an instance variable called something like "slendermanCounter" into the Slenderman class. Ex: private int slendermanCounter = 0; Don't forget to put this inside the class, but not inside of a function. Then when ever the slenderman gets destroyed, you simply add 1 to slendermanCounter. if (slenderman is eaten) { slendermanCounter++; } Then you need to check if slendermanCounter is equal to 20. if (slendermanCounter == 20) { Greenfoot.stop(); //Or what ever you want to do when the game ends here } Hope this helps! - Cole
Nz-Tank Nz-Tank

2013/3/13

#
Now this goes inside the slenderman class? or does it go inside the world class?
Nz-Tank Nz-Tank

2013/3/13

#
what is "(slenderman is eaten)" ? how do i actually use that?
ColeoCofer ColeoCofer

2013/3/13

#
It would go into the Slenderman class (Or the class that "destroys" the slenderman). I put the code in my player class, because in that class it checks when it eats coins. So you might want to put it in what ever class that destroys the slenderman. Here is an example from the methods we learned from class: I put it into a method to be organized. public void eatCheck() { if (canSee(Slenderman.class)) { //These two lines is the (slenderman is eaten) eat(Slenderman.class); slendermanCounter++; if (slendermanCounter == 20) { //If this is true, then 20 slenderman have been destroyed. Greenfoot.stop(); } } }
ColeoCofer ColeoCofer

2013/3/13

#
Here are the two methods too: public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if (actor != null) { getWorld().removeObject(actor); } }
Nz-Tank Nz-Tank

2013/3/13

#
so do i put all of this in the class that is going to be destroying the slenderman? for instance, i have the character throw a rock. I want slenderman to then die (which i have done). The only problem is i put this into the rock class and the game doesn't end.
danpost danpost

2013/3/13

#
The problem with putting it in the class that destroys the slenderman is that each destroyer would be using a different counter. The problem with putting it in the slenderman class is that when you remove it from the world, the counter goes also. Best would be to put the lives counter in the world class; and a simple check can be used in the world 'act' method:
if (getObjects(Slenderman.class).isEmpty())
{
    "lives counter"--;
    if ("lives counter" == 0)
    {
        // end game
    }
    else
    {
        // add new slenderman to world
    }
}
Nz-Tank Nz-Tank

2013/3/13

#
So now i am confused. Do i need all that other script or no? do i simply need the 'lives counter' in the world class that yu recommended, danpost?
danpost danpost

2013/3/14

#
As far as counting down lives and acting on any changes, this is all you need.
Nz-Tank Nz-Tank

2013/3/14

#
But what happens is when i compile and run, my characters slowly go down one by one instead of replacing after they go below 5. Aside from that, they game doesn't end either. what do i do now???
danpost danpost

2013/3/14

#
Please show the code you added to the world and the code for the Slenderman class.
Nz-Tank Nz-Tank

2013/3/14

#
World... public class Ground extends World { private int livescounter = 5; public void act() { if (getObjects(Slenderman.class).isEmpty()) if (livescounter == 0) { Greenfoot.stop(); } if (getObjects(Slenderman.class).size()<5) { addObject(new Slenderman(), getWidth()-1, Greenfoot.getRandomNumber(getHeight())); } if (getObjects(player.class).isEmpty()) { addObject(new Death(), 500, 500); } } And Slenderman... public class Slenderman extends Mover { /** Size of Slenderman */ private int size; /** When the stability reaches 0, Slenderman will explode */ private int stability; /** * finds player to capture him. **/ public void act() { move(-10); turnAtEdge(); randomTurn(); move(); lookForPlayer(); } /** * Create Slenderman with default size and speed. */ public Slenderman() { this(64); } /** * Create Slenderman with a given size and default speed. */ public Slenderman (int size) { this(size, new Vector(Greenfoot.getRandomNumber(360), 2)); } /** * Create an asteroid with a given size size and speed. */ private Slenderman(int size, Vector speed) { super(speed); setSize(size); } /** * Set the size of Slenderman. Note that stability is directly * related to size. Smaller asteroids are less stable. */ public void setSize(int size) { stability = size; this.size = size; GreenfootImage image = getImage(); image.scale(60, 180); } /** * Return the current stability of Slenderman (If it goes down to * zero, he dies). */ public int getStability() { return stability; } /** * Hit Slenderman dealing the given amount of damage. */ public void hit(int damage) { stability = stability - damage; if(stability <= 0) Die(); } /** * Slenderman dies. */ private void Die() { Greenfoot.playSound("SlendermanDeath.wav"); if(size <= 16) { getWorld().removeObject(this); return; } else { int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45); double l = getMovement().getLength(); Vector speed1 = new Vector(r + 60, l * 1.2); Vector speed2 = new Vector(r - 60, l * 1.2); getWorld().removeObject(this); } } /** * Check whether we are at the edge of the world. If we are, turn a bit. * If not, do nothing. */ public void turnAtEdge() { if ( atWorldEdge() ) { turn(17); } } /** * Randomly decide to turn from the current direction, or not. If we turn * turn a bit left or right by a random degree. */ public void randomTurn() { if (Greenfoot.getRandomNumber(100) > 90) { turn(Greenfoot.getRandomNumber(90)-20); } } /** * If Slenderman encounters the player, then the player is abducted and the game ends. */ public void lookForPlayer() { if ( canSee(player.class) ) { Greenfoot.playSound("Abducted.wav"); Greenfoot.stop(); } } }
danpost danpost

2013/3/14

#
In the world class: first, you do not change the counter value; second, the form is nowhere near what I posted (there is no else clause; the blocks are not similar to what I suggested); you have a check to see if there are less than 5 Slenderman objects in the world (do you have more than one at a time?).
Nz-Tank Nz-Tank

2013/3/14

#
Oh dear. Well I ammm new to greenfoot so ill use that as my excuse.... but i have 6 Slenderman in the world to begin with. that i know for sure. (and also i did put yours in word for word so i think this is after me trying to change it around in hopes that i come out with a solution).
There are more replies on the next page.
1
2