在函数中调用函数。错误说它不是一个函数

时间:2014-01-30 01:07:42

标签: typescript

这是我运行代码时遇到的错误:

  

TypeError:this.NewSprite不是函数

该功能显然存在,但无论如何都会出现错误。这是对函数

的有问题调用
        OnJSONLoad(response) {
        //store JSONarray in variable
        this.json = JSON.parse(response);
        srcArray = this.json;
        this.atlasImage = new Image();
        this.atlasImage.src = 'Assets/' + srcArray.meta.image;
        image.src = 'Assets/' + srcArray.meta.image;
        var holder = [];
        for (var i = 0; i < srcArray.frames.length; i++) {
            holder[i] = this.NewSprite(srcArray.frames[i].filename);//this is the call!
        }
        AtlasCache[AtlasKey[atlasPos]] = holder;
        atlasPos++;
        OnComplete();
    }
}

这是在OnJSONLoad():

之前声明的NewSprite函数
NewSprite(spriteName) {
        this.isFound = false;
        for (var i = 0; i < srcArray.frames.length; i++) {
            //search for array element to matches the filename of the frame
            if (srcArray.frames[i].filename == spriteName) {
                var spriteWanted = srcArray.frames[i];
                this.isFound = true;
                //return new sprite function with all the dimensions and data of the frame
                return new this.defineSprite(this.atlasImage, spriteWanted.frame.x, spriteWanted.frame.y, spriteWanted.frame.w, spriteWanted.frame.h);
                break;
            }
        }
        if (!this.isFound) {
            alert("Error: Sprite \"" + spriteName + "\" not found");
        }
    }

提前致谢。

1 个答案:

答案 0 :(得分:0)

使用Robert Slaney建议的箭头功能,它有效!

           var OnJSONLoad = (response) => {
            this.json = JSON.parse(response);
            srcArray = this.json;
            this.atlasImage = new Image();
            this.atlasImage.src = 'Assets/' + srcArray.meta.image;
            image.src = 'Assets/' + srcArray.meta.image;
            var holder = [];
            for (var i = 0; i < srcArray.frames.length; i++) {
                holder[i] = this.NewSprite(srcArray.frames[i].filename);
            }