我正在玩一个使用HTML / jQuery / Javascript制作的简单游戏,并决定将其移动到节点,以便可以将其连接到mongo db来捕获排行榜的首字母和得分。我是Node的新手,想看看我是否可以挑战一下。这是对1980年代Simon游戏的简单复制。感谢您可以提供的任何指导
现在,我已经将前端移动到EJS并正在使用节点,除了我用来给按钮点击动画和播放声音的jQuery代码外,我能够使所有工作正常。尝试在app.js文件中使用jQuery和Javascript代码时,出现“未定义”错误。
我尝试寻找可以播放音频,创建虚拟dom甚至为按钮设置动画的npm软件包,但是我无法使任何npm软件包正常工作,我想我只是想念了很大一部分节点拱的工作原理。我还把我正在做的一些代码移到ejs前端文件中,只是将其作为纯正的javascript嵌入,但这似乎是一种hack,仅适用于游戏的某些部分。我也不清楚何时将我的js放在服务器端vs客户端上。
//这是单击按钮后运行的我的app.post。我检查游戏是否处于启动状态,如果是,则确定单击了哪个按钮,然后尝试调用playSound和animatePress函数。
app.post("/game", function(req, res) {
if (gameStarted === true) {
var userChosenColor = req.body.button;
playSound(userChosenColor);
animatePress(userChosenColor);
}
});
///当我单独使用html / js时,此功能运行良好。我只是根据单击的按钮调用适当的mp3。当我移到节点时,它得到以下错误。
function playSound(name) {
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}
///此功能在html / js中工作正常。按下的类将背景更改为白色并添加阴影,然后迅速将其移除以动画化按下时的按钮动画
function animatePress(currentColor) {
$("#" + currentColor).addClass("pressed");
setTimeout(function() {
$("#" + currentColor).removeClass("pressed");
}, 100);
}
///当我调用playSound()函数时,出现以下错误 ReferenceError:音频未定义 在playSound(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ app.js:184:15) 在C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ app.js:91:5 在Layer.handle上[作为handle_request](C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在下一个(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ route.js:137:13) 在Route.dispatch(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ route.js:112:3) 在Layer.handle上[作为handle_request](C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:281:22 在Function.process_params(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:275:10) 在serveStatic(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ serve-static \ index.js:75:16) 在Layer.handle上[作为handle_request](C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在trim_prefix(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:317:13) 在C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:284:7 在Function.process_params(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:275:10) 在C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ body-parser \ lib \ read.js:130:5
///当我调用animatePress()函数时,出现以下错误 ReferenceError:未定义$ 在animatePress(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ app.js:189:3) 在C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ app.js:92:5 在Layer.handle上[作为handle_request](C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在下一个(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ route.js:137:13) 在Route.dispatch(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ route.js:112:3) 在Layer.handle上[作为handle_request](C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:281:22 在Function.process_params(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:275:10) 在serveStatic(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ serve-static \ index.js:75:16) 在Layer.handle上[作为handle_request](C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在trim_prefix(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:317:13) 在C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:284:7 在Function.process_params(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ express \ lib \ router \ index.js:275:10) 在C:\ Users \ Clint Tuttle \ Box Sync \ Software Development \ Full Stack Udemy Class \ Simon Game v2 \ node_modules \ body-parser \ lib \ read.js:130:5