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# Intermediate C# Abstraction Wrap Up

evanpavan
evanpavan
5,025 Points

Can someone help me understand why this method is not being called?

I've added this method to the Resurrecting Invader to print the message to the console. When I run the application the message is never printed even when all the invaders die.

Can someone help me understand why the method may not be being called?

        public void Resurrect()
        {
            if(_incarnation1.IsNeutralized)
            {
                System.Console.WriteLine("The invader came back to life, even stronger!");
            }
        }

The rest of the Resurrecting Invader code is per the same as the lesson

namespace TreehouseDefense
{
    class ResurrectingInvader : IInvader
    {
        private BasicInvader _incarnation1;
        private StrongInvader _incarnation2;

        public MapLocation Location => _incarnation1.IsNeutralized ? _incarnation2.Location : _incarnation1.Location;

        public int Health => _incarnation1.IsNeutralized ? _incarnation2.Health : _incarnation1.Health;

        public bool HasScored => _incarnation1.HasScored || _incarnation2.HasScored;

        public bool IsNeutralized => _incarnation1.IsNeutralized && _incarnation2.IsNeutralized;

        public bool IsActive => !(IsNeutralized || HasScored);

        public ResurrectingInvader(Path path)
        {
            _incarnation1 = new BasicInvader(path);
            _incarnation2 = new StrongInvader(path);
        }

        public void DecreaseHealth(int factor)
        {
            if(!_incarnation1.IsNeutralized)
            {
                _incarnation1.DecreaseHealth(factor);         
            }
            else
            {
                _incarnation2.DecreaseHealth(factor);
            }
        }
        public void Move()
        {
            _incarnation1.Move();
            _incarnation2.Move();
        }

        public void Resurrect()
        {
            if(_incarnation1.IsNeutralized)
            {
                System.Console.WriteLine("The invader came back to life, even stronger!");
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
220,598 Points

You've created a new method, but it doesn't look like you have any code yet to actually call this method.

I'd guess you probably want to call it from inside the "DecreaseHealth" method, after the health level of first incarnation has been changed.