Reputation: 21
Begginer here.
var player() = {
isDrunk: false
}
How do I set isDrunk to true after the player uses item "Beer"? (Completely made-up example)
Upvotes: 1
Views: 70
Reputation: 570
It looks like you want to create an object that can invoke an action to "drink" a beer. The following is the newer class syntax (ES6) which is not widely supported in older browsers, but worth a study.
class Player {
constructor(){
this.isDrunk = false;
}
useBeer(){
this.isDrunk = true;
}
isDrunk(){
return this.isDrunk;
}
}
var player = new Player();
player.useBeer();
console.log(player.isDrunk()); // true
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
Upvotes: 1
Reputation: 1544
You can set the value of isDrunk either using switch or if statement, anything of your choice. But before that kindly have a look at the syntax or way of creating objects.
var player = {isDrunk:false, hadBeer:false}
if (player.hadBeer) {
player.isDrunk = true;
} else {
player.isDrunk = false;
}
Upvotes: 0
Reputation: 1533
To specifically answer your question you would just do something like player.isDrunk = true
So you could have something like
var player = {
leftHanded: false,
rightHanded: true,
isDrunk: false
};
And then
function drinkBeer(player) {
player.isDrunk = true;
}
Then you can pass your players to your drinkBeer function as needed. Alternatively you can put the function inside the player object if you want each individual player to have a drinkBeer()
function that they can use to change their own isDrunk
property by calling player.drinkBeer()
That would look like this:
var player = {
leftHanded: false,
rightHanded: true,
isDrunk: false,
drinkBeer: function() {
isDrunk = true;
}
};
Upvotes: 3
Reputation: 835
You should google your question before you ask it because documentation for JavaScript objects is everywhere... But...
var player = {
myFirstValue: true,
mySecondValue: false
};
player.myFirstValue = false;
Upvotes: 1