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

2
3
4
5
6
danpost danpost

2014/4/30

#
Zzimon wrote...
Again where is this supposed to go, I'm guessing in each of my tile actors (Sword, Torch and Coin) right?
NO. ALL the code for placing the initial objects into the world goes in your subclass of World -- in this case, your Bejeweled_World class.
Zzimon Zzimon

2014/4/30

#
so is this code correct or? and again where does it go, in my prepare void right?
1
2
3
4
5
if (y > 1) // zero and one are the first two rows   
if (getObjectsAt(x, y-2).get(0).getClass() == actor.getClass()) 
{ return false; } 
if (getObjectsAt(x, y-1).get(0).getClass() == actor.getClass()) 
{ return false; }
danpost danpost

2014/4/30

#
We were working on the code within the 'canPlace' method.
Zzimon Zzimon

2014/4/30

#
ok, thanks, sorry forgot that bit ^^'
Zzimon Zzimon

2014/4/30

#
EDIT I just realized that i had, again placed it in the wrong section. Here it is in the proper place ( i think) though now i get the error saying 'method getObjectsAt in class greenfoot.World cannot be applied to given types; required: int,int,ava.lang..Class; found:int,int; reason: actual and formal argument lists differ in length'
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
{
    public Bejeweled_World()
    {
        super(20, 10, 60);
        GreenfootImage bg = new GreenfootImage("Bejeweled_bg.jpg");
        bg.scale(getCellSize(), getCellSize());
        setBackground(bg);
        prepare();
    }
    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
         
        if (y > 1) // zero and one are the first two rows   
        if (getObjectsAt(x, y-2).get(0).getClass() == actor.getClass()) 
        { return false; } 
        if (getObjectsAt(x, y-1).get(0).getClass() == actor.getClass()) 
        { return false; } 
    }
    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();
            }
            addObject(actor, col, row);
        }
    }
}
Zzimon Zzimon

2014/4/30

#
fixed previous code section though now i'm getting another error :/ 'method getObjectsAt in class greenfoot.World cannot be applied to given types; required: int,int,ava.lang..Class; found:int,int; reason: actual and formal argument lists differ in length'
danpost danpost

2014/4/30

#
The error message is telling you that there is a third argument, of type Class, that you need to include in the call to 'getObjectsAt'. Please refer to the World class documentation on that method.
Zzimon Zzimon

2014/4/30

#
So I need to put in a class like (x, y, Coin) or what? This doesn't seem to work though, and that's the only thing that comes to my mind after checking the documentation
danpost danpost

2014/4/30

#
Zzimon wrote...
So I need to put in a class like (x, y, Coin) or what? This doesn't seem to work though, and that's the only thing that comes to my mind after checking the documentation
Well, if you were looking for one specific class, like Coin, you would use 'Coin.class'. However, since we know absolutely that there are only tile objects in the world (at least at the time they are being initially placed into the world), we can use either 'Actor.class' or just 'null' (which includes all classes in the check).
Zzimon Zzimon

2014/4/30

#
ok, corrected it to this:
1
2
3
4
5
6
7
8
9
10
11
12
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
         
        if (y > 1)
        if (getObjectsAt(x, y-2, Actor.class).get(0).getClass() == actor.getClass()) 
        { return false; } 
        if (getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass()) 
        { return false; } 
    }
though even after this i get that the if (y>1) statement is unreachable :/ EDIT: Edited for Actor.class
danpost danpost

2014/4/30

#
You know ... I had a 'Match 3' scenario on this site some time ago and it got very little attention. Maybe I will upload it again after adding a high scores screen to it and allowing the game to be saved for resuming at a later time.
danpost danpost

2014/4/30

#
The 'return true;' statement must be the last statement in the method. Also, you are still doing individual checks. Lines 7 through 11 must be combined into one statement where when all three conditions are true, a false value is returned.
Zzimon Zzimon

2014/4/30

#
Heh. That would be great, does it use the same things that i need? And still do you know how i can fix the if(y>1) thing?
Zzimon Zzimon

2014/4/30

#
so would this work?
1
2
if (y>1 && getObjectsAt(x, y-2m Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass())
{return false;}
danpost danpost

2014/4/30

#
Zzimon wrote...
so would this work?
1
2
if (y>1 && getObjectsAt(x, y-2m Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass())
{return false;}
That is it! (except for the typo after the '2') Now, do similar for going 'left' instead of going 'up'.
There are more replies on the next page.
2
3
4
5
6