gpt4 book ai didi

javascript - 无法从未定义中读取属性 "texture"

转载 作者:行者123 更新时间:2023-11-28 08:08:05 28 4
gpt4 key购买 nike

我知道这是一个非常常见的问题。我收到无法从未定义中读取属性“纹理”的消息。抱歉,我对 javascript 还很陌生。

我的代码的javascript部分如下

this.createTexture=function(Texturename,width,height){
this.Texture[Texturename]=gl.createTexture();
this.Texture[Texturename].name=Texturename;
this.Texture[Texturename].width=width;
this.Texture[Texturename].height=height;
return this.Texture[Texturename,width,height];
}

我正在尝试在 webgl 部分创建我的纹理。

我怎样才能在webgl中调用createTexture?我只需输入纹理名称、宽度和高度即可让该函数正常工作。我知道这不仅仅会创建纹理,而只是一个简单的例子。我试过了,但是不行

function exampleTexture(){
Texture.createTexture("moon",1024,1024);
}

它说我的错误在行

this.Texture[Texturename]=gl.createTexture();

谢谢

最佳答案

this 引用的对象似乎没有 Texture 属性。在这种情况下:

this.createTexture=function(Texturename,width,height){
// use this.Texture if available, otherwise init it as an empty object
this.Texture = this.Texture || {};

this.Texture[Texturename]=gl.createTexture();
this.Texture[Texturename].name=Texturename;
this.Texture[Texturename].width=width;
this.Texture[Texturename].height=height;
return this.Texture[Texturename,width,height];
}

命名约定注意事项:在 JS 中,变量通常使用驼峰命名法,构造函数使用 PascalCase。因此,我会将 this.Texture 重命名为 this.textures,将 Texturename 重命名为 textureName

关于javascript - 无法从未定义中读取属性 "texture",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24592907/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com