How do I fix the error: "No debugger available, can not send 'variables'" and the error: "height is not defined at …(methods)"?

huangapple go评论66阅读模式
英文:

How do I fix the error: "No debugger available, can not send 'variables'" and the error: "height is not defined at ...(methods)"?

问题

I understand that you need assistance with the issues you're facing while working on a fighting game project in HTML/JS. Here are the translated portions of your text:

错误信息1:

Sprite {position: {…}, velocity: {…}, height: 150}
index.js:58
No debugger available, can not send 'variables'

错误信息2:

Uncaught ReferenceError ReferenceError: height is not defined
    at draw (...\Fighting game\index.js:18:58)
    at update (...\Fighting game\index.js:22:14)
    at animate (...\Fighting game\index.js:64:12)
    at <anonymous> (...\Fighting game\index.js:68:1)
--- requestAnimationFrame ---
    at animate (...\Fighting game\index.js:61:12)
    at <anonymous> (...\Fighting game\index.js:68:1)

此处是您的launch.json文件:

{
    // 使用 IntelliSense 了解可能的属性。
    // 悬停以查看现有属性的描述。
    // 获取更多信息,请访问:https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:5500",
            "webRoot": "${workspaceFolder}"
        }
    ]
}

此处是index.html文件:

<html>
  <head></head>
  <body>
    <canvas></canvas>
    <script src="index.js"></script>
  </body>
</html>

最后是您的index.js文件:

const canvas = document.querySelector('canvas'); // 使用具有标签 'canvas' 的元素
const c = canvas.getContext('2d'); // 游戏的上下文

canvas.width = 1024;
canvas.height = 576;

c.fillRect(0, 0, canvas.width, canvas.height); // x, y, w, h

class Sprite {
    constructor({position, velocity}){
        this.position = position;  // 这个 'position' 将是一个对象
        this.velocity = velocity;
        this.height = 150;
    }

    draw(){ // 绘制精灵
        c.fillStyle = 'red';
        c.fillRect(this.position.x, this.position.y, 50, this.height);
    }

    update(){
        this.draw();
        this.position.y += this.velocity.y;

        if(this.position.y + this.height + this.velocity.y >= canvas.height){
            this.velocity.y = 0;
            //this.position.y = canvas.height - this.height;
        }
    }
}

const player = new Sprite( // 创建玩家对象并将对象 'position' 插入其中
    {
        position: {
            x: 0,
            y: 0
        },
        velocity: {
            x: 0,
            y: 10
        }
    }
);

const enemy = new Sprite( // 创建敌人对象并将对象 'position' 插入其中
    {
        position: {
            x: 400,
            y: 100
        },
        velocity: {
            x: 0,
            y: 0
        }
    }
);

console.log(player);

function animate(){
    window.requestAnimationFrame(animate); // 通过不断调用函数 'animate' 来创建一个无限循环
    c.fillStyle = 'black';
    c.fillRect(0, 0, canvas.width, canvas.height);
    player.update();
    enemy.update();
}

animate();

如果您需要更多帮助或解决特定问题,请随时提出。

英文:

I have been trying to go through with the video by Chris Courses on making a fighting game with html/js (https://youtu.be/vyqbNFMDRGQ), but I have been getting these errors:

Sprite {position: {…}, velocity: {…}, height: 150}
index.js:58
No debugger available, can not send &#39;variables&#39;

and this:

Uncaught ReferenceError ReferenceError: height is not defined
    at draw (...\Fighting game\index.js:18:58)
    at update (...\Fighting game\index.js:22:14)
    at animate (...\Fighting game\index.js:64:12)
    at &lt;anonymous&gt; (...\Fighting game\index.js:68:1)
--- requestAnimationFrame ---
    at animate (...\Fighting game\index.js:61:12)
    at &lt;anonymous&gt; (...\Fighting game\index.js:68:1)

this is my launch.json file:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
&quot;version&quot;: &quot;0.2.0&quot;,
&quot;configurations&quot;: [
{
&quot;type&quot;: &quot;chrome&quot;,
&quot;request&quot;: &quot;launch&quot;,
&quot;name&quot;: &quot;Launch Chrome against localhost&quot;,
&quot;url&quot;: &quot;http://localhost:5500&quot;,
&quot;webRoot&quot;: &quot;${workspaceFolder}&quot;
}
]
}

btw, I have live server extension (just so you have an idea about the tools available and in use)

this is the index.html file:

&lt;html&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;
&lt;canvas&gt;&lt;/canvas&gt;
&lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

and finally my index.js file:

const canvas = document.querySelector(&#39;canvas&#39;); // use the element with the tametag &#39;canvas&#39;
const c = canvas.getContext(&#39;2d&#39;); // context of the game
canvas.width = 1024;
canvas.height = 576;
c.fillRect(0, 0, canvas.width, canvas.height); //x, y, w, h
class Sprite {
constructor({position, velocity}){
this.position = position;  // this &#39;position&#39; will be an object
this.velocity = velocity;
this.height = 150;
}
draw(){ // to draw the sprite
c.fillStyle = &#39;red&#39;;
c.fillRect(this.position.x, this.position.y, 50, height);
}
update(){
this.draw();
this.position.y += this.velocity.y;
if(this.position.y + this.height + this.velocity &gt;= canvas.height){
this.velocity.y = 0;
//this.position.y = canvas.height - this.height;
}
}
}
const player = new Sprite( //making the player OBJECT and inserting the object &#39;position&#39; in it
{
position: {
x: 0,
y: 0
},
velocity: {
x: 0,
y: 10
}
}
);
const enemy = new Sprite( //making the enemy OBJECT and inserting the object &#39;position&#39; in it
{
position: {
x: 400,
y: 100
},
velocity: {
x: 0,
y: 0
}
}
);
console.log(player);
function animate(){
window.requestAnimationFrame(animate); // make an infinite loop by calling the function &#39;animate&#39; over and over again
c.fillStyle = &#39;black&#39;;
c.fillRect(0, 0, canvas.width, canvas.height);
player.update();
enemy.update();
}
animate();

I hope everything is clear.

I tried looking into many sites, including stack overflow.
I noticed that some people have the same problem, but I coudnt fix it, even though I followed all steps carefully.
Please help me in this problem so that I can continue
Thanks.

答案1

得分: 0

好的,所以我终于解决了。对于任何想知道的人,这是在你关闭正在运行的程序后出现的消息。另外,“height is undefined” 只是一个语法错误。如果你修复了这个语法错误,这种情况就不会再发生。错误会消失。

英文:

Ok, so I finally solved it.
for anyone wondering, it is a message that comes after you have closed the running program. Also, the height is undefined was just a syntax error

if you fix the syntax error, this won't happen again. The error will be gone.

huangapple
  • 本文由 发表于 2023年5月10日 19:48:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定