Semyon Fedyukovich
Semyon Fedyukovich

Reputation: 31

phaser js 3 loadTexture is not a function

I'd like to write some tetris like output consisting from blocks, that may be in 2 states. And I need to change sprite of block, when it becomes active. For test I just added setting one block active on pressing button left.

I tried call loadTexture, but it throws loadTexture is not a function error

    function create() {
        field = new Array(height)
        for (let i = 0; i < height; i++) {
            field[i] = new Array(width);
            for (let j = 0; j < width; j++) {
                field[i][j] = new Object();
                field[i][j].isActive = false;
                field[i][j].sprite = this.add.sprite(j * blockSize, i * blockSize, 'block').setOrigin(0, 0);
            }
        }
        cursors = this.input.keyboard.createCursorKeys();
    }

    function update() {
        if (cursors.left.isDown) {
            field[1][1].isActive = true;
        }
        for (let i = 0; i < height; i++) {
            for (let j = 0; j < width; j++) {
                if (field[i][j].isActive && field[i][j].sprite.texture.key != 'blockActive') {
                    field[i][j].sprite.loadTexture('blockActive', 0);
                }
            }
        }
    }

Upvotes: 3

Views: 895

Answers (1)

PhotonStorm
PhotonStorm

Reputation: 3385

You've already figured out that is is setTexture and not loadTexture. However, you should really look into using a texture atlas and just changing the frame (via setFrame), rather than setting an entirely new texture each time the player presses a key.

Upvotes: 1

Related Questions