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

2020/1/4

Getting world to use class methods

PowerDj PowerDj

2020/1/4

#
In the game I'm programming, I need the world to be able to instantiate a "block". In the Block class, I already have a method, called generateBlock(), which generates a block. Ideally, I want the world to use something like "Block.generateBlock()" to instantiate a block, but because generateBlock() uses static methods, I cannot do this. Is there any way I can get around this, and tell the world to use a static method from the Block class?
danpost danpost

2020/1/4

#
PowerDj wrote...
In the Block class, I already have a method, called generateBlock(), which generates a block
Please show generateBlock method coding.
PowerDj PowerDj

2020/1/4

#
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
public void generateBlock() {
    //randomly generate a colour (out of 4 possible) and shape (out
    //of 9 possible) for the next block
    String blockColour = Integer.toString(Greenfoot.getRandomNumber(4));
    String blockShape = Integer.toString(Greenfoot.getRandomNumber(2));
 
    //instantiate a new image
    GreenfootImage blockImage;
    //assign an image based on the randomly generated numbers
    //images names use 'colour_shape' to make them all the same
    blockImage = new GreenfootImage(blockColour + "_" + blockShape + ".png");
 
    //finally, instantiate a new block and give it the generated image
    setImage(blockImage);
 
    //the rotation of the block must be set back to normal before placing it
    //since it retains the rotation of when it was placed otherwise
    setRotation(0);
 
    //each block shape has a different centre point
    //therefore they need to be placed in slightly different places
    //to ensure that they are put onto the grid
    if(blockShape.equals("0")){
        setLocation(432,382);
    }else if(blockShape.equals("1") || blockShape.equals("2") || blockShape.equals("3") || blockShape.equals("4")){
        setLocation(445,382);
    }else if(blockShape.equals("5") || blockShape.equals("6")){
        setLocation(432,368);
    }else if(blockShape.equals("7") || blockShape.equals("8")) {
        setLocation(445,368);
    }
}
danpost danpost

2020/1/4

#
PowerDj wrote...
<< Code Omitted >>
Okay -- now I need to see all code-blocks in your World subclass dealing with the Block objects (including your attempt(s) to call the given method).
PowerDj PowerDj

2020/1/4

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void prepare()
{
    leftEdge leftEdge = new leftEdge();
    addObject(leftEdge,204,382);
    upperEdge upperEdge = new upperEdge();
    addObject(upperEdge,460,179);
    rightEdge rightEdge = new rightEdge();
    addObject(rightEdge,717,382);
    lowerEdge lowerEdge = new lowerEdge();
    addObject(lowerEdge,460,586);
    Grid grid = new Grid();
    addObject(grid,459,382);
    generateBlock();
    setPaintOrder(Block.class, placedBlock.class);
}
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
public void generateBlock() {
    //randomly generate a colour (out of 4 possible) and shape (out
    //of 9 possible) for the next block
    blockColour = Integer.toString(Greenfoot.getRandomNumber(4));
    blockShape = Integer.toString(Greenfoot.getRandomNumber(9));
     
    //instantiate a new image
    GreenfootImage blockImage;
    //assign an image based on the randomly generated numbers
    //images names use 'colour_shape' to make them all the same
    blockImage = new GreenfootImage(blockColour + "_" + blockShape + ".png");
     
    //finally, instantiate a new block and give it the generated image
    Block block = new Block();
    block.setImage(blockImage);
     
    //each block shape has a different centre point
    //therefore they need to be placed in slightly different places
    //to ensure that they are put onto the grid
    if(blockShape.equals("0")){
        addObject(block,432,382);
    }else if(blockShape.equals("1") || blockShape.equals("2") || blockShape.equals("3") || blockShape.equals("4")){
        addObject(block,445,382);
    }else if(blockShape.equals("5") || blockShape.equals("6")){
        addObject(block,432,368);
    }else if(blockShape.equals("7") || blockShape.equals("8")) {
        addObject(block,445,368);
    }
}
At the moment the world has its own generateBlock() method identical to the other one but this isn't ideal for what I want to do.
1
2
3
4
5
public void generateBlock() {
    Block block = new Block();
 
    block.generateBlock();
}
I tried this, which compiles but doesn't generate a block.
1
2
3
public void generateBlock() {
    Block.generateBlock();
}
I also tried this, which doesn't compile because generateBlock() isn't static.
danpost danpost

2020/1/4

#
The "new Block", aka block, needs to be added into the world before you can call "block.generateBlock". You can add it at coordinates (0, 0).
PowerDj PowerDj

2020/1/4

#
Works perfectly, huge thanks!
You need to login to post a reply.