[SOLVED] ("Red Light, Green Light"): Switching between sound files and changing audio duration

Hi, I’m trying to make a game “Red Light, Green Light,” like Squid Game. I’m currently stuck on getting the audio file to switch between redlight.mp3 and greenlight.mp3, and also changing the duration of the audio each time it changes between red light and green light. Here is my project link: PlayCanvas 3D HTML5 Game Engine

and the file I’m working off of is GameManager.js
Thank you.

Hi @jaenanigans and welcome!

It looks like the event below is only fired once when the player start moving. Debug line ‘0’ and ‘2’ are executed. Debug line ‘1’ is never executed. I guess you want to fire the event again after some time, so the switch can do it’s job?

1 Like

Hi @Albertos,

Yes, that’s what I’m trying to do so that I can get the event to fire again after some time so the switch can do its job.

I’m not sure what the actual problem was for you since you already use everything you need in your project, but I decided to implant this for you. I use this_durationTime for it and made some minor changes.

var GameManager = pc.createScript('gameManager');

/** Attributes */

GameManager.attributes.add('playerMaxTime', { type: "number", default: 10 });
GameManager.attributes.add('mapBounds', { type: "vec3", default: [20, 0, 20] });

// //Adding new entity (Snorlax)
// GameManager.attributes.add('enemEntity', { type: "entity" });

/** Lifecycle Methods */

/**
 * Initialize - setup internal state, resources, listeners.
 */
GameManager.prototype.initialize = function() {
    this._round = false;
    this._playing = false;
    this._playerTime = 0;
    this._durationTime = 0;

    this._isWatching = true; 

    this._registerListeners('on');
    this.on('destroy', this._onDestroy, this);
};

/**
 * PostInitialize - setup stuff that requires all other scripts to already have been initialized.
 */
GameManager.prototype.postInitialize = function() {
    this._startRound();
};

/**
 * Destroy - clean up resources, listeners.
 */
GameManager.prototype._onDestroy = function() {
    this._registerListeners('off');
};

/**
 * Start a new round.
 */
GameManager.prototype._startRound = function() {
    // check if round is already in progress
    if (this._round) {
        return;
    }

    this._round = true;
    this._playing = false;
    this._playerTime = this.playerMaxTime;
    this.app.fire('roundStart');
};

/**
 * Update - called once per frame.
 *
 * @param {number} dt - time (in seconds) since last frame.
 */
GameManager.prototype.update = function(dt) {
    // when playing
    if (this._playing) {
        // check for game over condition
        this._playerTime -= dt;
        if (this._playerTime <= 0) {
            this.app.fire('roundOver');
            this._playing = false;
            this._round = false;
        }

        this._durationTime -= dt;
        if (this._durationTime <= 0) {
            this.app.fire("collected");
        }
    }
};

/** Event Handlers */

/**
 * Register this script's listeners which need to be cleared on entity destruction.
 *
 * @param {string} onOrOff - "on" for attaching the listeners; use "off" for detaching.
 */
GameManager.prototype._registerListeners = function(onOrOff) {
    this.app[onOrOff]('collected', this._onCollected, this); //app that starts
    this.app[onOrOff]('victory', this._onVictory, this);
    this.app[onOrOff]('ui:restartRound', this._startRound, this);
};

GameManager.prototype._onCollected = function(entity, bonus) {
    // if round is over, start a new round
    if (!this._round) {
        this._startRound();
    }

    this._durationTime = Math.random() * 3.5 + 2.5; //duration
    this._playing = true; // start timer

    this._isWatching = !this._isWatching; //switch between watching or not?

    if (this._isWatching) {
        this.entity.sound.play('redlight');
        this.entity.sound.stop('greenlight');
        if(this.app.keyboard.isPressed(pc.KEY_A) || this.app.keyboard.isPressed(pc.KEY_D) || this.app.keyboard.isPressed(pc.KEY_S) || this.app.keyboard.isPressed(pc.KEY_W) ){
            this.app.fire('roundOver');
            this._playing = false;
        }
    }

    if(!this._isWatching) {
        this.entity.sound.play('greenlight');
        this.entity.sound.stop('redlight');
    }

    const bonusAsNumber = bonus ? bonus : 0;
    this._playerTime = Math.min(this._playerTime + bonusAsNumber, this.playerMaxTime);
};

//Victory
GameManager.prototype._onVictory = function(entity, bonus) {
    // if round is over, start a new round
    if (!this._round) {
        this._startRound();
    }
    this._playing = false; // end timer
    this._round = false;

};

/** Public API */
GameManager.prototype.getPlayerTimePercentage = function() {
    return this._playerTime / this.playerMaxTime;
};

1 Like

@Albertos Thank you so much!

1 Like