Sign in to follow this  
PLAYERKILLERS

me.Renderable.onVisibilityChange()

Recommended Posts

https://melonjs.github.io/melonJS/docs/me.Renderable.html#onVisibilityChange

this.onVisibilityChange = function(inViewport) {
    if (inViewport === true) {
        console.log("object has entered the in a camera viewport!");
    }
};

The suggested code doesn't work at all for me. inViewport could be either 1, 0, or false. 

melon.js line 9165 

 Object.defineProperty(me.Renderable.prototype, "inViewport", {
        /**
         * @ignore
         */
        get : function () {
            return this._inViewport;
        },
        /**
         * @ignore
         */
        set : function (value) {
            if (this._inViewport !== value) {
                this._inViewport = value;
                if (typeof this.onVisibilityChange === "function") {
                    this.onVisibilityChange.call(this, value);
                }
            }
        },
        configurable : true
    });

Even after editing this to:

      if (this._inViewport != value) {

The event still fires frequently while an enemy sprite is on screen.

Share this post


Link to post
Share on other sites

hi, I'm not sure I understand what you mean; are u saying that inViewport is sometimes equal to 1 or 0 ?

in theory this should not be possible, as the value is the one returned by the camera isVisible function, that always returns true or false :

https://github.com/melonjs/melonJS/blob/master/src/renderable/container.js#L836

https://github.com/melonjs/melonJS/blob/master/src/camera/camera2d.js#L575

Share this post


Link to post
Share on other sites

Yes, it is either 1, 0, or false in my testing. The event fires even when the the NPC is in the viewport. When the NPC has actually exited the screen it will be 0. The camera code seems to be setting 0 or 1. The default is false. 

Share this post


Link to post
Share on other sites

so yeah, not sure why to be honest, maybe the combination of bitwise operation and using getter, but indeed sometimes a non Boolean value was passed !

the fix is quite easy actually, have a look below in case you want to fix it in your current build without/while waiting for the 6.4.0 version (I will release it within a few days):

https://github.com/melonjs/melonJS/commit/117d083b70d82bcacb35767c24d1c1aaba26579b

thank you for the feedback !

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.