Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

world.wrap bug #2045

Closed
VitaZheltyakov opened this issue Aug 29, 2015 · 1 comment
Closed

world.wrap bug #2045

VitaZheltyakov opened this issue Aug 29, 2015 · 1 comment

Comments

@VitaZheltyakov
Copy link
Contributor

world.wrap do not use world coordinates. So after scaling world, wrap is not working properly.
This code corrects error:

/**
* This will take the given game object and check if its x/y coordinates fall outside of the world bounds.
* If they do it will reposition the object to the opposite side of the world, creating a wrap-around effect.
* If sprite has a P2 body then the body (sprite.body) should be passed as first parameter to the function.
*
* @method Phaser.World#wrap
* @param {Phaser.Sprite|Phaser.Image|Phaser.TileSprite|Phaser.Text} sprite - The object you wish to wrap around the world bounds.
* @param {number} [padding=0] - Extra padding added equally to the sprite.x and y coordinates before checking if within the world bounds. Ignored if useBounds is true.
* @param {boolean} [useBounds=false] - If useBounds is false wrap checks the object.x/y coordinates. If true it does a more accurate bounds check, which is more expensive.
* @param {boolean} [horizontal=true] - If horizontal is false, wrap will not wrap the object.x coordinates horizontally.
* @param {boolean} [vertical=true] - If vertical is false, wrap will not wrap the object.y coordinates vertically.
*/
Phaser.World.prototype.wrap = function (sprite, padding, useBounds, horizontal, vertical) {

    if (padding === undefined) { padding = 0; }
    if (useBounds === undefined) { useBounds = false; }
    if (horizontal === undefined) { horizontal = true; }
    if (vertical === undefined) { vertical = true; }

    if (!useBounds)
    {
        if (horizontal && sprite.world.x + padding < this.bounds.x)
        {
            sprite.x = (sprite.x / sprite.world.x) * (this.bounds.right + padding);
        }
        else if (horizontal && sprite.world.x - padding > this.bounds.right)
        {
            sprite.x = (sprite.x / sprite.world.x) * (this.bounds.left - padding);
        }

        if (vertical && sprite.world.y + padding < this.bounds.top)
        {
            sprite.y = (sprite.y / sprite.world.y) * (this.bounds.bottom + padding);
        }
        else if (vertical && sprite.world.y - padding > this.bounds.bottom)
        {
            sprite.y = (sprite.y / sprite.world.y) * (this.bounds.top - padding);
        }
    }
    else
    {
        sprite.getBounds();

        if (horizontal)
        {
            if ((sprite.world.x + sprite._currentBounds.width) < this.bounds.x)
            {
                sprite.x = (sprite.x / sprite.world.x) * this.bounds.right;
            }
            else if (sprite.world.x > this.bounds.right)
            {
                sprite.x = (sprite.x / sprite.world.x) * this.bounds.left;
            }
        }

        if (vertical)
        {
            if ((sprite.world.y + sprite._currentBounds.height) < this.bounds.top)
            {
                sprite.y = (sprite.y / sprite.world.y) * this.bounds.bottom;
            }
            else if (sprite.world.y > this.bounds.bottom)
            {
                sprite.y = (sprite.y / sprite.world.y) * this.bounds.top;
            }
        }
    }

};
photonstorm added a commit that referenced this issue Apr 5, 2016
@photonstorm
Copy link
Collaborator

I tried this code, scaled the world and it all went to hell when it tried wrapping. Possibly because of the Camera though, either way it didn't work quite right. I'm going to accept this as a limitation of the current method, and have updated the jsdocs accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants