如何实现网页Canvas内运行的Processing.js程序的声音播放?
开发现状
我使用Processing.js开发了一款规模较大的游戏(共计10000行代码),已通过加载对应库实现将程序嵌入网页运行,示例代码如下:
<!DOCTYPE html> <html> <head> <title>Processing.JS in Webpage</title> </head> <body> <canvas id="mycanvas"></canvas> </body> <script src="https://cdn.jsdelivr.net/processing.js/1.4.8/processing.min.js"></script> <script> var programCode = function(processingInstance) { with (processingInstance) { size(400, 400); background(100,200,0) frameRate(60); noStroke() fill(255, 255, 0); ellipse(200, 200, 200, 200); noFill(); stroke(0, 0, 0); strokeWeight(5); arc(200, 205, 150, 132, 0, PI); fill(0, 0, 0); ellipse(250, 180, 10, 10); ellipse(153, 180, 10, 10); mousePressed = function(){ background(255,0,0); //how to play a sound file here? }; }}; // Get the canvas that ProcessingJS will use var canvas = document.getElementById("mycanvas"); // Pass the function to ProcessingJS constructor var processingInstance = new Processing(canvas, programCode); </script> </html>
待实现功能需求
当前我的游戏在Canvas中运行的各项功能基本正常,但希望实现玩家触发射击动作时播放对应音效的需求。我希望了解是否可以根据Processing.js代码内部的运行状态触发声音播放:例如我在代码中设置了用于标识玩家射击状态的变量playerIsShooting,当该变量状态发生变化时,需要触发对应音效:
//inside processing.js game code if(playerIsShooting){ //play sound here with processing.js or tell something else to play sound outside of my script? }
内容的提问来源于stack exchange,提问作者Oyster_Bucket
相关产品推荐
相关产品推荐

