Ver Fonte

1、编程课声音模块更新音效可选
2、加入音效空判断

liyanbo há 5 meses atrás
pai
commit
2967b9feab

+ 15 - 4
src/api/blockly/blockly.js

@@ -322,7 +322,7 @@ const availableBlocks = {
   play_sound: {
     jsonConfig: {
       "type": "play_sound",
-      "message0": "%1 声音",
+      "message0": "%1 声音: %2",
       "args0": [
         {
           "type": "field_image",
@@ -330,12 +330,19 @@ const availableBlocks = {
           "width": 20,
           "height": 20,
           "alt": "声音"
+        },
+        {
+          "type": "field_dropdown",
+          "name": "SOUND",
+          "options": [
+            ["打招呼", "打招呼"],
+          ]
         }
       ],
       "previousStatement": null,
       "nextStatement": null,
       "colour": 160,
-      "tooltip": "自动触发特效提示音,并根据当前方格类型执行相应操作",
+      "tooltip": "播放指定的声音特效",
       "helpUrl": ""
     },
     isGeneral: false
@@ -407,7 +414,9 @@ const availableGenerators = {
     return `await pause(${seconds});\n`;
   },
   play_sound: function(block) {
-    return 'await playSound();\n';
+    const sound = block.getFieldValue('SOUND');
+    return `await playSound('${sound}');
+`;
   },
   construct: function(block) {
     return 'await construct();\n';
@@ -458,7 +467,9 @@ const availablePythonGenerators = {
     return `pause(${seconds})\n`;
   },
   play_sound: function(block) {
-    return 'play_sound()\n';
+    const sound = block.getFieldValue('SOUND');
+    return `play_sound('${sound}')
+`;
   },
   construct: function(block) {
     return 'construct()\n';

+ 4 - 0
src/api/blockly/music.js

@@ -96,6 +96,10 @@ export const onMusicEnded = (state) => {
 
 // 播放mp3文件
 export const playMp3 = async function(audioUrl) {
+    if (!audioUrl) {
+        console.error('音频URL为空');
+        return;
+    }
     try {
         // 创建音频对象,播放声音特效
         const soundEffect = new Audio(audioUrl);

+ 11 - 4
src/components/blockly/MapGame.vue

@@ -300,6 +300,11 @@ const CONFIG = {
   }
 };
 
+//可播放的mp3类型(需要同步修改blockly.js)
+const BLOCKLY_PLAY_MP3_TYPE_DICT = {
+  "打招呼": passMp3,
+}
+
 // 路由和游戏状态
 const currentGameData = ref(null);
 const playerInitialDirection = ref(0); // 人物初始朝向
@@ -509,6 +514,7 @@ onMounted(async () => {
 });
 
 //================初始化=====================
+
 // 动态生成工具箱XML
 function generateToolboxXml() {
   let toolboxXml = `
@@ -1318,19 +1324,20 @@ window.pause = async function(seconds) {
 }
 
 // 声音函数
-window.playSound = async function() {
+window.playSound = async function(mp3FileName) {
   if (shouldStopExecution || isColliding.value || isSliding.value) {
     return;
   }
 
+  // 播放声音
   if(processingSpecialTasksDisappearing()){
     //延迟,确保声音播放完成
-    await playMp3(passMp3);
+    await playMp3(BLOCKLY_PLAY_MP3_TYPE_DICT[mp3FileName]);
     await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.PALY_MP3_TIMES));
     return
   }
 
-  //延迟,确保声音播放完成
+  //延迟,确保声音播放完成(错误)
   await playMp3(failureMp3);
   await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.PALY_MP3_TIMES));
 };
@@ -2465,7 +2472,7 @@ onUnmounted(() => {
   cursor: pointer;
   transition: all 0.3s ease;
   // 防止文字换行
-  white-space: nowrap; 
+  white-space: nowrap;
   // 超出部分省略号显示
   overflow: hidden;
   // 超出部分省略号显示