2 * Copyright (c) 2012, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
23 * FlashCards() class contains all the variables and functions needed to run the flash card game
26 function FlashCards() {
28 COLORDECK: "ColorDeck",
29 SHAPEDECK: "ShapeDeck",
30 SPANISHDECK: "SpanishDeck",
31 COUNTINGDECK: "CountingDeck"
33 this.cardCount = 0; //current game wrong answers
34 this.rightCount = 0; //current game right answers
35 this.cardSet = this.cardDecks.SHAPEDECK; //name of card set and file prefix
36 this.endGameFlag = false; //endGame flag
37 this.deckAnswer = []; //the array of answers for this deck
41 * FlashCards.helpClicked() plays audio and makes the help card dialog visible when help icon is clicked
44 FlashCards.prototype.helpClicked = function () {
45 this.buttonClickSound.play();
46 document.getElementById("help-dialog").style.display = "inline";
50 * FlashCards.helpCloseClicked() plays audio and makes the help card dialog invisible when help X icon is clicked
53 FlashCards.prototype.helpCloseClicked = function () {
54 this.buttonClickSound.play();
55 document.getElementById("help-dialog").style.display = "none";
59 * FlashCards.hideAnswer() will hide the wrong and right buttons and answer text reseting the cursor style
62 FlashCards.prototype.hideAnswer = function () {
63 document.getElementById("answer").style.display = "none";
67 * FlashCards.clear() will reset game counters, hide stars, replay button, answers, and reset the score
70 FlashCards.prototype.clear = function () {
71 var parent = document.getElementById("game-screen"),
76 this.endGameFlag = false;
80 //remove all stars from dom
81 children = document.getElementsByClassName("star");
82 for (count = children.length - 1; count >= 0; count = count - 1) {
83 parent.removeChild(document.getElementsByClassName("star")[count]);
86 document.getElementById("score-number").innerHTML = "0";
87 document.getElementById("replay-button").style.display = "none";
88 document.getElementById("endgame-prompt").style.display = "none";
93 * FlashCards.navPaneClose() closes nav pane and plays nav pane sound effect
96 FlashCards.prototype.navPaneClose = function () {
97 this.swooshSound.play();
98 if (document.getElementsByClassName("animation-open")[0]) {
99 document.getElementsByClassName("animation-open")[0].setAttribute("class", "animation-close");
104 * FlashCards.navPaneToggle() opens or closes nav pane and plays nav pane sound effect
107 FlashCards.prototype.navPaneToggle = function () {
108 this.swooshSound.play();
109 if (document.getElementsByClassName("animation-open")[0]) {
110 document.getElementsByClassName("animation-open")[0].setAttribute("class", "animation-close");
112 document.getElementsByClassName("animation-close")[0].setAttribute("class", "animation-open");
117 * FlashCards.navPaneClicked() plays button click audio, opens or closes nav pane and plays nav pane sound effect
120 FlashCards.prototype.navPaneClicked = function () {
121 this.buttonClickSound.play();
122 this.navPaneToggle();
126 * FlashCards.getShapeDeckAnswers() will call getMessage to get the localized answers for this deck
129 FlashCards.prototype.getShapeDeckAnswers = function () {
130 this.shapeAnswer = [];
131 this.shapeAnswer[0] = getMessage("circle");
132 this.shapeAnswer[1] = getMessage("square");
133 this.shapeAnswer[2] = getMessage("triangle");
134 this.shapeAnswer[3] = getMessage("oval");
135 this.shapeAnswer[4] = getMessage("star");
136 this.shapeAnswer[5] = getMessage("octagon");
137 this.shapeAnswer[6] = getMessage("rectangle");
138 this.shapeAnswer[7] = getMessage("trapezoid");
142 * FlashCards.setCardContent() sets the image centered horizontally in the card
145 FlashCards.prototype.setCardContent = function () {
146 var img = new Image();
147 img.src = 'images/' + this.cardSet + this.cardCount + '.png';
148 document.getElementById("card").style.display = "none";
149 img.onload = function(){
150 document.getElementById("card-graphic").style.width = img.width+"px";
151 document.getElementById("card-graphic").style.height = img.height+"px";
152 document.getElementById("card-graphic").style.top = (160-(img.height/2))+"px";
153 document.getElementById("card-graphic").style.backgroundImage = 'url('+ img.src +')';
154 document.getElementById("card").style.display = "inline";
159 * FlashCards.shapeDeckClicked() set the game to play through the this deck
162 FlashCards.prototype.setShapeDeck = function () {
163 this.whipCrackSound.play();
165 this.cardSet = this.cardDecks.SHAPEDECK;
166 this.deckAnswer = this.shapeAnswer;
167 this.setCardContent();
168 document.getElementById("nav-flag").innerHTML = getMessage("shapes");
169 document.getElementById("card-answer").style.color = "#499aff";
170 document.getElementById("card-answer").innerHTML = this.deckAnswer[this.cardCount];
174 * FlashCards.shapeDeckClicked() plays the button click audio, navPane animation, and calls the setter for this deck
177 FlashCards.prototype.shapeDeckClicked = function () {
178 this.buttonClickSound.play();
179 this.navPaneClicked();
184 * FlashCards.getCountingDeckAnswers() will call getMessage to get the localized answers for this deck
187 FlashCards.prototype.getCountingDeckAnswers = function () {
188 this.countingAnswer = [];
189 this.countingAnswer[0] = getMessage("one");
190 this.countingAnswer[1] = getMessage("two");
191 this.countingAnswer[2] = getMessage("three");
192 this.countingAnswer[3] = getMessage("four");
193 this.countingAnswer[4] = getMessage("five");
194 this.countingAnswer[5] = getMessage("six");
195 this.countingAnswer[6] = getMessage("seven");
196 this.countingAnswer[7] = getMessage("eight");
197 this.countingAnswer[8] = getMessage("nine");
198 this.countingAnswer[9] = getMessage("ten");
202 * FlashCards.setCountingDeck() set the game to play through the this deck
205 FlashCards.prototype.setCountingDeck = function () {
206 this.whipCrackSound.play();
208 this.cardSet = this.cardDecks.COUNTINGDECK;
209 this.deckAnswer = this.countingAnswer;
210 this.setCardContent();
211 document.getElementById("nav-flag").innerHTML = getMessage("counting");
212 document.getElementById("card-answer").style.color = "navy";
213 document.getElementById("card-answer").innerHTML = this.deckAnswer[this.cardCount];
217 * FlashCards.countingDeckClicked() plays the button click audio, navPane animation, and calls the setter for this deck
220 FlashCards.prototype.countingDeckClicked = function () {
221 this.buttonClickSound.play();
222 this.navPaneClicked();
223 this.setCountingDeck();
227 * FlashCards.getSpanishDeckAnswers() will call getMessage to get the localized answers for this deck
230 FlashCards.prototype.getSpanishDeckAnswers = function () {
231 this.spanishAnswer = [];
232 this.spanishAnswer[0] = getMessage("sun");
233 this.spanishAnswer[1] = getMessage("cloud");
234 this.spanishAnswer[2] = getMessage("cat");
235 this.spanishAnswer[3] = getMessage("dog");
236 this.spanishAnswer[4] = getMessage("tree");
237 this.spanishAnswer[5] = getMessage("fish");
238 this.spanishAnswer[6] = getMessage("fruit");
239 this.spanishAnswer[7] = getMessage("worm");
240 this.spanishAnswer[8] = getMessage("bird");
241 this.spanishAnswer[9] = getMessage("crocodile");
242 this.spanishAnswer[10] = getMessage("book");
243 this.spanishAnswer[11] = getMessage("socks");
244 this.spanishAnswer[12] = getMessage("cup");
245 this.spanishAnswer[13] = getMessage("chair");
249 * FlashCards.setSpanishDeck() set the game to play through the this deck
252 FlashCards.prototype.setSpanishDeck = function () {
253 this.whipCrackSound.play();
255 this.cardSet = this.cardDecks.SPANISHDECK;
256 this.deckAnswer = this.spanishAnswer;
257 this.setCardContent();
258 document.getElementById("nav-flag").innerHTML = getMessage("spanish");
259 document.getElementById("card-answer").style.color = "#000000";
260 document.getElementById("card-answer").innerHTML = this.deckAnswer[this.cardCount];
264 * FlashCards.spanishDeckClicked() plays the button click audio, navPane animation, and calls the setter for this deck
267 FlashCards.prototype.spanishDeckClicked = function () {
268 this.buttonClickSound.play();
269 this.navPaneClicked();
270 this.setSpanishDeck();
274 * FlashCards.getColorDeckAnswers() will call getMessage to get the localized answers for this deck
277 FlashCards.prototype.getColorDeckAnswers = function () {
278 this.colorAnswer = [];
279 this.colorAnswer[0] = getMessage("red");
280 this.colorAnswer[1] = getMessage("purple");
281 this.colorAnswer[2] = getMessage("blue");
282 this.colorAnswer[3] = getMessage("green");
283 this.colorAnswer[4] = getMessage("orange");
284 this.colorAnswer[5] = getMessage("yellow");
285 this.colorAnswer[6] = getMessage("black");
286 this.colorAnswer[7] = getMessage("pink");
287 this.colorAnswer[8] = getMessage("white");
288 this.colorAnswer[9] = getMessage("brown");
289 this.colorAnswer[10] = getMessage("grey");
293 * FlashCards.getColorDeckColors() will set the text colors for each card in this deck
296 FlashCards.prototype.getColorDeckColors = function () {
298 this.colorHex[0] = "#DD0000"; //red
299 this.colorHex[1] = "#7E0B80"; //purple
300 this.colorHex[2] = "#0000ff"; //blue
301 this.colorHex[3] = "#00BC00"; //green
302 this.colorHex[4] = "#DA8D00"; //orange
303 this.colorHex[5] = "#FEF600"; //yellow
304 this.colorHex[6] = "#000000"; //black
305 this.colorHex[7] = "#FF00FF"; //pink
306 this.colorHex[8] = "#FFFFFF"; //white
307 this.colorHex[9] = "#996600"; //brown
308 this.colorHex[10] = "#C0C0C0"; //grey
312 * FlashCards.setColorDeck set the game to play through the this deck
315 FlashCards.prototype.setColorDeck = function () {
316 this.whipCrackSound.play();
318 this.cardSet = this.cardDecks.COLORDECK;
319 this.deckAnswer = this.colorAnswer;
320 this.setCardContent();
321 document.getElementById("nav-flag").innerHTML = getMessage("colors");
322 document.getElementById("card-answer").innerHTML = this.colorAnswer[this.cardCount];
323 document.getElementById("card-answer").style.color = this.colorHex[this.cardCount];
327 * FlashCards.colorDeckClicked() plays the button click audio, navPane animation, and calls the setter for this deck
330 FlashCards.prototype.colorDeckClicked = function () {
331 this.buttonClickSound.play();
332 this.navPaneClicked();
337 * FlashCards.initGame() gets the localized strings for the game play screen, sets the default deck to Shapes
340 FlashCards.prototype.initGame = function () {
342 this.backgroundSound.play();
344 document.getElementById("score-text").innerHTML = getMessage("scoreText");
345 document.getElementById("help-text").innerHTML = getMessage("helpText");
347 this.setGameEventListeners();
349 this.getColorDeckAnswers();
350 this.getColorDeckColors();
351 this.getShapeDeckAnswers();
352 this.getCountingDeckAnswers();
353 this.getSpanishDeckAnswers();
359 * FlashCards.playNowClicked() will initialize the game screen when Play Now button is clicked and hide the splash screen
362 FlashCards.prototype.playNowClicked = function () {
364 document.getElementById("splash-screen").style.display = "none";
365 document.getElementById("game-screen").style.display = "inline";
369 * FlashCards.isLastCard()
371 * @return true if card count is equal to the number of cards in the deck, else return false
373 FlashCards.prototype.isLastCard = function () {
374 if ((this.deckAnswer.length) === this.cardCount) {
381 * FlashCards.drawStars() draws the stars for right and wrong answers
384 FlashCards.prototype.drawStars = function () {
389 //paint correct stars equal to rightCount
390 for (count = 1; count <= this.rightCount; count = count + 1) {
391 star = document.createElement("img");
392 star.setAttribute('src', 'images/StarFilled.png');
393 star.setAttribute('id', ('star' + count));
394 star.setAttribute('class', ('star'));
395 container = document.getElementById("game-screen");
396 container.appendChild(star);
399 //paint remaining stars as empty stars
400 for (count = (this.rightCount + 1); count <= (this.deckAnswer.length); count = count + 1) {
401 star = document.createElement("img");
402 star.setAttribute('src', 'images/StarEmpty.png');
403 star.setAttribute('id', ('star' + count));
404 star.setAttribute('class', ('star'));
405 container = document.getElementById("game-screen");
406 container.appendChild(star);
411 * FlashCards.endGame() opens nav pane, plays audio sound for end of game, shows correct star score and replay button
414 FlashCards.prototype.endGame = function () {
415 this.trumpetFanfareSound.play();
416 this.endGameFlag = true;
418 document.getElementById("card").style.display = "none";
419 document.getElementById("card-flip").setAttribute("id", "card-graphic");
423 //if user got more right than wrong then show localized strings for "good job" else show localized strings for "try again"
424 if (((this.deckAnswer.length) - this.rightCount) < this.rightCount) {
425 document.getElementById("endgame-prompt").innerHTML = getMessage("goodJob");
427 document.getElementById("endgame-prompt").innerHTML = getMessage("tryAgain");
430 //set card answer color and display replay button
431 document.getElementById("endgame-prompt").style.display = "inline";
432 document.getElementById("replay-button").style.display = "inline";
436 * FlashCards.flipCard() hide answers and moves to next card in the deck, increments counters and checks for end game state
439 FlashCards.prototype.flipCard = function () {
441 document.getElementById("card-graphic").setAttribute("id", "card-flip");
442 this.cardCount = this.cardCount + 1;
443 if (!this.isLastCard()) {
444 this.cardFlipSound.play();
445 this.setCardContent();
446 document.getElementById("card-answer").innerHTML = this.deckAnswer[this.cardCount];
447 if (this.cardSet === this.cardDecks.COLORDECK) {
448 document.getElementById("card-answer").style.color = this.colorHex[this.cardCount];
450 document.getElementById("card-flip").setAttribute("id", "card-graphic");
457 * FlashCards.wrongButtonClicked() plays audio sounds, increments wrongCount and calls flipCard()
460 FlashCards.prototype.wrongButtonClicked = function () {
461 this.wrongAnswerSound.play();
462 this.buttonClickSound.play();
467 * FlashCards.rightButtonClicked() plays audio sounds, increments rightCount and calls flipCard()
470 FlashCards.prototype.rightButtonClicked = function () {
471 this.rightAnswerSound.play();
472 this.buttonClickSound.play();
473 this.rightCount = this.rightCount + 1;
475 document.getElementById("score-number").innerHTML = this.rightCount;
479 * FlashCards.replayButtonClicked() restarts current game and calls clear()
482 FlashCards.prototype.replayButtonClicked = function () {
483 if (this.cardSet === this.cardDecks.COLORDECK) {
484 this.colorDeckClicked();
485 } else if (this.cardSet === this.cardDecks.SHAPEDECK) {
486 this.shapeDeckClicked();
487 } else if (this.cardSet === this.cardDecks.SPANISHDECK) {
488 this.spanishDeckClicked();
489 } else if (this.cardSet === this.cardDecks.COUNTINGDECK) {
490 this.countingDeckClicked();
496 * FlashCards.showAnswer() will show right and wrong buttons, answer and set cursor type
499 FlashCards.prototype.showAnswer = function () {
500 document.getElementById("answer").style.display = "inline";
504 * FlashCards.cardClicked() plays button click sound, makes sure the nav pane is closed, and if not end game state will call showAnswer()
507 FlashCards.prototype.cardClicked = function () {
508 this.buttonClickSound.play();
510 if (!this.endGameFlag) {
516 * FlashCards.scrollNavPane will scroll the navigation Pane the direction the user moves the mouse
519 FlashCards.prototype.scrollNavPane = function (delta) {
520 document.getElementById("scroll-items").style.webkitTransform = "translateY("+delta+"px)";
524 * FlashCards.setGameEventListeners sets event handlers for the game
527 FlashCards.prototype.setGameEventListeners = function () {
529 starty = 0, //starting y coordinate of drag
530 isDrag = -1; //flag for qualifying drag event
532 document.getElementById("nav-pane").addEventListener('click', function () {
533 self.navPaneClicked();
535 document.getElementById("scroll-overlay").addEventListener('mousedown', function (e) {
537 isDrag = 0; //mouse down
539 document.getElementById("scroll-overlay").addEventListener('mousemove', function (e) {
540 isDrag = 1; //mouse move
542 document.getElementById("scroll-overlay").addEventListener('mouseup', function (e) {
543 if(isDrag === 1) { //if equals 1 is drag event
544 self.scrollNavPane((-1)*(starty-e.clientY));
546 isDrag = -1; //regardless reset endy
548 document.getElementById("shape-deck").addEventListener('click', function () {
549 self.shapeDeckClicked();
551 document.getElementById("color-deck").addEventListener('click', function () {
552 self.colorDeckClicked();
554 document.getElementById("counting-deck").addEventListener('click', function () {
555 self.countingDeckClicked();
557 document.getElementById("spanish-deck").addEventListener('click', function () {
558 self.spanishDeckClicked();
560 document.getElementById("card").addEventListener('click', function () {
563 document.getElementById("card-answer").addEventListener('click', function () {
566 document.getElementById("wrong-button").addEventListener('click', function () {
567 self.wrongButtonClicked();
569 document.getElementById("right-button").addEventListener('click', function () {
570 self.rightButtonClicked();
572 document.getElementById("replay-button").addEventListener('click', function () {
573 self.replayButtonClicked();
575 document.getElementById("help-icon").addEventListener('click', function () {
578 document.getElementById("help-close").addEventListener('click', function () {
579 self.helpCloseClicked();
584 * FlashCards.setSplashScreenEventListeners sets event handlers for the splash screen
587 FlashCards.prototype.setSplashScreenEventListeners = function () {
589 this.buttonClickSound = new GameSound("sound-buttonclick", "audio/GeneralButtonClick_wav.ogg", "none");
590 document.getElementById("play-button").addEventListener('click', function () {
591 self.buttonClickSound.play();
592 self.playNowClicked();
597 * FlashCards.initSound will initialize all the sound id's for each sound file
600 FlashCards.prototype.initSound = function () {
601 this.cardFlipSound = new GameSound("sound-cardflip", "audio/CardFlip.ogg", "none");
602 this.backgroundSound = new GameSound("sound-background", "audio/GameplayBackgroundAtmospheric_Loop.ogg", "none", true);
603 this.swooshSound = new GameSound("sound-navpane", "audio/NavPaneSwoosh.ogg", "none");
604 this.trumpetFanfareSound = new GameSound("sound-end", "audio/Replay.ogg", "none");
605 this.rightAnswerSound = new GameSound("sound-starbutton", "audio/StarButton.ogg", "none");
606 this.wrongAnswerSound = new GameSound("sound-thumbbutton", "audio/ThumbsDown.ogg", "none");
607 this.whipCrackSound = new GameSound("sound-begin", "audio/WhipCrackBegin.ogg", "none");
611 * FlashCards.init() will set the license, play intro sound, and set splash screen text
614 FlashCards.prototype.init = function () {
615 document.getElementById("app-name").innerHTML = getMessage("appName");
616 document.getElementById("adventure-ribbon").innerHTML = getMessage("adventureText");
617 document.getElementById("cards-ribbon").innerHTML = getMessage("cardsText");
618 document.getElementById("play-button").innerHTML = getMessage("playButtonText");
619 this.adventureThemeSound = new GameSound("sound-intro", "audio/Theme_Loop.ogg", "auto", true);
620 this.adventureThemeSound.play();
621 this.setSplashScreenEventListeners();
622 license_init("license", "splash-screen");
625 window.addEventListener('load', function () {
626 var App = new FlashCards();
628 //hack to get active state to work on webkit
629 this.touchstart = function (e) {};