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

Utils Array Add Uniqueness Bug #4411

Closed
mudala opened this issue Mar 6, 2019 · 1 comment
Closed

Utils Array Add Uniqueness Bug #4411

mudala opened this issue Mar 6, 2019 · 1 comment

Comments

@mudala
Copy link

mudala commented Mar 6, 2019

Version

Phaser Version: 3.15.1

Description

Phaser.Utils.Array.Add's behavior is incorrect when adding elements to an array in which they already exist.

Example:

let a = [1, 2, 3];
Phaser.Utils.Array.Add(a, [1, 2, 4]);
console.log(a); // expected: [1, 2, 3, 4], actual: [1, 2, 3, 1]

This manifests itself when adding game objects to the display list through scene.children.add. If a game object has already been added to the scene's display list, and it is added again as a part of an array using scene.children.add, it can prevent valid game objects from being added.

Example Test Code

Direct example

let a = [1, 2, 3];
Phaser.Utils.Array.Add(a, [1, 2, 4]);
console.log(a); // expected: [1, 2, 3, 4], actual: [1, 2, 3, 1]

Game object example

var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    parent: 'phaser-example',
    scene: {
        create: create,
    }
};

var game = new Phaser.Game(config);

function create() 
{
    let text1 = this.make.text({ y: 0,  text: 'Text 1' }, true);
    let text2 = this.make.text({ y: 20, text: 'Text 2' }, false);
    let text3 = this.make.text({ y: 40, text: 'Text 3' }, false);

    this.children.add([text1, text2, text3]);
}

The expected behavior here is to see all three text objects. The actual result is text1 being added twice and text2 being added once, while text3 is not added at all.

Additional Information

The problematic code is here: https://github.com/photonstorm/phaser/blob/04080bc1bab46834e4e9947a17905c1be0d5560b/src/utils/array/Add.js#L75

A quick fix is to replace item.pop(); with item.splice(itemLength, 1);.

item.pop(); is incorrect because the duplicate item is not always in the last position of the array!

LoolzRules added a commit to LoolzRules/phaser that referenced this issue Mar 24, 2019
photonstorm added a commit that referenced this issue Apr 5, 2019
@photonstorm
Copy link
Collaborator

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

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