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

2012/6/3

Need Help. Method returning 0

guvnor guvnor

2012/6/3

#
I need some help. I'm trying to pass a score to another class using the getScore method, but it keeps returning 0.
    public void score()
    {
        score = score + 1;       
        Test w = (Test) getWorld();
        ScoreBoard sb = new ScoreBoard(score);
        getWorld().removeObjects(getWorld().getObjects(ScoreBoard.class));
        w.addObject(sb, 50, 20);
    }
    
    public int getScore()
    {
        return score;
    }
    public void finalScore()
    {
        Fly fly = new Fly();
        Test w = (Test) getWorld();

        if(alive == false){
            GameOver go = new GameOver(fly.getScore());
            w.addObject(go, 200, 200);
        }    
    }
SPower SPower

2012/6/3

#
Maybe print your score before you return it:
public int getScrore()
{
     System.out.println("" + score);
     return score;
}
If this doesn't return 0, you're doing something wrong in another place.
danpost danpost

2012/6/3

#
I believe the problem resides in the 'finalScore' method. You are creating a 'new' Fly object and using 'getScore' on it (though, it has not had a chance to accumulate any score yet). It makes a difference how you would want to code it, depending on what class this code is actually in. Is 'finalScore' in the 'Fly' class or another class? Is a fly always in the world, or is there a chance that no flies are in the world? EDIT: Also, what class is the variable 'alive' in?
guvnor guvnor

2012/6/3

#
Hey, thanks for the replies. FinalScore is in class spider and is called when the spider collides with the fly. Alive is in class spider. And there is always a fly in the world until it collides with the spider(which is when i want to pass the finalscore method from fly to spider.
danpost danpost

2012/6/3

#
OK, change line 1 in the 'finalScore' method to:
public void finalScore(Fly fly)
and remove line 3. This will make the 'fly' that was in the world the object you get the score from. Then, in the Fly class instead of calling 'spider.finalScore();', call this:
spider.finalScore(this);
I used "spider" here, as I do not know what you actually named the spider in the Fly class.
danpost danpost

2012/6/3

#
Even better, just pass the score to the finalScore method. Use this in place of line 1:
public void finalScore(int score)
and still remove line 3. Also, line 7 will need changed to:
GameOver go = new GameOver(score);
Then, in the Fly class call with this:
spider.finalScore(getScore());
Now, in the 'finalScore method', you can remove line 4, and change line 8 to
getWorld().addObject(go, 200, 200);
danpost danpost

2012/6/3

#
Here is how I see your 'finalScore' method, now:
public void finalScore(int score)
{
    if (!alive) getWorld().addObject(new GameOver(score), 200, 200);
}
danpost danpost

2012/6/3

#
Also, this is how I see your 'score' method in the Fly class:
public void score()
{
    getWorld().removeObjects(getWorld().getObjects(ScoreBoard.class));
    getWorld().addObject(new ScoreBoard(++score), 50, 20);
}
danpost danpost

2012/6/3

#
My only question, now, okay, the spider kills, and eats, the fly; the fly is dead and gone; you now call finalScore; but then ask if the spider is alive. Why? That is, why does the spider have to be not alive for the gameover screen to appear? When will the spider die? (Okay, that's more than one question!)
guvnor guvnor

2012/6/3

#
Hey thanks a lot for the help. Going to get out of bed and test this code now. The reason i have alive in spider was because i sort of had the same problem. I tried passing alive to the fly class when it equalled false but it always returned true, like how the score always returned 0.
guvnor guvnor

2012/6/3

#
So alive is changed from true to false when the spider eats the fly.
guvnor guvnor

2012/6/3

#
Also, to your post above shouldn't I be calling spider.finalScore(getScore()); in the Spider class when the spider eats the fly? I'll show you what I have now. Code from the Fly class:
    public void score()
    {
        getWorld().removeObjects(getWorld().getObjects(ScoreBoard.class));  
        getWorld().addObject(new ScoreBoard(++score), 50, 20);     
    }
    
    public int getScore()
    {
        return score;
    } 
Code from the Spider class:
    public void eatFly()
    {
        Actor fly = getOneObjectAtOffset(0, 0, Fly.class);
        Test w = (Test) getWorld();
        
        if(fly != null)
        {
            alive = false;
            w.removeObject(fly);
        }
    }
    
    public void finalScore(int score)
    {       
        Fly fly = new Fly();
        finalScore(fly.getScore());
        
        System.out.println(score);
        
        if (alive == false){
            getWorld().addObject(new GameOver(score), 200, 200); 
        }        
    }
danpost danpost

2012/6/4

#
Remove everything you just posted that is in the Spider class (remove the 'eatFly' method and the 'finalScore' method). Oh, and remove the call (probably in your Spider's act method) to 'eatFly'. Now, in the Fly class, add the following:
public void getEaten()
{
    if (getOneObjectAtOffset(0, 0, Spider.class) != null)
    {
        getWorld().addObject(new GameOver(getScore()), 200, 200);
        getWorld().removeObject(this);
        Greenfoot.stop();
    }
}
and call this from the Fly's act method.
guvnor guvnor

2012/6/4

#
Awesome, this works now! Thank's a lot Dan. That was bloody frustrating, may have to start using these forums more often :P
danpost danpost

2012/6/4

#
Glad you have it working now! You can remove the 'alive' variable from the spider class, as it is not being used.
You need to login to post a reply.