Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Loops and Final Touches Playing the Game

Fredrik Rönnehag
Fredrik Rönnehag
2,342 Points

Creating the tower[] object

Error message: Error CS7036 There is no argument given that corresponds to the required formal parameter 'path' of 'Tower.Tower(MapLocation, Path)'

Game.cs

// Lines causing the error

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

Tower.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TowerDefenseGame
{
    class Tower
    {
        private readonly MapLocation _mapLocation;
        private const int _turretRange = 1;
        private const int _turretPower = 1;

        public Tower(MapLocation location, Path path)
        {
            for (int i = 0; i < path.Length; i++)
            {
                MapLocation pathToCheck = path.GetLocationAt(i);

                if(location.X == pathToCheck.X && location.Y == pathToCheck.Y)
                {
                    throw new OutOfBoundsException("Towers can't be placed on a path");
                }
            }
            _mapLocation = location;
        }

        public void FireOnInvaders(Invader[] invaders)
        {

            foreach(var invader in invaders)
            {

                if (invader.IsActive && _mapLocation.InRangeOf(invader.Location, _turretRange))
                {
                    invader.DecreaseHealth(_turretPower);
                    break;
                }
            }

        }


    }
}

1 Answer

Fredrik Rönnehag
Fredrik Rönnehag
2,342 Points

Found the error!

        public Tower(MapLocation location)
        {
            l_location = location;
        }

Needed to create a second constructor.