HTML and CSS Reference
In-Depth Information
3. Now, before we restart the game every ime, we randomize the power value. It
provides diferent values on every round of card selecion:
gameScene.restartGame = function() {
game.randomizePower();
// rest of the existing code in restartGame.
}
4. Now, we mark the selected card as a new Card instance, and then check the power
of the card after logging into the game:
/* select a card */
selectedCard = new Card(elm);
console.log("Power of selected card: ", selectedCard.power());
// rest of original code for card selection.
Objective complete - mini debriefing
We have added a power value to the cards. These values will be used for comparison when
we add the batle logic later.
Randomize logic
The game randomizes the power values in all cards for every round when the game begins.
It is a separate logic that selects all the power elements and adds a randomized value ranging
from 40 to 100 to it. After we get the value, we use the textContent property to set the
string value to the card node.
Card definition
Most of the card logic is done on the HTML node directly, such as the toggling classes. But the
power is stored in the node as a string type. We want to get the integer type of the power
value to make the batle calculaions easier. That's why we create a Card object deiniion.
The Card object contains the original HTML node reference and a power method to get the
integer power value from the text content of the HTML node.
The reason why we only instaniate the selected card is because we don't read any power
value of the non-selected card.
The official way to convert a string to an integer is the parseInt method.
But the fastest way is to muliply the string by 1. The following URL tests
the performance between these two methods:
http://jsperf.com/parseint-vs-x1
 
Search WWH ::




Custom Search