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

2014/7/12

please help me with my scenario

vencetest vencetest

2014/7/12

#
http://www.greenfoot.org/scenarios/11819 please please need help.
Ambar Ambar

2014/7/12

#
what help do you need
danpost danpost

2014/7/12

#
As far as getting the score to the scoreboard in the game over world, you first need a 'get' method in the Score class to return the value of 'int Score'. Then, you will need a local reference to the new game over world that you create in the Human class so you can change the 'int Score' value of its scoreboard to that of the scoreboard in the PoringShoot world:
if (lives == 0)
{
    GameOver gameOver = new GameOver(); // gets local reference to game over world
    // the following sets scoreboard in gameover world to the one in poringshoot world
    int score = ((PoringShoot)getWorld()).getScoreboard().getScore();
    gameOver.getScoreboard().updateScore(score);
    Greenfoot.setWorld(gameOver); // sets the game over world as the new active world
}
Something to be considered: with the lives value checking in the Human class, each human will perform that checking. This is not very efficient. Plus, with the code you have now, you get errors when the mouse is clicked and two Humans are touching each other; as well as when a Human instance goes off the right edge of the world. The first error is mainly due to your 'disShot' method. I think what you were trying to do was have any Human clicked on get removed with a lose of a life. Right now, the 'if (Greenfoot.mouseClicked(null))' is saying to execute the 'if' block if the mouse is clicked ANYWHERE, which is obviously not what you want to ask here. Then, the 'getOneObjectAtOffset(0, 0, Human.class)' says to return any OTHER Human object whose image intersects the middle of my image (the image of the Human object the method is currently running on, or for -- which is the Human actor whose turn it is for its 'act' method to be run on in this act cycle). There, I believe you were trying to get a reference to the Human actor whose act method was currently running. The following would do what I believe you wanted:
if (Greenfoot.mouseClicked(this))
{
    PoringShoot poringShoot = ((PoringShoot)getWorld();
    poringShoot.removeObject(this);
    // adjust lives
}
'this' is a java keyword that is used to refer to the object of the method -- the instance of the class that the method is being run on or for. One reason for the second error (when human goes off right edge of world) is because you have 'tjekKant' as the first call in your 'act' method of the Human class. However, you have another method (at the moment) that can also remove the actor from the world. You cannot run either (or any method that requires the object still be in the world) after the other without first making sure that the first did not remove the actor from the world (this can be done using 'if (getWorld() != null)'. But, there is a more basic flaw. You have the 'lives' field in the Human class. You can look at it this way also: the field 'lives' is not tracking the lives of each Human instance created; it is more or less tracking (or at least trying to track) the lives of ALL Human objects. By decreasing the value of 'lives' in one instance of the Human class, it does not change the lives in the others. After fixing the right edge removal problem, you will find that, if clicking infrequently, the number of Lives objects will not decrease. Also, after fixing both above, as long as humans are not hit very often, even with frequent clicks, the number of Lives objects will not decrease when a human is hit (this is after is it already decreased once). A better place for your 'lives' field would be in your PoringShoot class; as well as for its value checking. Each Human instance can inform the world if it was clicked on, individually. Place a method in the PoringShoot class to adjust the lives and have it deal with the Lives objects as needed. In fact, you will only need to check the value of 'lives' when it is changed (in this method the Human objects will call when hit).
You need to login to post a reply.