Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Jose Perez
Jose Perez
1,338 Points

Can some explain to me the following

What does Level level = new(invaders){ Towers = towers };

Do please?? I’m a little lost I’m try to connect how everything works Thank you.

Steven Parker
Steven Parker
231,261 Points

Please provide a link to where you saw this, it doesn't seem like valid code.

I'm wondering if you left out the word "Level" between "new" and "(invaders)"?

3 Answers

Jose,

I remember having to pause the video so I could let this soak in, too! Prior to this video you should have created a 'Level' class that takes in an array of invaders as a parameter. What I've pasted in is the code that shows the creation of the invaders array, the creation of the towers array, and then the creation of your level in the 'Game.cs'.

Even though your level class has a 'Towers' field, it isn't required to create the level object. What Jeremy is doing is creating the level object with the invaders array AND also assigning the towers array to the Towers field of the level object. Hope this clarifies some things!

Here is a link to the video: (https://teamtreehouse.com/library/playing-the-game)

Snippet from 'Game.cs':

                Invader[] invaders = 
                {
                    new ShieldedInvader(path),
                    new FastInvader(path),
                    new StrongInvader(path),
                    new Invader(path)
                };

                Tower[] towers =
                {
                    new SniperTower(new MapLocation(1, 3, map)),
                    new PowerfulTower(new MapLocation(3, 3, map)),
                    new LongRangeTower(new MapLocation(5, 3, map))
                };

                Level level1 = new Level(invaders)
                {
                    Towers = towers
                };

Added a link to my post!

Jose Perez
Jose Perez
1,338 Points

thank you very helpful!