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

3
4
5
6
Zzimon Zzimon

2014/4/30

#
Awesome! won't i be needing one for right and down too?
danpost danpost

2014/4/30

#
Just do not spawn tiles unless there is an unoccupied location.
Zzimon Zzimon

2014/4/30

#
Now i have this
    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 && getObjectsAt(x, y-2, Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass())  
        { return false; }  
    } 
But i am still getting the error that all of the if statement is an 'unreachable statement'
danpost danpost

2014/4/30

#
Zzimon wrote...
Awesome! won't i be needing one for right and down too?
Absolutely .... NOT. Because we are placing them in an across'n'down fashion, there will never be any tiles to the right or below as you place them.
danpost danpost

2014/4/30

#
Zzimon wrote...
Now i have this
    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 && getObjectsAt(x, y-2, Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass())  
        { return false; }  
    } 
But i am still getting the error that all of the if statement is an 'unreachable statement'
Line 7 is one of those checks mentioned in my comment at line 4. Move line 7 to where it is supposed to go.
Zzimon Zzimon

2014/4/30

#
so should the actor == null if statement at line 3 be removed or should i put it in the && line with the rest?
danpost danpost

2014/4/30

#
Zzimon wrote...
so should the actor == null if statement at line 3 be removed or should i put it in the && line with the rest?
That should be left alone. It should be the first statement; and it should return false if it alone is true.
Zzimon Zzimon

2014/4/30

#
Ok. Awesome thank you so very much for the help! :D I'll come back to post more in here if I need more help :)
Zzimon Zzimon

2014/4/30

#
Ok, rather quickly ran into a problem. My plan is with each of the tile actors that i can click them and then when they are selected move them up, down left or right, this I believe I can do with my experience so far but then i have to get the two actors to switch places, and I can't really think of any smart way to do that so I'd like to ask if you had an 'easy'/smart way to go around that.
Zzimon Zzimon

2014/4/30

#
Also if there is a way to select an actor with the Greenfoot.mouseClicked(this) command, so to keep the object selected until one presses a certain key with the Greenfoot.isKeyDown("s") command
Zzimon Zzimon

2014/4/30

#
I tried using this code
public int state = 0;
    public void act()
    {
        if (state == 0)
        {
            if (Greenfoot.mousePressed(this))
            {
                state = 1;
            }
        }
        else if (state == 1)
        {
            movement();
        }
    }
    public void movement()
    {
        if (Greenfoot.isKeyDown("w") && Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("s") && Greenfoot.isKeyDown("d"))
        {
            setLocation(getX(), getY() - 1);
            state = 0;
        }
    }
even though i get no errors nothing happens when i click a sword tile, which is the one i put the code into
danpost danpost

2014/4/30

#
Instead of using an 'int' field, try an 'Actor' field. When an actor is clicked, set it to the actor. When a switch is made, set it back to 'null'. Since you do not want more than one Actor to be selected at any time, the 'Actor field will need to be 'static' or be located in your world subclass. That way, if a different one is clicked on before a key is pressed, it will change its value to it (instead of two tiles thinking that they are selected at the same time -- you certainly do not want to have two sets of tiles swapped with each other).
Zzimon Zzimon

2014/5/1

#
New problem, after saving this project in my Dropbox some of the things have been altered quite a bit i think. I changed the code back to what it was before but now i get the message: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.get(ArrayList.java:411) at Bejeweled_World.canPlace(Bejeweled_World.java:25) at Bejeweled_World.prepare(Bejeweled_World.java:34) at Bejeweled_World.<init>(Bejeweled_World.java:17) at Main_Control.<init>(Main_Control.java:27) my current code for Main_Control and Bejeweled_World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;
/**
 * 
 */
public class Bejeweled_World extends World
{
    public Bejeweled_World()  
    {   
        super(20, 10, 2);   
        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;    
        if (y > 1) // zero and one are the first two rows      
        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; }  // 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();  
            }  
            addObject(actor, col, row);  
        }  
    }  
}  
and Main_Control:
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);
    }
}
Zzimon Zzimon

2014/5/1

#
I fixed the previous problem with the outofbounds statement by replacing the folder with an older copy.
Zzimon Zzimon

2014/5/1

#
I don't quite understand what you mean with the Actor field, could you give an example of how to use it?
You need to login to post a reply.
3
4
5
6