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

2015/2/19

getScore() not getting get?

primepanda1 primepanda1

2015/2/19

#
Hello fellow programmers, I seem to be having trouble with my code. You see, I have a counter that goes on in "Eating" and "Governor" is a subclass of eating. The counter increments and increases score, and there is a method in "Eating" called "getScore()." It returns the score, and for some reason in the "Governor" class, it is not getting the score, take a look. (EATING CLASS CODE)
    public void act()
    
    {
        counter(); //calls the method
    }
    private void counter(){ //increments the score
        counter += 1; //increments the counter by  one
        if (counter % 1 == 0) { //everytime counters mod equals 0 it runs this        
            score++; //increments the score
            System.out.print("soup"); //test to see if it is bing reached(it is)
        }     
    }
    public int getScore(){ //this gets the score and returns it
        return score; //returns score
    }
(GOVERNOR CODE)
    public void governorBoss(){
        if (getScore() == 101){ //this gets score from the eating class
            System.out.println("test"); //to test if the code is being reached (it is not)
            setImage(Boss); //this sets image to a bigger governor

        }
        if (getScore()== 100){ //when score equals 100, run this
            getWorld().removeObject(this); //removes object from the world
            System.out.println("ssn"); //test to see if the code is being reached (it is not)
        } 
    }  
I thought maybe it's because the "Eating" wasn't in the world, so I made a blank image and set it on "Eating," and spawned eating in the world, and tested to see if it is actually counting, so I got the score, and it returned what it should have been. I had it where the counter was in the "Governor" class, but that didn't work because each governor had it's own counter, so I made the "Eating" class have one counter that all the "Governors" can reach, but unfortunately it is not. My main objective is to get all the "Governors" to be gone when the "score" reaches 100, and to set the image of the governor to something else, I don't know if there is anyway to do that, but if there is, tell me! Thanks programmers! (sorry if some of this seems obscure for I am new to programming)
Super_Hippo Super_Hippo

2015/2/19

#
I guess you have a line like "private int score;" in your Eating class (and not in your Governor class). This means that every object of type Eating and subclasses (in your case objects of the class Governor) has its own score field. If it would be 'static', only one score field would be used by every object of the class and their subclasses, so you could say that a static variable is a variable of a class and not of an object. Your goal is to remove all Governors when the score reaches 100. This would happen in less than two seconds if there is an object of "Eating" in the world, if you would implement it like you tried here. I don't exactly know what this is supposed to do, but the following line will always be true, because if you divide an integer value by 1, there will never be a remainder.
if (counter % 1 == 0)
I don't think that you need a class called "Eating". I also don't see the reason for the name. The easiest way would be to count the score in the world subclass, so it could look like this (→better change the 100 to something bigger).
private int score=0;

public void act()
{
    score++;
    if (score==100) removeObjects(getObjects(Governor.class));
}
danpost danpost

2015/2/19

#
primepanda1 wrote...
I had it where the counter was in the "Governor" class, but that didn't work because each governor had it's own counter, so I made the "Eating" class have one counter that all the "Governors" can reach, but unfortunately it is not.
Moving the counter to the Eating class does not change the fact that each instance (whether created from the class the counter is in or created from one of its subclasses) will have its own counter. Remember, an instance of a subclass is still an instance of its superclasses (all the way back to the Object class which is a superclass for everything). You can put the counter in your world class (which has only one instance) to maintain just one of them. As an alternative, but not considered very good programming, you can make the field a 'static' field. That will make it a class field rather than an instance field. All instances will then be able to use the ONE counter field. If score does not increment more than once per act cycle and 'governerBoss' is called every act, then the score will never be '101' to set a bigger image for the governer (the actor is removed from the world when score reaches '100').
You need to login to post a reply.