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

2014/4/27

Creating a grid in a non-grid world

1
2
3
4
5
6
danpost danpost

2014/4/30

#
Zzimon wrote...
danpost wrote...
This will be inside the double 'for' loop; you now only need to create a method called 'canPlace' that returns the proper boolean value for whether the actor can be placed there or not.
how do i make the canplace part work of the random generator ?
The 'canPlace' method receive three pieces of information: the chosen actor and the current location coordinates. It will also return a boolean value indicating whether the actor can be placed at those coordinates. Therefore:
1
private boolean canPlace(Actor actor, int x, int y)
would be appropriate for the method declaration. Within the method, return 'false' for all reasons that the actor could not be placed at that location and return 'true' at the end, after all possible fails have been tested.
Zzimon Zzimon

2014/4/30

#
now I'm getting an error message saying "missing method body,or declare abstract" my code for this looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
}
private boolean canPlace(Actor actor, int x, int y);
public void prepare()
{
    for (int row=0; row<getHeight(); row++) for (int col=0; col<getWidth(); col++)
    {
        Actor actor = null;
        while (!canPlace(actor, col, row))
        {
            int which = Greenfoot.getRandomNumber(3);
            if(which == 0) actor = new Sword();
            if(which == 1) actor = new Coin();
            if(which == 2) actor = new Torch();
        }
    }
}
danpost danpost

2014/4/30

#
Zzimon wrote...
now I'm getting an error message saying "missing method body,or declare abstract"
The line was just the first line of the method (the method declaration line). You need to remove the semi-colon (';') you placed after it and add the method body '{ /** some code */ }' at that location.
danpost danpost

2014/4/30

#
Here. I will start you off.
1
2
3
4
5
6
private boolean canPlace(Actor actor, int x, int y)
{
    if (actor == null) return false;
    // other checks returning false if actor cannot be placed
    return true;
}
Zzimon Zzimon

2014/4/30

#
Ok, so this states if there is an actor in the given coordinates, and if there is it returns false and if there isn't it returns true right?
Zzimon Zzimon

2014/4/30

#
which world is this supposed to be in by the way? As far as I understand, the code you've supplied me with so far should at least spawn the tiles, though no matter in which world actor (my main or my bejeweled(puzzle)world) i put it nothing happens.
danpost danpost

2014/4/30

#
No. Line 3 says that if no actor was passed in the argument (if 'null' was passed), then return 'false'. As you certain cannot place a 'null' at any location. Actually, on the first call to the method, the value of 'actor' WILL be 'null'.
danpost danpost

2014/4/30

#
It will go in your puzzle world constructor. Show the code for your (puzzle) world class after you add it in.
Zzimon Zzimon

2014/4/30

#
That's what i have in my puzzle world class at the moment
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Bejeweled_World here.
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bejeweled_World extends World
{
    public Bejeweled_World()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(20, 10, 60);
        GreenfootImage bg = new GreenfootImage("Bejeweled_bg.jpg");
        bg.scale(getCellSize(), getCellSize());
        setBackground(bg);
    }
    private boolean canPlace(Actor actor, int x, int y)
    
        if (actor == null) return false
        // other checks returning false if actor cannot be placed 
        return true
    }
    public void prepare()
    {
        for (int row=0; row<getHeight(); row++) for (int col=0; col<getWidth(); col++)
        {
            Actor actor = null;
            while (!canPlace(actor, col, row))
            {
                int which = Greenfoot.getRandomNumber(3);
                if(which == 0) actor = new Sword();
                if(which == 1) actor = new Coin();
                if(which == 2) actor = new Torch();
            }
        }
    }
}
danpost danpost

2014/4/30

#
You need to call the 'prepare' method from your constructor.
Zzimon Zzimon

2014/4/30

#
ok so I just need to move that part to my Main world and then it should work?
Zzimon Zzimon

2014/4/30

#
well currently I have this in my Main world(my constructor) which then doesn't work because the canPlace method haven't been declared yet
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * This class makes up the background of the game and generates the other PIP 'worlds' in the game
 * such as the Bejeweled part and the adventure part while creating the menu at the left.
 *
 * @author Simon Nanoq Callisen
 * @version 28-04-2014
 */
public class Main_Control extends World
{
    PIP pip;
    PIP pip2;
    boolean wDown;
    boolean sDown;
    Actor mouseActor;
    int mouseOffX, mouseOffY;
     
    public Main_Control()
    {   
        super(1675, 910, 1);
        //paint the background blue
        GreenfootImage background = getBackground();
        background.setColor(Color.blue);
        background.fill();
        //spawn the Bejeweled/puzzle world
        World minor = new Bejeweled_World();
        Class[] order = { Sword.class };
        Class[] order2 = { Torch.class };
        Class[] order3 = { Coin.class };
        pip = new PIP(minor, order);
        addObject(pip, 1070, 605);
    }
    public void prepare()
    {
        for (int row=0; row<getHeight(); row++) for (int col=0; col<getWidth(); col++)
        {
            Actor actor = null;
            while (!canPlace(actor, col, row))
            {
                int which = Greenfoot.getRandomNumber(3);
                if(which == 0) actor = new Sword();
                if(which == 1) actor = new Coin();
                if(which == 2) actor = new Torch();
            }
        }
    }
}
danpost danpost

2014/4/30

#
No. You do not need to move anything. Just add 'prepare();' after line 16 in your Bejeweled_World class.
Zzimon Zzimon

2014/4/30

#
Ok, now my code is like you've instructed, though as far as i understand my tiles should be spawning now, which they aren't
Zzimon Zzimon

2014/4/30

#
in my Main_Control world class i currently only have the PIP part and the public Main_Control() script while i have moved my prepare void back to the Bejeweled_World class
There are more replies on the next page.
1
2
3
4
5
6