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

2015/11/15

getOneObjectAtOffset() query

davemib123 davemib123

2015/11/15

#
Hi All, I'm begining to setup the collisions for the game objects, so far I have this:
1
2
3
4
5
6
7
8
9
10
11
12
private void checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth / 2);
        Actor rightWall = getOneObjectAtOffset(xDistance, 0, SteppingTile.class);
        if(rightWall != null)
        {
            int wallWidth = rightWall.getImage().getWidth();
            int newX = rightWall.getX() - (wallWidth + getImage().getWidth()) / 2;
            setLocation(newX - shared.speed, getY());
        }
    }
How do I go about adding other classes into the method like:
1
Actor rightWall = getOneObjectAtOffset(xDistance, 0, Block.class);
and
1
Actor rightWall = getOneObjectAtOffset(xDistance, 0, Pipe.class);
I've tried to use an || operator but couldnt see how to add it in correctly. Thanks
danpost danpost

2015/11/15

#
After line 5 is executed, the value of 'rightWall' will either contain an Actor object or be 'null' (contain no Actor object). You could continue, or insert at line 6, the following:
1
2
if (rightWall == null) rightWall = getOneObjectAtOffset(xDistance, 0, Block.class);
if (rightWall == null) rightWall = getOneObjectAtOffset(xDistance, 0, Pipe.class);
davemib123 davemib123

2015/11/15

#
great! just what i wanted :)
You need to login to post a reply.