I try to create a self-defined Action. The action is to play a sound from an array. Even though I managed to make it sound, I received the error saying TypeError: n.apply(...) is undefined
.
The Action is called with a syntax like so:
playoneshot bird
or playoneshot lion
When it is defined in the script, the idea is so that the Action play a short sound that is chosen randomly from an array of sounds. There are two arrays: one array for bird sounds, one for lion sounds. The sounds are saved into the music asset folder, and defined as assets in the script:
monogatari.assets("music", {
"b01": "bird01.mp3",
"b02": "sound02.mp3",
"l01": "lion01.mp3",
"l02": "lion02.mp3"
})
Then I defined the Action in the main.js
inside the init function like the following:
class Playoneshot extends Monogatari.Action {
static matchString([action]) {
return action === "playoneshot";
}
constructor([myaction, soundType]){
super();
this.soundType = soundType;
}
apply() {
let soundArr;
switch (this.soundType) {
case "bird":
soundArr = ["b01", "b02"];
break;
case "lion":
soundArr = ["l01", "l02"];
break;
}
// Play random one shot
let idx = Math.floor(Math.random() * soundArr.length);
let sound = soundArr[idx];
monogatari.run("play music " + sound);
}
revert() {
// nothing
}
}
Playoneshot.id = "Playoneshot";
monogatari.registerAction(Playoneshot);
Could anyone help me to understand the error message? I thank you in advance!