0

I have a variable that I want the program to continuously check its value. the code goes something like this:

var score = 1

if (player touches the star) 
{
    player moves to bottom of screen
score = score + 1
}

if (score == 2)
{
   a bunch of images.x = 1300
`your text`}

this is obviously not the real code (i use javascript with phaser library) but i hope that you get the idea

i tried changing the place in the code where if (score == 2) { a lot of images move off screen} but it did not work. I even put the places where the images .x properties were mentioned inside if (score > 2) if statements but it did not move the images no matter what. I think that the only way to fix this is to constantly check the score variable.

4
  • sorry guys ignore the 'mytext' thing in the question Commented Apr 28, 2024 at 22:59
  • a very simmilar question was asked a few years ago on this website with the title JavaScript continuously check a variable but that only works for true or false variables it seems like Commented Apr 28, 2024 at 23:06
  • I believe this will help: stackoverflow.com/a/50862441 Commented Apr 28, 2024 at 23:11
  • Maybe you are talking about the “game loop”. Commented Apr 29, 2024 at 0:17

1 Answer 1

0

The easiest way to check a variable multiple time is as James mentiond in the game loop, the update function.

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    scene: {
        create,
        update
    },
    // ...
}; 

function create () {
   // ...
}

function update () {
    //... this function is called 60 times per second
}
new Phaser.Game(config);

You just will have to becareful how to access the variables. Make the part of the object like this this.score = 2 or make the global variable. I would not recommend the later.

Update, based on comment:

document.body.style = 'margin:0;';

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    physics: {
        default: 'arcade',
        arcade: { gravity: { y: 100 }},
    },
    scene: {
        create,
    },
}; 

function create () {
    let score = 0;
    let label = this.add.text(10,10, 'Score: 0')
        .setScale(1.5)
        .setOrigin(0)
        .setStyle({fontStyle: 'bold', fontFamily: 'Arial'});

    let player = this.add.rectangle( 50, 10, 10, 10, 0xffffff );
    
    // adding a physics body to the GameObjects 
    this.physics.add.existing(player); 
    
    // create "Coins" GameObjects
    let coin1 = this.add.rectangle( 50, 50, 10, 10, 0xff0000 );
    let coin2 = this.add.rectangle( 50, 80, 10, 10, 0xff0000 );
    
    // adding a physics body to the GameObjects 
    this.physics.add.existing(coin1, true); 
    this.physics.add.existing(coin2, true); 
    
    // collider: callback function that checks if the player collides with coins
    // One could also use the function "overlap", if the player should not be slowed down, on collision
    this.physics.add.collider(player, [coin1, coin2], (p,c) =>{
        // remove coin
        c.destroy();
        // add score
        score += 2;
        // update label
        label.setText(`Score: ${score}`);
    });
}

new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

I understand that I must put this.score = 2 in the update function, but how would i put the function that is called when the player touches the star in the update function (it crashes when I do this).
@BilalSiddiqui It depends always on your usecase since there are many ways to do things in phaser. I updated my answer with a new code. Here I'm using the collider function, this one is only called when to objects collide. I could have done it with the update function, but in this usecase like this it is easier. I hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.