I'm currently developing a text-based game using JavaScript that involves picking up a sword with the help of an in-game function.
var takeSword = function() {
if (currentRoom.hasSword) {
$("<p>You picked up a sword.</p>").properDisplay();
}
else {
$("<p>The sword is not here.</p>").properDisplay();
}
};
The issue I'm facing is that it's possible to repeatedly pick up the sword while remaining in the same room. How can I modify the function so that once the sword is collected, it cannot be picked up again?
Initially, my approach was to use a variable like var sword = false;
and then update it to sword = true;
within the function, but this did not prove effective.
This code snippet doesn't cover the entire script; there's an object defined earlier that sets 'hasSword = true;', allowing the sword to be obtained initially but preventing it from being picked up in other areas of the game.