Javascript:为什么这个实例变量未定义?

发布于 2024-12-17 06:37:42 字数 3501 浏览 0 评论 0原文

我是 Javascript 新手,正在使用 WebGL 并收到错误,“self.starshipVertexPositionBuffer 未定义”。当尝试访问 itemSize 属性时,它看起来像是崩溃了(请参阅下面的评论),但在执行该语句之前它正在初始化(请参阅下面的评论),所以我不明白为什么它会因该错误而崩溃。你的想法?

    var starship;
    function initstarship()
    {
        starship = new starship();
        starship.initBuffers();
    }

    function starship()
    {
        this.angle = 0;
        this.speed = 0;
        this.xDir = 0;
        this.yDir = 0;
        this.xPos = 0.0;
        this.yPos = 0.0;
        this.starshipVertexPositionBuffer = null;
        this.starshipVertexTextureCoordBuffer = null;
        this.starshipVertexIndexBuffer = null;
    }

    starship.prototype.updatePosition = function(timeElapsed){
        this.xPos += this.xDir * this.speed;
        this.yPos += this.yDir * this.speed;
    }   

    starship.prototype.initBuffers = function () {
        this.starshipVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, this.starshipVertexPositionBuffer);
        vertices = [
            -0.15, -0.15,  0.15,
             0.15, -0.15,  0.15,
             0.15,  0.15,  0.15,
            -0.15,  0.15,  0.15,

        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

        //INITIALIZED HERE
        this.starshipVertexPositionBuffer.itemSize = 3;
        this.starshipVertexPositionBuffer.numItems = 4;

        this.starshipVertexTextureCoordBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, this.starshipVertexTextureCoordBuffer);
        var textureCoords = [
            0.0, 0.0,
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0,

        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
        this.starshipVertexTextureCoordBuffer.itemSize = 2;
        this.starshipVertexTextureCoordBuffer.numItems = 4;

        this.starshipVertexIndexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.starshipVertexIndexBuffer);
        var starshipVertexIndices = [
            0, 1, 2,      0, 2, 3,    
        ]
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(starshipVertexIndices), gl.STATIC_DRAW);
        this.starshipVertexIndexBuffer.itemSize = 1;
        this.starshipVertexIndexBuffer.numItems = 6;
    }

    starship.prototype.draw = function()
    {    
        mvPushMatrix();

        mat4.translate(mvMatrix, [this.xPos, this.yPos, z]);

        mat4.rotate(mvMatrix, degToRad(this.angle), [0, 0, 1]);

        gl.bindBuffer(gl.ARRAY_BUFFER, self.starshipVertexPositionBuffer);

        //CRASHING ON THIS NEXT LINE
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, self.starshipVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);  

        gl.bindBuffer(gl.ARRAY_BUFFER, self.starshipVertexTextureCoordBuffer);
        gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, self.starshipVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);

        gl.activeTexture(gl.TEXTURE0);
        gl.bindTexture(gl.TEXTURE_2D, starshipTexture);
        gl.uniform1i(shaderProgram.samplerUniform, 0);

        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, self.starshipVertexIndexBuffer);
        setMatrixUniforms();
        gl.drawElements(gl.TRIANGLES, self.starshipVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        mvPopMatrix();
    }

    function webGLStart() {
        initstarship();
        tick();
    }

非常感谢您的智慧!

I'm new to Javascript and I'm playing around with WebGL and getting the error, "self.starshipVertexPositionBuffer is undefined". It looks like it's crashing (see comment below) when trying to access the itemSize attribute, but it's being initialized (see comment below) before that statement is made, so I don't understand why it's crashing with that error. Your thoughts?

    var starship;
    function initstarship()
    {
        starship = new starship();
        starship.initBuffers();
    }

    function starship()
    {
        this.angle = 0;
        this.speed = 0;
        this.xDir = 0;
        this.yDir = 0;
        this.xPos = 0.0;
        this.yPos = 0.0;
        this.starshipVertexPositionBuffer = null;
        this.starshipVertexTextureCoordBuffer = null;
        this.starshipVertexIndexBuffer = null;
    }

    starship.prototype.updatePosition = function(timeElapsed){
        this.xPos += this.xDir * this.speed;
        this.yPos += this.yDir * this.speed;
    }   

    starship.prototype.initBuffers = function () {
        this.starshipVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, this.starshipVertexPositionBuffer);
        vertices = [
            -0.15, -0.15,  0.15,
             0.15, -0.15,  0.15,
             0.15,  0.15,  0.15,
            -0.15,  0.15,  0.15,

        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

        //INITIALIZED HERE
        this.starshipVertexPositionBuffer.itemSize = 3;
        this.starshipVertexPositionBuffer.numItems = 4;

        this.starshipVertexTextureCoordBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, this.starshipVertexTextureCoordBuffer);
        var textureCoords = [
            0.0, 0.0,
            1.0, 0.0,
            1.0, 1.0,
            0.0, 1.0,

        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
        this.starshipVertexTextureCoordBuffer.itemSize = 2;
        this.starshipVertexTextureCoordBuffer.numItems = 4;

        this.starshipVertexIndexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.starshipVertexIndexBuffer);
        var starshipVertexIndices = [
            0, 1, 2,      0, 2, 3,    
        ]
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(starshipVertexIndices), gl.STATIC_DRAW);
        this.starshipVertexIndexBuffer.itemSize = 1;
        this.starshipVertexIndexBuffer.numItems = 6;
    }

    starship.prototype.draw = function()
    {    
        mvPushMatrix();

        mat4.translate(mvMatrix, [this.xPos, this.yPos, z]);

        mat4.rotate(mvMatrix, degToRad(this.angle), [0, 0, 1]);

        gl.bindBuffer(gl.ARRAY_BUFFER, self.starshipVertexPositionBuffer);

        //CRASHING ON THIS NEXT LINE
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, self.starshipVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);  

        gl.bindBuffer(gl.ARRAY_BUFFER, self.starshipVertexTextureCoordBuffer);
        gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, self.starshipVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);

        gl.activeTexture(gl.TEXTURE0);
        gl.bindTexture(gl.TEXTURE_2D, starshipTexture);
        gl.uniform1i(shaderProgram.samplerUniform, 0);

        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, self.starshipVertexIndexBuffer);
        setMatrixUniforms();
        gl.drawElements(gl.TRIANGLES, self.starshipVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);

        mvPopMatrix();
    }

    function webGLStart() {
        initstarship();
        tick();
    }

Thanks so much for your wisdom!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2024-12-24 06:37:42

您没有在该代码中的任何地方定义 self 。您可能会将其与 this 混淆。

You don't define self anywhere in that code. You might be confusing it with this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文