Changing scene when entity reach past a certain position

Hello everybody,

I want to trigger a ‘changeScene’ when an entity reach a certain position.
This is how I achieve it it currently.

I attached a script to my moving entity and when this entity reaches past a certain position the scene changes.

GotoMainscene.prototype.update = function(dt) {

    var entityPos = this.entity.getPosition();

    console.log(entityPos);

    if(entityPos.x>-2){

        this.app.scenes.changeScene("main");

    }

};

The problem is that I don’t think this is the smart way of to do it.
And doing so causes many problems. It is like calling the changeScene(“main”) not once but many times.
does anybody know a better way to do it ? Getting to a different scene triggered by the position of an object ?

Add a flag that stops check when the goal is reached

GotoMainscene.prototype.update = function(dt) {

    var entityPos = this.entity.getPosition();

    console.log(entityPos);

    if(entityPos.x>-2 && !this.reachedGoal){

        this.app.scenes.changeScene("main");
        this.reachedGoal = true;

    }

};

changeScene is async. It doesn’t immediately change the scene (unless the scene data is preloaded).

See: Loading Scenes | Learn PlayCanvas