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

2013/4/20

Maze help

1
2
3
Gingervitis Gingervitis

2013/4/21

#
Everything is still a wacky. The ball can still go through the walls and the movements are still a little off.... I never thought a simple maze game would cause so many problems.
danpost danpost

2013/4/22

#
There may be more to it than what you are showing us. Maybe we should look at the bigger picture.
Gingervitis Gingervitis

2013/4/22

#
Would you like me to publish the scenario?
danpost danpost

2013/4/22

#
That is really up to you; however, it would be the best way for someone to find out what is going on and letting you know how to fix it.
Gingervitis Gingervitis

2013/4/22

#
I published the code
danpost danpost

2013/4/22

#
I got a copy of it. Will look at it shortly.
danpost danpost

2013/4/22

#
OK, I see two problems. The first is my fault in that, in the 'hWall' check all references to 'X' should be 'Y'. The other is with where two bricks (one vertical and one horizontal) merge to make a corner. We will need to make sure we are hitting on the long side of the VWall and HWall objects. Will return with some code in a little bit.
Gingervitis Gingervitis

2013/4/22

#
Ok.
danpost danpost

2013/4/22

#
Do you really want to change its direction when it hits a wall? It might be easier to just let the user turn the actor away from the walls and just program it not to go throught the walls. For this, the code would simply be:
public void act()  
{  
    checkTurns();  
    move();  
}  

private void checkTurns()  
{  
    if (Greenfoot.isKeyDown("left")) turn(-5);  
    if (Greenfoot.isKeyDown("right")) turn(5);  
}  

public void move()  
{  
    int d = 0;  
    if (Greenfoot.isKeyDown("up")) d++;  
    if (Greenfoot.isKeyDown("down")) d--;  
    move(3*d); 
    if (!getIntersectingObjects(VWall.class).isEmpty() ||
        !getIntersectingObjects(HWall.class).isEmpty()) move(-3*d);
}  
It would be good to give the image of the actor something that would indicate which way it would move if the user was to move it.
Gingervitis Gingervitis

2013/4/22

#
Is there a way to make it so when you press 'left' or 'right' it will move in that direction instead of just turning? That way there won't be a need for a different image.
danpost danpost

2013/4/22

#
Will update soon.
danpost danpost

2013/4/23

#
This is what I came up with:
public void act()  
{  
    move();
}

public void move()
{
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("up")) dy--;  
    if (Greenfoot.isKeyDown("down")) dy++;
    if (Greenfoot.isKeyDown("left")) dx--;
    if (Greenfoot.isKeyDown("right")) dx++;
    setLocation(getX()+3*dx, getY()+3*dy);
    if (getIntersectingObjects(VWall.class).isEmpty() &&
        getIntersectingObjects(HWall.class).isEmpty()) return;
    setLocation(getX()-3*dx, getY()-3*dy);
}
Gingervitis Gingervitis

2013/4/23

#
I just tried out the new code. It is a whole better than before. It is actually amazing. Right now when you are close to the wall you get stuck and you can only move in the center of the maze. Is there a way to move even when it hits a wall?
danpost danpost

2013/4/23

#
We could check for a wall after each part of the move (horizontal, then vertical; or, vertical, then horizonal).
public void move()
{
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("up")) dy--;
    if (Greenfoot.isKeyDown("down")) dy++;
    if (Greenfoot.isKeyDown("left")) dx--;
    if (Greenfoot.isKeyDown("right")) dx++;
    if (dx != 0)
    {
        setLocation(getX()+dx, getY());
        if (!getIntersectingObjects(VWall.class).isEmpty() ||
            !getIntersectingObjects(HWall.class).isEmpty())
                setLocation(getX()-dx, getY());
    }
    if (dy != 0)
    {
        setLocation(getX(), getY()+dy));
        if (!getIntersectingObjects(VWall.class).isEmpty() ||
            !getIntersectingObjects(HWall.class).isEmpty())
                setLocation(getX(), getY()-dy);
    }
}
Gingervitis Gingervitis

2013/4/23

#
It seems that when I replaced the old code with the new code, it made the ball move slower and it still got caught on the wall.
There are more replies on the next page.
1
2
3