var app = function (){ this . ui = new ui ( this ); this . network = new network ( this ); this . data = []; this . level = 0 ; this . question = null ; var self = this ; // Boot all first time load this . boot = function (){ this . ui . setScene ( ' welcome ' ); this . ui . listenEvents (); this . loadData (); } // Load data from api this . loadData = function (){ this . network . get ( DATA_URL , function ( data ){ self . data = data ; self . ui . hide ( ' loadingText ' ); self . ui . show ( ' startButton ' ); }, function ( error ){ alert ( error ); } ) } // Preload an image this . loadImage = function (){ this . ui . show ( ' imageLoading ' ); this . network . loadImage ( self . data [ self . level ]. image , function ( image ){ self . ui . hide ( ' imageLoading ' ); self . ui . showImage ( image ); } ); } // Start new game this . start = function (){ this . ui . setScene ( ' image ' ); this . loadImage (); } // Pick a ramdom question then show it this . pickAndShowQuestion = function (){ // Pick a random question var questions = this . data [ this . level ]. questions ; var random = Math . round ( Math . random () * ( questions . length - 1 )); this . question = questions [ random ]; // Show it this . ui . showQuestion ( this . question ); this . ui . setScene ( ' question ' ); } // Check for answer on button click this . checkAnswer = function ( answer ){ if ( answer == this . question . correct ){ this . nextLevel (); } else { this . ui . setScene ( ' gameOver ' ); } } // Next level this . nextLevel = function (){ if ( this . level == this . data . length - 1 ){ alert ( " Bạn đã chiến thắng " ); this . restart (); return ; } this . level ++ ; this . ui . updateLevelNumber (); this . ui . setScene ( ' image ' ); this . loadImage (); } // Restart game this . restart = function (){ this . level = 0 ; this . ui . updateLevelNumber (); this . ui . setScene ( ' welcome ' ); } }