0

I'm trying to code a really simple game using p5.play. In it, once a button is pressed, four sprites are expected to move off screen to display a separate screen. For some reason, when the button "You received a letter!" is pressed it shows the opened letter but the "Click to begin" button returns to the screen. I can't tell if its because I placed my variables in the wrong function or if its something within my if-statement loops.

Here is the code:

function setup() {
  createCanvas(600,400);
  startButton = new Sprite(width/2, height/2+100);
  letterClosed = new Sprite(letterClosedImg, -500, -600,600,600,"k")
  letterClosedImg.resize (130,90);
  letterOpen = new Sprite(letterOpenImg, -500, -600,300,200,"k")
  textBox = new Sprite(-800, -800);
  score = 1;

}

/* DRAW LOOP REPEATS */
function draw() {
  background(mainMenu);
  //Start Button
    stroke(0);
    startButton.pos = {x:width/2, y:height/2+100};
    startButton.w = 150;
    startButton.h = 50;
    startButton.collider = 'k';
    startButton.color = 'AntiqueWhite';
    textFont(gameFont);
    textSize(20);
   startButton.text = ('Click to Begin');
  textSize(12);

   
   
    textBox.w = 250;
    textBox.h = 50;
    textBox.collider = 'k';
    textBox.color = 'AntiqueWhite';
    stroke(0);
    textFont(gameFont);
   textBox.text = ('? You received a letter...');

  //Once user starts game nested loop
  if(startButton.mouse.presses()){
    showLetterScreen();
    startButton.pos = {x: -700, y: -700};
    textBox.pos = {x:width/2, y:height/2+100};
    score = score+1;
  }
  
  if (score == 2){
  if(textBox.mouse.presses()){
    textBox.pos = {x:-400, y:-800};
    startButton.pos = {x: -700, y: -700};
    displayLetterOpen();
    print("letter is open");
    letterClosed.pos = {x: -400, y: -800};
  }
  }
  
}

/* FUNCTIONS */

//letter appears on screen
function showLetterScreen(){
  print("this is the letter screen");
  background(mainMenu);
  letterClosed.pos={x: width/2, y: height/2}
  letterClosedImg.resize(120,90);
}


//Open the letter
function displayLetterOpen(){
  background(mainMenu);
    letterOpen.pos = {x: width/2, y: height/2};
    letterOpenImg.resize(100,50);
//there r brackets to end this block but i dont think its relevant

I tried to create a variable that value updates by 1 whenever a button is pressed. This prevented the click to begin button from displaying the you received a letter button an infinite amount of times but yet it still displays it.

0 Answers0