javascript - 我正在使用纸质 js 进行 patatap 克隆,但一直出错
问题描述
我正在上 udemy 上一门课程,课程名称是 colt Steele 的 web developer bootcamp,在第 239 课中,当我添加对象“keyData”时,我的控制台中显示一个错误。我的代码是这样的。`
<!DOCTYPE html>
<html>
<head>
<title>Circles</title>
<script type="text/javascript" src="paper-full.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.3/howler.js"></script>
<link rel="stylesheet" type="text/css" href="circles.css">
<script type="text/paperscript" canvas="myCanvas">
var keyData = {
a: {
color: "purple",
sound: new Howl({
src: ['sounds/bubbles.mp3']
});
},
s: {
color: "green",
sound: new Howl({
src: ['sounds/clay.mp3']
});
},
d: {
color: "yellow",
sound: new Howl({
src: ['sounds/confetti.mp3']
});
}
}
var circles = [];
function onKeyDown(event){
if(keyData[event.key]){
var maxPoint = new Point(view.size.width, view.size.height);
var randomPoint = Point.random();
var point = maxPoint * randomPoint;
var newCircle = new Path.Circle(point, 500);
newCircle.fillColor = keyData[event.key].color;
keyData[event.key].sound.play();
circles.push(newCircle);
}
}
function onFrame(event){
for(var i = 0; i < circles.length; i++){
circles[i].scale(.9);
circles[i].fillColor.hue += 1;
}
}
<!-- alert(view.size); this will give an alert showing the max width and max height of the screen-->
<!-- alert(view.center); this will give an alert showing the center of the screen-->
</script>
</head>
<body>
<canvas id="myCanvas" resize></canvas>
</body>
</html>
该错误仅在键入对象“keyData”时出现。这是错误。`
paper-full.js:15726 Uncaught SyntaxError: Unexpected token (8:6)
at raise (paper-full.js:15726)
at unexpected (paper-full.js:16366)
at expect (paper-full.js:16362)
at parseObj (paper-full.js:16829)
at parseExprAtom (paper-full.js:16799)
at parseExprSubscripts (paper-full.js:16730)
at parseMaybeUnary (paper-full.js:16716)
at parseExprOps (paper-full.js:16682)
at parseMaybeConditional (paper-full.js:16669)
at parseMaybeAssign (paper-full.js:16655)
` 仍然存在的主要问题是,当我键入对象时出现错误。请帮助。谢谢你。
解决方案
您的代码中有额外;
的内容。keyData
这是更正后的代码。
<!DOCTYPE html>
<html>
<head>
<title>Circles</title>
<script type="text/javascript" src="paper-full.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.3/howler.js"></script>
<link rel="stylesheet" type="text/css" href="circles.css">
<script type="text/paperscript" canvas="myCanvas">
var keyData = {
a: {
color: 'purple',
sound: new Howl({
src: ['sounds/bubbles.mp3']
})
},
s: {
color: 'green',
sound: new Howl({
src: ['sounds/clay.mp3']
})
},
d: {
color: 'yellow',
sound: new Howl({
src: ['sounds/confetti.mp3']
})
}
};
var circles = [];
function onKeyDown(event) {
if (keyData[event.key]) {
var maxPoint = new Point(view.size.width, view.size.height);
var randomPoint = Point.random();
var point = maxPoint * randomPoint;
var newCircle = new Path.Circle(point, 500);
newCircle.fillColor = keyData[event.key].color;
keyData[event.key].sound.play();
circles.push(newCircle);
}
}
function onFrame(event) {
for (var i = 0; i < circles.length; i++) {
circles[i].scale(.9);
circles[i].fillColor.hue += 1;
}
}
<!-- alert(view.size); this will give an alert showing the max width and max height of the screen-->
<!-- alert(view.center); this will give an alert showing the center of the screen-->
</script>
</head>
<body>
<canvas id="myCanvas" resize></canvas>
</body>
</html>
推荐阅读
- tokbox - 开始连接后打开工具订阅者更改镜像属性
- bash - 在 bash 脚本中获取第一个未注释的行(即不以 # 开头)
- cryptography - 将 MetaMask 连接到币安链
- windows - EACCES:Windows Next js 中的权限被拒绝 0.0.0.0:3000
- omnet++ - 导入和运行 INET 框架的错误和警告
- python - 将列表从一个函数返回到另一个函数
- javascript - 我想制作一个标签来同时切换我的 4 个 iframe 内容
- .htaccess - 使用 .htaccess 重定向 URL
- android - 按返回后重新加载图像
- reactjs - 仅在生产中反应无效令牌指定错误