SetFlame
SetFlame

Reputation: 89

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

I am creating a level selection screen to my game, and I found that I get the following error. Uncaught TypeError: Cannot read property 'Tween' of undefined, the error comes a line that can be found in the "arrowClicked" function at the bottom of the script.

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();
    }       
}
};

Upvotes: 0

Views: 3920

Answers (2)

Tim
Tim

Reputation: 625

try with this.add.tween instead of game.add.tween

Upvotes: 1

Jonast92
Jonast92

Reputation: 4967

game.add is probably never defined so it's classified as undefined.

game is obviously defined as it's passed into the function but the message is stating that it does not contain a property .add so you're unable to call a method tween of that undefined property.

Therefor you're receiving this error; undefined does not contain tween so you must define game.add within the game object which contains tween properly in order for your code to work.

Upvotes: 0

Related Questions