Hi Again

Looking to code the following scores into a class called 'die'.

Basically i'm recreating space invaders and everytime a alien dies the relevant score needs to be added up. I currently have this as my points:

public enum gamescore
        {
            Martian = 10,
            Vesuvian = 20,
            Mercurian = 30,
            Meteor = 50,
            MotherShip = 100,
            Destroyer = 200

        }

Im looking to code it into the .base so it reflects across the 6 classes. The method I want it to work within is as follows:

public virtual void Die()
        {

            boundary.Controls.Remove(picture); //removes picture when they die
        }

Could any of you give help me with the code required to add to the 'Die' method?

Cheers

P

Recommended Answers

All 3 Replies

Create a property in your base class to house the gamescore enum like so:

public gamescore GameScore { get; set; }

Then in the constructor of each class, set the GameScore property accordingly, for example:

public MotherShip()
{
  GameScore = gamescore.MotherShip;
}

Then in your Die() method in your base class, you should return the points scored like so:

public int Die()
{
  boundary.Controls.Remove(picture);
  return GameScore;
}

Incidentally, I don't believe that you want to make your Die method virtual.

Hope this helps,
darkagn :)

So just to clarify the base would have code like this:

public virtual void Die()
        {
            boundary.Controls.Remove(picture); //removes picture when they die
            return GameScore;
        }
public gamescore GameScore 
      {
          get { return GameScore; }
          set { GameScore = value; }

If I change the Die() method from virtual to int then it brings up a whole host of errors.

Cheers

No that's not quite right. Take a look again at the signature I used for the Die method.

// not public virtual void Die(), instead:
public int Die()
// but if you want you could do:
// public virtual int Die()

And the property should be simply:

public gamescore Gamescore
{
  get;
  set;
}
commented: Very nice! +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.