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

2012/12/12

Maze Collisions Help PLEASE!!!

gamrman gamrman

2012/12/12

#
I'm making a maze game for a programing final that I'm doing and I need help with the collisions for the walls. I've tried getOneIntersectingObject but haven't gotten it to work. PLEASE HELP!!! I'm going to put the code up soon.
gamrman gamrman

2012/12/12

#
 public void collision()
    {
        Block theBlock = null;
        theBlock = (Block)getOneIntersectingObject(Block.class);
        if (theBlock != null)
        {
            if (Greenfoot.isKeyDown("right"))
            {
                setLocation(getX()+0, getY());
            }
            if (Greenfoot.isKeyDown("left"))
            {
                setLocation(getX()-0, getY());
            }
            if (Greenfoot.isKeyDown("up"))
            {
                setLocation(getX(), getY()-0);
            }
            if (Greenfoot.isKeyDown("down"))
            {
                setLocation(getX(), getY()+0);
            }
        }
    }
Here's the code.
danpost danpost

2012/12/12

#
What code have you tried and how is it not working? Please show the class with the code you tried.
gamrman gamrman

2012/12/12

#
The code is in the class player, which is the one that will be moving around in the maze.
gamrman gamrman

2012/12/12

#
[Disallowed URL] This is the maze
gamrman gamrman

2012/12/12

#
sorry, it wont let me upload the pic of the maze
danpost danpost

2012/12/12

#
Your 'setLocation' statement will not relocate (or move) your player if you change its location coordinates by zero.
Entity1037 Entity1037

2012/12/13

#
If your trying to stop it from going through walls, then the solution is easy. getOneObjectAtOffset( x, y, .class); This will detect objects at an EXACT point from the center of your object at what you put in the x and y. So if you want to detect colliding with a wall to your right, you would do: Actor rightCollide = getOneObjectAtOffset(40, 0, wall.class); if (rightcollide!=null){ if (Greenfoot.isKeyDown("right")){ xmove=0; } } ... setLocation(getX()+xmove, getY()+ymove); This will make your player not be able to move if there is a wall to his right (place this code after the code that makes the player move from pressing your keys or it will not work properly). You don't have to use the names I used, this is just an example, but make sure you have the setLocation at the very end of the movement code. Also it is possible to do: ... xmove=-5; ... setLocation(getX()+xmove, getY()+ymove); This will move it on the x by -5, however it may not look like it. If your game has big cells instead of each cell being a pixel, then just make that 40 a 1, and make your move 1 also. I hope this helps.
gamrman gamrman

2012/12/13

#
My world is just (800, 800, 1). How do you make the variables xmove and ymove to work, I'm not sure how to.
danpost danpost

2012/12/13

#
Just replace xmove and ymove with values representing the speed in the x and y directions.
ScratchMan ScratchMan

2012/12/14

#
If you want an object to bounce off of another object, you could use this code, edited from eating code! { Actor obstacle; obstacle = getOneObjectAtOffset(3, 3, obstacle.class); if(obstacle != null) { turn(90); } }
ddavid33 ddavid33

2012/12/16

#
@gamrman where u able to get it sorted?
ManiacalPenguin ManiacalPenguin

2012/12/30

#
make a variable to store the x and y values of the object that you don't want to go through the maze private int x; private int y; the method to make it stop at walls is simple public void stopAtWalls() { if (getOneIntersectingObject(Wall.class) != null) { setLocation(x,y); } else { x = getX(); y = getY(); } }
You need to login to post a reply.