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

2015/4/22

Help - Trying to move a score over to anotehr subclass

alittle_bit alittle_bit

2015/4/22

#
Kind of new to teh greenfoot scene and have found myself quite teh predicament. I am trying to move a score through one class (called ship_1, dont question teh '1') to its subclass (Score). The idea is that as the ship gets more points the score will change the number of score until when it gets to 10, there will be a winning screen (or loosing if u hit the edge of the world) So this is kind of needed. This is the Ship of course
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class ship_1 extends Mover
{   
        //gives the word 'Score' meaning
    int Score = 0;
     
    /**
     * This will remove Enemy if the player connects to one
     * We will add to a score board to allow for a game
     */
    public void absorb()
    {
         if (isTouching(Enemy.class))
        {
            removeTouching(Enemy.class);
            Score = Score + 1;
        }
    }
    //this code alows for us to check the score when testing
    public int displayScore()
    {
        return Score;
    }
     
    public void wall()
    {
        if ( isAtEdge () )
       {
        getWorld().removeObject(this);
       }
    }
     
    public void act()
    {
        Control_1();
        absorb();
        wall();
    }   
}
And this is the Score
1
2
3
4
5
6
7
8
9
10
11
public class Score extends ship_1
{
    /**
     * If the score of teh ship rises, then this class will change image
     */
    public void act()
    {
        int score = displayScore();
        setImage(new GreenfootImage(score + ".png"));
    }   
}
Problem revolves around the fact that as the Ship class gets points the Score class does not, I've been told by a friend they know what I need but they haven't gotten in touch yet and thought id put this out there in case you guys have a better idea... Any ideas?
danpost danpost

2015/4/22

#
First and foremost, a Score object is NOT any type of ship_1 object. That means that you should not subclass ship_1 with Score. You should be able to describe any class using the same words as you describe any class it extends and you cannot do that with Score an ship_1. When extending a class, a more specific type of object will be described in the subclass; but, it does not change types. All objects created from the subclass will get all the state (fields) and behavior (methods) of the class it extends. The reason your Score class does not increase points when your ship increases point is because both objects have a Score field of their own; this is a due to the extending of the ship_1 class. Now, you have this class called 'Score'. I know that it can be difficult at times to come up with names for classes and fields; however, to keep things from getting confusing, you should avoid naming any variables with the same name as a class. Using conventional naming, this would never happen anyway as class names would begin with uppercase characters and variable names would begin with lowercase characters. At any rate, naming the 'int' field in the ship_1 class with 'Score' is not a good idea ('score' or 'points' would be fine). You will need to redesign your Score class as line 8 will no longer be able to call the 'displayScore' method.
alittle_bit alittle_bit

2015/4/23

#
Well I understand I made a mess of the classing, making something sub-classes of others in an attempt I later found still left them with their own 'score field'. I have however think Ive got it. If whenever I run the absorb method, it includes changing the score class's image and then have the 'Score' class check to see if it changes image to lets say a 10 for the score, when that happens, place the 'Win' object down and stop the game... This may take up unnecessary time if I ever repeat but it should work. Thanks for posting anyway. (for the first program I've written from scratch in Greenfoot before I think I can not bother fixing classes super and sub class)
You need to login to post a reply.