Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to import a p5js file into another file?

I was wondering if it is possible to important a p5js file into another file. I am creating a non linear adventure game. If the player presses on a certain button, I want that button to activate a mini game that I have in another p5js file.

function mouseReleased() {
  cursor(ARROW);

  if (scene0 == true) {
    if ((mouseY > 162) && (mouseY < 248) && (mouseX > 54) && (mouseX < 278)){
      scene0 = false;
      scene1= true;
    }
  }
    if (scene1 == true) {
    if ((mouseY > 408) && (mouseY < 482) && (mouseX > 52) && (mouseX < 222)){
      scene1 = false;
      miniGame1 = true;
    }
  }

Thank you.

like image 250
metrocode Avatar asked Oct 17 '22 09:10

metrocode


1 Answers

The short answer is yes it's possible. In fact it's the default behavior.

Let's say you have this HTML content:

<!DOCTYPE html>
<html>
    <head>
        <script src="mini-game.js"></script>
        <script src="main-game.js"></script>
    </head>
    <body>
      <p>My HTML</p>
    </body>
</html>

Here is mini-game.js:

function miniGame(){
  console.log('mini game');
}

And here is main-game.js:

function mainGame(){
  miniGame();
}

Notice that the code in main-game.js can run the miniGame() function defined by the mini-game.js file.

You might hit a snag if you are defining P5.js functions in both functions. If this is the case, you'll have to refactor your code to use instance mode or to take in the sketch as an argument.

like image 58
Kevin Workman Avatar answered Nov 15 '22 06:11

Kevin Workman