Sign in to follow this  
Growler

LevelEntity goTo re-adding player entity even with isPeristent

Recommended Posts

@Parasyte @obiot

On Melon 5.1, I want to let the player travel/warp around the same map. They click on a place they want to go, and it loads the player to that location. And really, for any NPC entity, I'd want it to be persistent because I alter property values on the entity and don't want them reset.

Except as you can see from the video below, it fades to white twice, but also me.game.world.children's length grows by one. It's hard to tell what is being added to me.game.world.children array and why.

The PlayerEntity isPersistent flag is true, yet it seems to load the player entity again anyway.

PlayerEntity instance:

                 this.isPersistent = true

                 console.log('PLAYER RENDERABLE: ', this.renderable);

me.LevelEntity instance:

                Utilities.Main.setSubwayContent((args) => {
                    game.data.spawnX = args.x;
                    game.data.spawnY = args.y;
                    this.goTo();
                });

               ...

                 onFadeComplete: function() {
                         game.data.playerEntity.pos.x = game.data.spawnX;
                         game.data.playerEntity.pos.y = game.data.spawnY;
                  }

//////////////////////////////////////////////////////////////////////////////


If I use levelDirector instead and set the pos on the entity when the new level loads, it goes haywire (see 2nd video).

me.LevelEntity instance:

                Utilities.Main.setSubwayContent((args) => {
                    game.data.spawnX = args.x;
                    game.data.spawnY = args.y;
                    this.goTo();
                });   

              ...

          onFadeComplete: function() {
                me.levelDirector.loadLevel(this.gotolevel);
          }

game.PlayerEntity = me.Entity.extend({
    init: function(x, y, settings) {
        // call the constructor
        this._super(me.Entity, 'init', [x, y, settings]);

        if (game.data.spawnX && game.data.spawnY) {
            this.pos.x = game.data.spawnX;
            this.pos.y = game.data.spawnY;
        }

Question: what's a safe, easy way to respawn the player elsewhere in the map?

 

 

Share this post


Link to post
Share on other sites

Hi, sorry, I started looking at a small thing earlier this week, and ended being busy tracking down memory allocation in melonJS since the last 2 days, which was really useful at the end :)

I think the issue and therefore the solution is a mix of the following  :

- persistent object should not be defined in a map, but added manually, else you are indeed adding additional new persistent object when you are changing map. you could still define a transparent non-colliding element in the map as the "spawning point", and retrieve the coordinates of it (together maybe with other parameters) using one of the `me.game.world.getChildXXXX` (as a reminder the root world object in melonJS is a me.Container as well) to manually add the player entity. -> that would be (in my opinion) the safest way to respwan an entity in a map (does not even need to be persistent actually)

- the double fading is probably due to the object being indeed persistent and triggering a second time the level change. if I am correct, probably you could just add a flag that is only resetted in the object constructor to avoid changing level again, and do something like the following pseudo-code `if (triggered == false ) then { triggered = true ; changeLevel() ; }` .

let me know if this helps, or if I missed/misunderstood anything.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.