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

2016/2/29

Beginner Sokoban Help

Mr.$mith Mr.$mith

2016/2/29

#
Hello, I'am trying to make Sokoban game as school project in Greenfoot, but I ran into a problem. When player moves to left and bump into a box I'am trying to call a method that will move the box to left as well. But nothing is happening, beacouse I have declared the Box in Player, but theboxes are added to the map by World. How can I do a method that will move the only box I bumped into (there are 3 boxes)?
danpost danpost

2016/2/29

#
Mr.$mith wrote...
I have declared the Box in Player
Please show the method the code you are referring to is in. Also, for any fields used within the code, show their declaration statements.
Mr.$mith Mr.$mith

2016/3/1

#
In World I add Box, Player and Wall to the map with coordinates. I wanted Player to push the Box when it touches it Box
    public void Left()
    {
        setLocation (x-1,y);
        return;
    }
Player
            
Box b = new Box;

public void Act()
{

if (isTouching(Box.class))
            {
                b.Left(); 

            }

}
But it doesn't work with the boxes that are already at the map, I need to add
getWorld().addObject(b,11,11);
to Player to make it work. But that only works with only this one box.
danpost danpost

2016/3/3

#
Okay, yes ... you have declared a Box field in the Player class; however, you will not know which box to assign to that field until you determine which box the player is trying to push (you have already found that creating a new box will not work here). I have a sample sokoban movement demo scenario which has code that does what you require. It is here.
Mr.$mith Mr.$mith

2016/3/12

#
Okay, now I know... Thanks alot danpost!
You need to login to post a reply.