Uncaught TypeError: Cannot read property 'Tween' of undefined Phaser.io

Uncaught TypeError: Cannot read property 'Tween' of undefined Phaser.io

我正在为我的游戏创建关卡选择屏幕,我发现出现以下错误。 Uncaught TypeError: Cannot read property 'Tween' of undefined,错误出现在脚本底部的 "arrowClicked" 函数中。

UshanGame.Selection = function(game){};

var thumbRows = 2;
var thumbCols = 3;
var thumbWidth = 128;
var thumbHeight = 128;
var thumbSpacing = 3;
var levelThumbsGroup;
var currentPage = 0;
var leftArrow;
var rightArrow;
var pages;


UshanGame.Selection.prototype = {
create: function(){
    console.log("%c ✔✔ Level Selection Ready! ✔✔", "color:red;");

    // array with finished levels and stars collected.
    var starsArray = [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2];
    // how many pages are needed to show all levels?
    pages = starsArray.length /(thumbRows * thumbCols);

    leftArrow = this.add.button(50, 420, "level_arrows", this.arrowClicked);
    leftArrow.anchor.setTo(0.5);
    leftArrow.frame = 0;
    leftArrow.alpha = 0.3;
    rightArrow = this.add.button(270, 420, "level_arrows", this.arrowClicked);
    rightArrow.anchor.setTo(0.5);
    rightArrow.frame = 1;

    // creation of the thumbails group
    levelThumbsGroup = this.add.group();

    // determining level thumbnails width and height for each page
    var levelLength = thumbWidth * thumbCols + thumbSpacing * (thumbCols - 1);
    var levelHeight = thumbWidth * thumbRows + thumbSpacing * (thumbRows - 1);

    // looping through each page
    for(var l = 0; l < pages; l++){
        // horizontal offset to have level thumbnails horizontally centered in the page
        var offsetX = (this.width-levelLength)/ 2 + game.width * l;
        // I am not interested in having level thumbnails vertically centered in the page, but
        // if you are, simple replace my "20" with
        // (game.height-levelHeight)/2
        var offsetY = 20;

        // looping through each level thumbnails
         for(var i = 0; i < thumbRows; i ++){
            for(var j = 0; j < thumbCols; j ++){  
                var levelNumber = i * thumbCols + j + l *(thumbRows * thumbCols);       
                var levelThumb = this.add.button(offsetX + j * (thumbWidth + thumbSpacing), offsetY + i * (thumbHeight + thumbSpacing), "levels", this.thumbClicked, this); 
                levelThumb.frame=starsArray[levelNumber];
                levelThumb.levelNumber = levelNumber + 1;
                levelThumbsGroup.add(levelThumb);
                // if the level is playable, also write level number
                if(starsArray[levelNumber] < 4){
                    var style = {
                        font: "18px Arial",
                        fill: "#ffffff"
                    };
                    var levelText = this.add.text(levelThumb.x + 5, levelThumb.y + 5, levelNumber + 1,style);
                    levelText.setShadow(2, 2, 'rgba(0,0,0,0.5)', 1);
                    levelThumbsGroup.add(levelText);
                }
            }
        }
    }
},

arrowClicked: function(button){
    if(button.frame == 1 && currentPage < pages - 1){
        leftArrow.alpha = 1;
        currentPage++;

        if(currentPage == pages - 1){
            button.alpha = 0.3;
        }

        var buttonsTween = game.add.tween(levelThumbsGroup);
        buttonsTween.to({
            x: currentPage * game.width * -1
        }, 500, Phaser.Easing.Cubic.None);
        buttonsTween.start();
    }

    if(button.frame==0 && currentPage>0){
        rightArrow.alpha = 1;
        currentPage--;

        if(currentPage == 0){
            button.alpha = 0.3;
        }

        var buttonsTween = game.add.tween(levelThumbsGroup);
        buttonsTween.to({
            x: currentPage * game.width * -1
        }, 400, Phaser.Easing.Cubic.None);
        buttonsTween.start();
    }       
}
};

game.add 可能从未被定义,所以它被归类为 undefined.

game 显然是在传递给函数时定义的,但消息指出它不包含 属性 .add 因此您无法调用方法 tween undefined 属性.

因此您收到此错误; undefined 不包含 tween 因此您必须在包含 tweengame 对象中正确定义 game.add 以使您的代码正常工作。

试试看 this.add.tween 代替 game.add.tween