Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hello have a game where you look for key cards. Which allow you to open certain doors. for example if u have a red key card you can open a red door. The problem is that when I don't have red key card I can still walk through the door. Here is my code so far:

Player class were collision happen:

  public void tick(block b[][]) {

    // movement
    for (int i = 0; i < b.length; i++) {
        for (int j = 0; j < b[0].length; j++) {

            if (b[i][j].getID() != 1 && b[i][j].getID() != 8 && b[i][j].getID() != 9 && b[i][j].getID() != 14 && b[i][j].getID() != 15 && b[i][j].getID() != 18 && b[i][j].getID() != 19 && b[i][j].getID() != 25&& b[i][j].getID() != 52 && b[i][j].getID() != 79 && b[i][j].getID() != 56&& (b[i][j].getID() != 80 && !Esentials.keycard7)&& b[i][j].getID() != 81) {

                // right
                if (collision.iscolliding(new Point((int) x + width + play.camx, (int) y + play.camy + 2), b[i][j]) || collision.iscolliding(new Point((int) x + width + play.camx, (int) y + height + play.camy - 1), b[i][j])) {
                    right = false;
                }
                // left
                if (collision.iscolliding(new Point((int) x + play.camx - 1, (int) y + play.camy + 2), b[i][j]) || collision.iscolliding(new Point((int) x + play.camx - 1, (int) y + height + play.camy - 1), b[i][j])) {
                    left = false;
                }
                // top
                if (collision.iscolliding(new Point((int) x + play.camx + 1, (int) y + play.camy), b[i][j]) || collision.iscolliding(new Point((int) x + width + play.camx - 2, (int) y + play.camy - 1), b[i][j])) {
                    up = false;
                }

                // bottom
                if (collision.iscolliding(new Point((int) x + play.camx + 2, (int) y + height + play.camy + 1), b[i][j]) || collision.iscolliding(new Point((int) x + width + play.camx - 2, (int) y + height + play.camy + 1), b[i][j])) {

                    down = false;
                }

            }
        }
    }

    r = new Rectangle((int) x, (int) y, width, height);

    // movement
    if (right) {
        play.camx += 1;

    }
    if (left) {
        play.camx -= 1;
    }
    if (up) {
        play.camy -= 1;
    }
    if (down) {
        play.camy += 1;

    }

}

The Captain Key card class:

   public class captaindoor {

public int x, y;
public boolean open = false;
public Rectangle r, r1;

public boolean canopen = false;
public boolean msg = false;

public captaindoor(int x, int y) {

    this.x = x;
    this.y = y;

    r = new Rectangle(x - play.camx, y - play.camy, 20, 20);
    r1 = new Rectangle(x - play.camx, y - play.camy, 20, 20);

}

public void tick() {
    r = new Rectangle(x - play.camx, y - play.camy, 20, 20);
    r1 = new Rectangle(x - play.camx, y-5 - play.camy, 20, 30);


    if(Esentials.keycard2){
        canopen = true;
    }

    if(r1.intersects(play.p.r)){
        if(!canopen){
            msg = true;
        }
    }else{
        msg = false;
    }

    if (r.intersects(play.p.r) && Esentials.keycard7) {
        if (!open && canopen) {
            open = true;
            Sound.snap.play();

        }
    } else {
        open = false;
    }

}

public void render(Graphics g) {
    Image img;
    if(!open && msg){
        ImageIcon i162 = new ImageIcon("res/gui/openstuff.png");
        img = i162.getImage();
        g.drawImage(img, Comp.size.width /2 +20- 125, 50, null);

        g.setColor(Color.cyan);
        g.setFont(new Font("italic", Font.BOLD,10 ));
        g.drawString("Captain Keycard Required ", Comp.size.width /2 - 62, 87);

    }else{}


    if (open) {
        ImageIcon i62 = new ImageIcon("res/tiles/captaindooropen.png");
        img = i62.getImage();
        g.drawImage(img, x - play.camx, y - play.camy, null);

    } else {
        ImageIcon i62 = new ImageIcon("res/tiles/captaindoor.png");
        img = i62.getImage();
        g.drawImage(img, x - play.camx, y - play.camy, null);

    }

    if(play.debug){
        g.setColor(Color.red);
        g.drawRect(r.x, r.y, r.width, r.height);
        g.setColor(Color.yellow);
        g.drawRect(r1.x, r1.y, r1.width, r1.height);
    }else{

    }


}

}

and some of the block class that you might need:

      public void interactables() {
    if (id == 5) {
        screens.interactables.t1.add(new TableWithClue(x, y, play.nexttable));
        play.nexttable += 1;

    }
    if (id == 8) {
        screens.interactables.d.add(new door1(x, y));

    }
    if (id == 9) {
        screens.interactables.d2.add(new door2(x, y));

    }
    if (id == 4) {
        screens.interactables.c.add(new chest(x, y));

    }
    if (id == 14) {
        screens.interactables.d3.add(new walkTo(x, y));

    }
    if (id == 15) {
        screens.interactables.d4.add(new walkBack(x, y));

    }
    if (id == 27) {
        screens.interactables.d5.add(new walkTo2(x, y));

    }
    if (id == 28) {
        screens.interactables.d6.add(new walkTo3(x, y));

    }
    if (id == 29) {
        screens.interactables.bd7.add(new walkBack2(x, y));

    }
    if (id == 30) {
        screens.interactables.bd8.add(new walkBack3(x, y));

    }
    if (id == 79) {
        screens.interactables.sd.add(new staffdoor(x, y));

    }
    if (id == 80) {
        screens.interactables.cd.add(new captaindoor(x, y));

    }
    if (id == 81) {
        screens.interactables.secd.add(new securitydoor(x, y));

    }

}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.