Screen turning white or clearing up when clicking a certain object

edited November 2014 in Questions about Code

Hi there, I am a newb at Processing and recently I've been trying to create a Text Adventure Game.

I have only created one screen and the aim is to click on the door and have the screen turn white (ie. a new screen appears which I intend to work on later).

The problem is when the user clicks on any part of the screen, the screen turns white. I only want it to turn white when the DOOR is clicked. I have tried editing my code many times using different mouse functions etc. but to no avail...

Here is my code:

PImage blood;
int value = 158; // r
int value2 = 138; // g
int value3 = 79; // b

void setup()
{
  background(0);
  size(600, 600);
  blood = loadImage("BloodSpot.png");
  smooth();

  display(); // Everything on screen
} // end setup

void draw()
{
} // end draw

void mouseClicked()
{
  // Clearing the screen when door is clicked
  fill(255);
  rect(0, 0, 600, 600);

  // when clicked, door changes to white
  value = 255;
  value2 = 255;
  value3 = 255;
} // end mousePressed

void display()
{
  // Screen
  fill(255, 0, 0); // red text
  textSize(20);
  text("Hello. How are you?", 66, 66); 
  text("Ah you must be here for that!", 88, 88);
  text("Please follow me inside.", 109, 109);
  text("Mind the step though... ", 133, 133);
  //image(blood, 300, 200);

  // Door 
  stroke(0); // outline
  noFill();
  fill(value, value2, value3);
  rect(400, 250, 180, 400);

  // Door knob
  noStroke();
  noFill();
  fill(145, 15, 15);
  ellipse(545, 420, 15, 15);

  // Clicking the door
  /* if(mousePressed == true)
   {
   value = 255;
   value2 = 255;
   value3 = 255;
   }
   else
   {
   value = 158;
   value2 = 138;
   value3 = 79;
   }
   }*/
} // end display

Many thanks for the help! Apologies if this is the wrong section.

Answers

  • _vk_vk
    edited November 2014

    You need to check mouse coordinates when clicked and just do something if they are inside door's coordinates. Much like a button. Check the button example packed with processing " File > Examples > Topics > GUI > Button ". Ask if still in doubt :)

    Check the code in pointed example, but the basic idea is :

    • is mouse x position > object 'start' in x

    • is mouse x position < object 'start' in x' + object 'end' in x

    • is mouse y position > object 'start' in y

    • is mouse y position < object 'start' in y + object 'end' in y

    • if all the above do change your screen.

Sign In or Register to comment.