Simple clicker with storage Demo

Hi all I’m trying to create a very simple Clicker demo on Phaser 3 but Im stuck, Im unable to call any functions, this is my complete code I marked my errors with <=== for ease, could anyone please assists? I have no idea what Im doing wrong and do please tell me if the whole thing is wrong as well I would really appreciate this thanks a lot all. :expressionless:(
the error is: Uncaught TypeError: this.setScore is not a function

Please find code below:

    var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);

var Game = {};

var mainscore;
var mainscoreText;
var visits;
var visitsText;

function preload ()
{

	this.load.image('enemy', '/images/enemy.png');


}

function create ()
{
	
	mainscoreText = this.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#fff' });
	this.setScore();	<=============================================my ERROR starts here

	visitsText = this.add.text(16, 100, 'Visits: 0', { fontSize: '32px', fill: '#fff' });
	this.setVisits();  <=============================================OR HERE IF I COMMENT OUT THE OTHER FUNCTION

	var skeleton = this.add.image(400,300,'enemy').setInteractive();
	    skeleton.on('pointerdown', function (pointer) {

        this.setTint(0xff0000);
        this.updateScore(10);
        mainscoreText.setText('Score: ' + mainscore);
              

    });

    skeleton.on('pointerout', function (pointer) {

        this.clearTint();

    });

    skeleton.on('pointerup', function (pointer) {

        this.clearTint();

    });


}

function update ()
{
}


function render() 
{

}

function setScore()
{

	this.scene.mainscore = parseInt(localStorage.getItem('mainscore')) || 0;
    this.scene.scoreTxt.setText(this.scene.mainscore);	

}

function setVisits() 
{

	this.scene.visits = (parseInt(localStorage.getItem('visits')) || 0) + 1;
    this.scene.visitsTxt.setText(this.scene.visits);
   
    localStorage.setItem('visits',this.scene.visits);

}

function saveFile() 
{

	 var file = {
        score: this.scene.mainscore,
        visits: this.scene.visits
    };
    localStorage.setItem('saveFile',JSON.stringify(file));

}

function loadFile() 
{

	var file = JSON.parse(localStorage.getItem('saveFile'));
    this.scene.score = file.mainscore;
    this.scene.visits = file.visits;

}

function updateScore() 
{

	this.scene += increment;
	this.scene.mainscoreText.setText(this.scene.mainscore);
localStorage.setItem('mainscore', this.scene.mainscore);

}

‘this’ refers to scene. Your functions are not defined there. Lose the ‘this’.

1 Like

Thanks sorry to be a noob :slight_smile:

Everyone’s a noob sometime :slight_smile:

Noob Patrons are rare though…