gpt4 book ai didi

javascript - self.Rotate() 不是原型(prototype)中的函数

转载 作者:行者123 更新时间:2023-11-30 15:47:51 25 4
gpt4 key购买 nike

我一直在查看其他与我类似的问题,但我似乎找不到答案。

我遇到的错误也在标题中:

Uncaught TypeError: self.Rotate() is not a function

这是发生这种情况的完整代码:

var Card = function(renderer, stage) {
var self = this;

self.name = "None";
self.health = 5;

self.renderer = renderer;
self.stage = stage;

self.sprite = null;

PIXI.loader.add("FirePlace/GW2-Logo.jpg").load(self.Setup);
};

Card.prototype.Setup = function() {
self.sprite = new PIXI.Sprite(PIXI.loader.resources["FirePlace/GW2-Logo.jpg"].texture);
console.log(self.stage);
self.stage.addChild(self.sprite);
console.log("Sprite loaded");
self.renderer.render(self.stage);
self.Rotate();
};

Card.prototype.SetName = function(name) {
self.name = name;
};

Card.prototype.Rotate = function() {
requestAnimationFrame(self.Rotate);

if (self.sprite === null)
console.log("Sprite is null");

if (self.sprite !== null && self.sprite.rotation <= 1.5708)
self.sprite.rotation += 0.03;

self.renderer.render(self.stage);
};

在 Setup 函数的末尾,我尝试调用 Rotate 函数,但这是它失败的地方。

最佳答案

我发现这段代码可以防止您遇到的 addChild 问题。查看代码中的注释进行解释。

var Card = function(renderer, stage) {
var self = this;

self.name = "None";
self.health = 5;

self.renderer = renderer;
self.stage = stage;

self.sprite = null;
var x = function(){
self.Setup.apply(self,arguments);
}
PIXI.loader.add("FirePlace/GW2-Logo.jpg").load(x); // origionally: PIXI.loader.add("FirePlace/GW2-Logo.jpg").load(self.Setup);
// self would not be defined in whatever other context it is used in when it is called. All I had to do was wrap it in a function and it was fixed
};

Card.prototype.Setup = function() {
var self = this;
self.sprite = new PIXI.Sprite(PIXI.loader.resources["FirePlace/GW2-Logo.jpg"].texture);
console.log("Stage is:",self.stage); // Now stage is defined since I wrapped this function in another function.
self.stage.addChild(self.sprite);
console.log("Sprite loaded");
self.renderer.render(self.stage);
self.Rotate();
};

Card.prototype.SetName = function(name) {
var self = this; // I also defined self = this in every single function :D
self.name = name;
};

Card.prototype.Rotate = function() {
var self = this;
var x = function(){
if (self.sprite === null)
console.log("Sprite is null");

if (self.sprite !== null && self.sprite.rotation <= 1.5708)
self.sprite.rotation += 0.03;

self.renderer.render(self.stage);
window.requestAnimationFrame(x);
}; // Same goes for this one.
window.requestAnimationFrame(x);


};

关于javascript - self.Rotate() 不是原型(prototype)中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801698/

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