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

2016/12/2

What is the meaning of this Source Code ?

Conscience Conscience

2016/12/2

#
i got this code from Scenarios on this website but i dont understand why got so many constructor that differ in length between parameters and its content
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
     * First 2 collumns: Width and Height of the whole World
     * 3rd & 4th collumns: Width and Height of the Screen, that should be shown
     * 5th collumn: CellSize
     * The Position on the left uppon corner is 0,0
     * Background Moves,too
     */
    public Scrollworld(int Width, int Height,int ScreenWidth,int ScreenHeight,int CellSize)
    {
        this(Width,Height,ScreenWidth,ScreenHeight,0,0,CellSize,true);
    }
 
    /**
     * First 2 collumns: Width and Height of the whole World
     * 3rd & 4th collumns: Width and Height of the Screen, that should be shown
     * 5th & 6th collumns: The Position on the left uppon corner
     * 7th collumn: CellSize
     * Background Moves,too;
     */
    public Scrollworld(int Width, int Height,int ScreenWidth,int ScreenHeight,int CellSize,boolean BackgroundMove)
    {
        this(Width,Height,ScreenWidth,ScreenHeight,0,0,CellSize,BackgroundMove);
    }
 
    /**
     * First 2 collumns: Width and Height of the whole World
     * 3rd & 4th collumns: Width and Height of the Screen, that should be shown
     * 5th & 6th collumns: The Position on the left uppon corner
     * 7th collumn: CellSize
     * Background Moves,too;
     */
    public Scrollworld(int Width, int Height,int ScreenWidth,int ScreenHeight,int ScreenLeft,int ScreenUp,int CellSize)
    {
        this(Width,Height,ScreenWidth,ScreenHeight,ScreenLeft,ScreenUp,CellSize,true);
    }
 
    /**
     * First 2 collumns: Width and Height of the whole World
     * 3rd & 4th collumns: Width and Height of the Screen, that should be shown
     * 5th & 6th collumns: The Position on the left uppon corner
     * 7th collumn: CellSize
     * 8th collumn: should the background move, too
     */
    public Scrollworld(int Width, int Height,int ScreenWidth, int ScreenHeight,int ScreenLeft,int ScreenUp, int CellSize, boolean BackgroundMove)
    {
        super(ScreenWidth, ScreenHeight, CellSize, false);
        if(ScreenWidth>Width)
            ScreenWidth=Width;
        if(ScreenHeight>Height)
            ScreenWidth=Height;
        this.Width=Width;
        this.Height=Height;
        this.BackgroundMove=BackgroundMove;
        this.CellSize=CellSize;
        if(BackgroundMove)
        {
            Background=new GreenfootImage(Width,Height);
            Background.setColor(Color.white);
            Background.fillRect(0,0,Width,Height);
        }
        ShowWorld(ScreenLeft, ScreenUp);
    }
danpost danpost

2016/12/2

#
Conscience wrote...
i got this code from Scenarios on this website but i dont understand why got so many constructor that differ in length between parameters and its content < Code Omitted >
Actually, the last constructor is the only one needed. All the others just provide default values to some of the parameters of the last one.
Conscience Conscience

2016/12/3

#
danpost wrote...
Conscience wrote...
i got this code from Scenarios on this website but i dont understand why got so many constructor that differ in length between parameters and its content < Code Omitted >
Actually, the last constructor is the only one needed. All the others just provide default values to some of the parameters of the last one.
can u help me explaining the last constructor please ? i dont understand what it means .. im so confused right now...
danpost danpost

2016/12/3

#
Conscience wrote...
can u help me explaining the last constructor please ? i dont understand what it means
Line 44 is the constructor signature line. The eight parameters consist of two for the dimensions of the scrolling area, two for the dimensions of the view window (the world dimensions), two for the initial horizontal and vertical scrolling offsets, one for the cell size of the world and one to indicate whether the background image is to scroll or not. Line 46 call the World class constructor to create the world (using the world dimensions given). Lines 47 through 50 ensure that the scrolling area is not less than the world area. Lines 51 through 54 save the values given (or their adjusted values) in instance fields. Lines 55 through 60 creates a base image the size of the world, if the background is to scroll. Line 61 calls a method to perform the initial scrolling (to the scrolling offsets given).
You need to login to post a reply.