Tvick22 / platformer3x

Student repository for Trimester 3, Spring 2024. (Leaderboard Team)
https://tvick22.github.io/platformer3x/
Apache License 2.0
0 stars 0 forks source link

Fixes to Chat #7

Closed jellinki closed 6 months ago

jellinki commented 6 months ago

I changed the code so that the chatbox would refresh once you send a message. This is to improve efficiency so that the player doesn't have to delete their message to type a new one once they send one.

`        function onMessage(){
            // Remove the listener to prevent multiple listeners being added
            Multiplayer.removeListener("onMessage");
            // Add a new listener to handle incoming messages
            Multiplayer.createListener("onMessage",(data)=>{
                var message = this.parseMessage(data.message);
                addMessage(message, data.name ? data.name : data.id);
                this.soundArray.forEach((d)=>{
                    if (d[1]==true){ //sound can be played
                        d[0].play();
                        d[1]=false;
                        return;
                    }
                });
                var sound = createSound(this.soundSource);
                var arrayToAdd = [sound,true];
                this.soundArray.push(arrayToAdd);
                sound.addEventListener("ended",()=>{
                    arrayToAdd[1]=true;
                })
                sound.play();
            });

            var message = input.value;
            message = this.parseMessage(message);
            addMessage(message, "you");
            this.sendMessage(message);

            // Clear the input field after sending the message
            input.value = "";

            // Optionally, scroll the chat to the latest message
            div2.scrollTop = div2.scrollHeight;
        }`