Jump to content

LevelEntity goTo re-adding player entity even with isPeristent


Growler
 Share

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?

 

 

Link to comment
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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...