1
\$\begingroup\$

I don't understand the behaviour of containers in this example:

Phaser example

The part I don't unerstand is line 41. Why is lastContainer assigned to newContainer? In line 40, newContainer is added as a child of lastContainer; my intuition would be that the effect of line 40 is so that the rotations can be performed on lastContainer (or, rather, on containerTails, which seems to contain four nested container structures, one for each arm), and propagated to its children.

It makes no sense to me that the variable containing the parent container should be assigned to its own child; how does that not overwrite the nested structure of containers, effectively removing the parent container from the picture?

\$\endgroup\$
1
  • \$\begingroup\$ For me containers are just here to make things happen the same for all of them like a sort of a group when they are linked together but with some properties. What does inspires you the documentation ? photonstorm.github.io/phaser3-docs/… \$\endgroup\$ Commented Jun 8, 2019 at 21:19

1 Answer 1

0
\$\begingroup\$

No no, see the lastContainer variable is stored outside of the loop (line 23). So what the code is doing from line 39 is creating a new container, adding it to the previous container and setting the new container as the lastContainer this way the next container can add itself to the newContainer.

    var lastContainer;

    // ...

    for (var index = 0; index < count; ++index) {
        // In an if statement
        {
            // Create the new container (only if this is false index === 0
            var newContainer = this.make.container({x: image.width, y: 0, add: false});

            // Now add it to the previous container
            lastContainer.add(newContainer);

            // And set the lastContainer to the new container so that the loop does update the same container over and over again
            lastContainer = newContainer;
        }
        // ...
    }
```
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.