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

2017/4/6

getNeighbours as movement pattern

Drane Drane

2017/4/6

#
hey guys, for a game I program in need to make my objects move only 1 cell in a cross. therefore i want to use the getNeighbours method to check if there are other objects in this cells and if not the object can move there. my problem is that i use the mouseDrag methods to move my objects and i have no idea how i can limit the movement to 1 cell in each direction. my movement code looks like this
    public void bewegen2()
    {
        if(Greenfoot.mouseDragged(this) && wechsel.spieler == 2 && wechsel.phase ==2 )
        {
            MouseInfo info = Greenfoot.getMouseInfo();
            setLocation(info.getX(), info.getY());
        }
        if(Greenfoot.mouseDragEnded(this) && wechsel.spieler == 2 && wechsel.phase == 2 )
        {
            MouseInfo info = Greenfoot.getMouseInfo();
            setLocation(info.getX(), info.getY());
            wechsel.spieler = 1 ;
        }
    }
the wechsel.spieler and wechsel.phase are for the turns of the players. hope you can help me and if u need more to help me, just ask
danpost danpost

2017/4/6

#
First thing you need to do is save the location of the player before the mouse drag starts. This is necessary so that (1) you can determine if the end drag location is one square away and in a cross pattern and (2) you can place the player back at that starting position if the end drag location is not valid. See what you do with it. EDIT: Using 'getNeighbors' will check multiple cells. Better is to check only the cell the player was dragged to once it is found to be at a valid location (using 'getObjectsAtOffset').
You need to login to post a reply.