Jump to content

Adding Properties and creating methods


rissa146

Recommended Posts

3) Add a lives property and subtractLife and addLife methods that will increment or decrement a player's lives. A new player should start off with 3 lives.
function PlayerConst(userName, score, highScore) {
this.username = userName;
this.score = score;
this.highScore = highScore;
this.gameOver.reset = score;

}

}


PlayerConst.prototype.update = function update(){
this.score++;
if(this.score > this.highScore){
this.highScore = this.score;
}

PlayerConst.prototype.lives = function lives(){
this.addLife = addLife++;
this.subtractLife = subtractLife--;
}

new PlayerConst.prototype.lives(3);

Link to comment
Share on other sites

What you've just done is create a lives method that attempts to modify addLives and subtractLives properties.

 

I've asked you before to look up the definitions of these terms, I'll just put them right here:

  • A property is a value that's associated to an object. It's like a variable, but attached to the object.
  • A method is a function that's attached to an object. It's able to manipulate properties that belong to that object.
  • A constructor is the function that creates the object (That's how it works in Javascript, in real Object-Oriented programming the definition is a bit different)
  • Objects are created using the new keyword. If you run the constructor function without the new keyword it's not a constructor and won't give you an object.

And now, I'll just solve your problem for you. This is going to cause you more harm than good, since when you encounter a situation like this in real life you won't have anybody to help you and you'll lose your job.

function PlayerConst(userName, score, highScore) {
  this.username = userName; 
  this.score = score;
  this.highScore = highScore;

  // Lives PROPERTY
  this.lives = 3;
}

PlayerConst.prototype.update = function(){
  this.score++;
  if(this.score > this.highScore){
    this.highScore = this.score;
  }
}

// addLife METHOD
PlayerConst.prototype.addLife() {
  this.lives++;
}

// subtractLife METHOD
PlayerConst.prototype.subtractLife() {
  this.lives--;
}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...