How can I make my code more efficent when it comes to implementing sound files

edited May 16 in Library Questions

as i have this set up as a tree and i want it to look better and work more effiecently than it already does So if you could help with my tree that would be greatly appricated

`import processing.sound.*;

import g4p_controls.*;

import java.util.ArrayList;
import java.util.List;
PImage menu;
SoundFile Harrible;
SoundFile cremeyboy;
SoundFile bianca;
SoundFile C;
SoundFile Excuse;
PImage[] scenes = new PImage[10];
boolean startscreen = true;
boolean startmusic = true;
Node root;
Node currentScene;

void setup() {

  size(1280, 720);
  menu = loadImage("menu.jpg");

  Excuse = new SoundFile(this,"noExcuse.wav");
  C = new SoundFile(this, "Condragulation.wav");
  Harrible = new SoundFile(this, "Harrible.mp3");
  cremeyboy = new SoundFile(this, "cremey boy.wav");
  bianca = new SoundFile(this, "bianca.wav");
  root = new Node("scene1.png", 1,"choose your queen you have the choice from bianca and dela");
  currentScene = root;

  root.left = new Node("scene2.png", 2,"You chose bianca now go to the work room");
  root.right = new Node("scene3.png", 3, "you chose bendelacreme now go to the work room to see to choose you couture (you can go either left or right) ");
  Node iterator;
  iterator = root.left;

  iterator.left = new Node("elganzabianca.png", 69,"Choose Left or Right for which couture you want dependent on what couture you choose will determine the reception you get");
  iterator.right = new Node("elganzabianca.png", 69,"Choose Left or Right for which couture you want dependent on what couture you choose will determine the reception you get");
  iterator = root.right;

  iterator.left = new Node("scene7.png", 1111,"yeet");
  iterator.right = new Node("scene7.png", 9999,"yeet");
  iterator = root.left.left;

  iterator.left = new Node("EndScene1.png", 10,"You have to sashay away");
  iterator.right = new Node("EndScene1.png", 8,"You won the judges over with your couture");
  iterator = root.left.right;

 iterator.left = new Node("EndScene1.png", 8,"Not all the judges agree but you do live another week but you won the challenge");
  iterator.right = new Node("EndScene1.png", 6,"Thats terrible nobody liked your couture and for that you sashay away");
  iterator = root.right.right;

iterator.left = new Node("EndScene1.png", 5,"You argued with ru now you sashay away");
  iterator.right = new Node("EndScene1.png", 8,"Great Job you live to see another");
  iterator = root.right.left;

  iterator.left = new Node("EndScene1.png", 6,"Thats Horrible ");
  iterator.right = new Node("EndScene1.png", 8,"You Won the Challenge you can now leave the stage a get ready for another week");
  iterator = root.left.left.right;

   iterator.left = new Node("Finscreen.png", 8,"yeet");
  iterator.right = new Node("Finscreen.png", 9,"yeet");
  iterator = root.left.right.left;

  iterator.left = new Node("Finscene.png", 10,"Sorry Dela No one liked your couture so you haved to sashay away");
  iterator.right = new Node("Finscene.png", 11,"Do the stanky leg b xoxoxo :^) you won now you can sleep ");
  iterator = root.right.right.left;

void draw() {
  if (startscreen) {

  } else {
    startmusic = false;
    image(currentScene.scene, 0, 0);

  text(currentScene.Dialog, 500, 100);

//these if statements play the audio and stop it after it has has played

  if (currentScene.index == 1) {
    currentScene.index =-1;

  if (currentScene.index ==-1){

  if (currentScene.index == 3) {;
    currentScene.index =-3;

  if (currentScene.index ==-3){


  if (currentScene.index == 2) {;
    currentScene.index =-2;

  if (currentScene.index ==-2){


    if (currentScene.index == 6) {;
    currentScene.index =-6;

  if (currentScene.index ==-6){

      if (currentScene.index == 7) {;
    currentScene.index =-7;

  if (currentScene.index ==-7){


     if (currentScene.index == 5) {;
    currentScene.index =-5;

  if (currentScene.index ==-5){

    if (currentScene.index == 8) {;
    currentScene.index =-8;

  if (currentScene.index ==-8){


     if (currentScene.index == 10) {;
    currentScene.index =-10;

  if (currentScene.index ==-10){

  println(255, currentScene.index);

void keyPressed() {

  if (keyCode == RIGHT) {

    currentScene = currentScene.right;

    if (keyCode == LEFT){

      currentScene = currentScene.left;



interface Display{
 void display(); 

public class Node implements Display{
  void display(){


 PImage scene;
 int index;
 Node left;
 Node right;
  String Dialog;
 Node (String filename,int i, String DialogInput){ 
   scene = loadImage(filename);
   index = i;
   Dialog = DialogInput;




/* =========================================================
 * ====                   WARNING                        ===
 * =========================================================
 * The code in this tab has been generated from the GUI form
 * designer and care should be taken when editing this file.
 * Only add/edit code inside the event handlers i.e. only
 * use lines between the matching comment tags. e.g.

 void myBtnEvents(GButton button) { //_CODE_:button1:12356:
     // It is safe to enter your event code here  
 } //_CODE_:button1:12356:

 * Do not rename this tab!
 * =========================================================

public void button1_click1(GButton source, GEvent event) { //_CODE_:button1:446807:
  println("button1 - GButton >> GEvent." + event + " @ " + millis());
  startscreen = false;
} //_CODE_:button1:446807:

public void button2_click1(GButton source, GEvent event) { //_CODE_:button2:341443:
  println("button2 - GButton >> GEvent." + event + " @ " + millis());
} //_CODE_:button2:341443:

// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI(){
  surface.setTitle("Sketch Window");
  button1 = new GButton(this, 116, 511, 127, 43);
  button1.addEventHandler(this, "button1_click1");
  button2 = new GButton(this, 116, 558, 128, 52);
  button2.addEventHandler(this, "button2_click1");

// Variable declarations 
// autogenerated do not edit
GButton button1; 
GButton button2; `


  • Remarks

    • setup() is very long so I advice to make a sub function initTree()

    • Node root; is only used in function initTree()- no need to make it global

    • remember: ctrl-t for auto-format in processing IDE

    • This: PImage[] scenes = new PImage[10]; is not in use

    Line image(currentScene.scene, 0, 0);

    Make this is a function in the class Node:

    void displayScene() {
          image(scene, 0, 0);

    and call it from draw() as currentScene.displayScene();

    Your sounds

    When each scene has a sound make the sound file part of the class Node scene

    SoundFile soundForTheScene;

    then instead of; in draw() say; in the function displayScene() within the class! Nice.

    function in the class Node is now:

    void displayScene() {
          image(scene, 0, 0);

    Best, Chrisir ;-)

  • thank you

  • edited May 16

    Lines 88-164 should be gone then

    Not sure what this is

         currentScene.index =-8;

    minus is just that the sound is playing? Then use a boolean soundPlays in the class

  • in these lines:

    iterator.left = new Node("elganzabianca.png", 69,"Choose Left or Right for which couture

    you should also give the sound file name as parameter and load the sound in the constructor as you do with the image


    line 36 to 70:

    also you could have text file containing all data (scene number, image name, sound file name, scene text, child node numbers...) and read the text file and build the tree from there.

Sign In or Register to comment.