squareTwo.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. // Fractions Comparison Square states
  2. /****************************** MENU ****************************/
  3. var menuSquareTwo={
  4. create: function() {
  5. // Creating sound variable
  6. var beepSound = game.add.audio('sound_beep');
  7. // Reading dictionary
  8. var words = game.cache.getJSON('dictionary');
  9. // Menu options
  10. //information label
  11. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  12. // Return to language button
  13. m_world = game.add.sprite(10, 10, 'world');
  14. m_world.inputEnabled = true;
  15. m_world.input.useHandCursor = true;
  16. m_world.events.onInputDown.add(this.loadState, {state: "boot", beep: beepSound});
  17. m_world.events.onInputOver.add(this.showOption, {message: words.menu_world});
  18. m_world.events.onInputOut.add(this.showOption, {message: ""});
  19. // Return to menu button
  20. m_list = game.add.sprite(60, 10, 'list');
  21. m_list.inputEnabled = true;
  22. m_list.input.useHandCursor = true;
  23. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  24. m_list.events.onInputOver.add(this.showOption, {message: words.menu_list});
  25. m_list.events.onInputOut.add(this.showOption, {message: ""});
  26. // Setting title
  27. var style = { font: '28px Arial', fill: '#00804d'};
  28. var title = game.add.text(860, 40, words.game_menu_title, style);
  29. title.anchor.setTo(1, 0.5);
  30. //Showing Games and Levels
  31. var maxHeight = 120; //Max height of a stair
  32. var stairHeight = 29; //height growth of a stair
  33. var stairWidth = 80; //Width of a stair
  34. var startStair = 240;
  35. var startSymbol = 150;
  36. var startSquare = (startSymbol/2)+startStair+stairWidth*5;
  37. var bplus = game.add.sprite(startSymbol, 300, 'equal');
  38. bplus.frame = 0;
  39. bplus.scale.setTo(0.7);
  40. bplus.anchor.setTo(0.5,0.5);
  41. //First stairs, 6 levels
  42. var stairsA = [];
  43. for(var i=1;i<=5;i++){
  44. //stair
  45. var x1 = startStair+(stairWidth*(i-1));
  46. var y1 = 100+maxHeight-i*stairHeight;
  47. var x2 = stairWidth;//x1 + 40;
  48. var y2 = stairHeight*i;//y1 + 24;
  49. stairsA[i] = game.add.graphics(0, 0);
  50. stairsA[i].lineStyle(1, 0xFFFFFF, 1);
  51. stairsA[i].beginFill(0x99b3ff);
  52. stairsA[i].drawRect(x1, y1, x2, y2);
  53. stairsA[i].endFill();
  54. //event
  55. stairsA[i].inputEnabled = true;
  56. stairsA[i].input.useHandCursor = true;
  57. stairsA[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, type: 'A' });
  58. stairsA[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  59. stairsA[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  60. //label
  61. var xl = x1+stairWidth/2; //x label
  62. var yl = y1+(stairHeight*i)/2; //y label
  63. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  64. label.anchor.setTo(0.5, 0.4);
  65. }
  66. var stairsB = [];
  67. for(var i=1;i<=5;i++){
  68. //stair
  69. var x1 = startStair+(stairWidth*(i-1));
  70. var y1 = 270+maxHeight-i*stairHeight;
  71. var x2 = stairWidth;//x1 + 40;
  72. var y2 = stairHeight*i;//y1 + 24;
  73. stairsB[i] = game.add.graphics(0, 0);
  74. stairsB[i].lineStyle(1, 0xFFFFFF, 1);
  75. stairsB[i].beginFill(0xff6666);
  76. stairsB[i].drawRect(x1, y1, x2, y2);
  77. stairsB[i].endFill();
  78. //event
  79. stairsB[i].inputEnabled = true;
  80. stairsB[i].input.useHandCursor = true;
  81. stairsB[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, type: 'B' });
  82. stairsB[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  83. stairsB[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  84. //label
  85. var xl = x1+stairWidth/2; //x label
  86. var yl = y1+(stairHeight*i)/2; //y label
  87. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  88. label.anchor.setTo(0.5, 0.4);
  89. }
  90. var stairsC = [];
  91. for(var i=1;i<=5;i++){
  92. //stair
  93. var x1 = startStair+(stairWidth*(i-1));
  94. var y1 = 440+maxHeight-i*stairHeight;
  95. var x2 = stairWidth;//x1 + 40;
  96. var y2 = stairHeight*i;//y1 + 24;
  97. stairsC[i] = game.add.graphics(0, 0);
  98. stairsC[i].lineStyle(1, 0xFFFFFF, 1);
  99. stairsC[i].beginFill(0xb366ff);
  100. stairsC[i].drawRect(x1, y1, x2, y2);
  101. stairsC[i].endFill();
  102. //event
  103. stairsC[i].inputEnabled = true;
  104. stairsC[i].input.useHandCursor = true;
  105. stairsC[i].events.onInputDown.add(this.loadMap, {beep: beepSound, difficulty: i, type: 'C' });
  106. stairsC[i].events.onInputOver.add(function (item) { item.alpha=0.5; }, this);
  107. stairsC[i].events.onInputOut.add(function (item) { item.alpha=1; }, this);
  108. //label
  109. var xl = x1+stairWidth/2; //x label
  110. var yl = y1+(stairHeight*i)/2; //y label
  111. var label = game.add.text(xl, yl, i, { font: '25px Arial', fill: '#ffffff', align: 'center' });
  112. label.anchor.setTo(0.5, 0.4);
  113. }
  114. },
  115. //Navigation functions,
  116. showOption: function(){
  117. m_info.text = this.message;
  118. },
  119. loadState: function(){
  120. this.beep.play();
  121. game.state.start(this.state);
  122. },
  123. //MapLoading function
  124. loadMap: function(){
  125. this.beep.play();
  126. twoPosition = 0; //Map position
  127. twoMove = true; //Move no next point
  128. twoDifficulty = this.difficulty; //Number of difficulty (1 to 5)
  129. twoType = this.type; //Operator of game
  130. if(twoPosition<5){
  131. game.state.start('mapSTwo');
  132. }else{
  133. game.state.start('unofinal');
  134. }
  135. }
  136. };
  137. /****************************** MAP ****************************/
  138. var mapSquareTwo={
  139. create: function() {
  140. // Creating sound variable
  141. beepSound = game.add.audio('sound_beep');
  142. // Reading dictionary
  143. var words = game.cache.getJSON('dictionary');
  144. // Background
  145. game.add.image(0, 40, 'bgmap');
  146. if(oneMenu){
  147. // Menu options
  148. //information label
  149. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  150. // Return to language button
  151. m_world = game.add.sprite(10, 10, 'world');
  152. m_world.inputEnabled = true;
  153. m_world.input.useHandCursor = true;
  154. m_world.events.onInputDown.add(this.loadState, {state: "boot", beep: beepSound});
  155. m_world.events.onInputOver.add(this.showOption, {message: words.menu_world});
  156. m_world.events.onInputOut.add(this.showOption, {message: ""});
  157. // Return to menu button
  158. m_list = game.add.sprite(60, 10, 'list');
  159. m_list.inputEnabled = true;
  160. m_list.input.useHandCursor = true;
  161. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  162. m_list.events.onInputOver.add(this.showOption, {message: words.menu_list});
  163. m_list.events.onInputOut.add(this.showOption, {message: ""});
  164. // Return to diffculty
  165. m_back = game.add.sprite(110, 10, 'back');
  166. m_back.inputEnabled = true;
  167. m_back.input.useHandCursor = true;
  168. m_back.events.onInputDown.add(this.loadState, {state: "menuSTwo", beep: beepSound});
  169. m_back.events.onInputOver.add(this.showOption, {message: words.menu_back});
  170. m_back.events.onInputOut.add(this.showOption, {message: ""});
  171. }
  172. // Styles for labels
  173. var stylePlace = { font: '26px Arial', fill: '#ffffff', align: 'center'};
  174. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  175. // Progress bar
  176. var percentText = onePosition*20;
  177. var percentBlocks = onePosition;
  178. for(var p=1;p<=percentBlocks;p++){
  179. var block = game.add.image(680+(p-1)*30, 10, 'block');
  180. block.scale.setTo(2, 1); //Scaling to double width
  181. }
  182. game.add.text(840, 10, percentText+'%', styleMenu);
  183. game.add.text(670, 10, words.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  184. game.add.image(680, 10, 'pgbar');
  185. //Road
  186. this.points = {
  187. 'x': [ 90, 204, 318, 432, 546, 660 ],
  188. 'y': [ 486, 422, 358, 294, 230, 166 ]
  189. };
  190. //Road
  191. this.points = {
  192. 'x': [ 90, 204, 318, 432, 546, 660 ],
  193. 'y': [ 486, 422, 358, 294, 230, 166 ]
  194. };
  195. //House
  196. var house = game.add.image(this.points.x[0], this.points.y[0], 'house');
  197. house.scale.setTo(0.7);
  198. house.anchor.setTo(0.7, 0.8);
  199. //School
  200. var school = game.add.image(this.points.x[5], this.points.y[5], 'school');
  201. school.scale.setTo(0.35);
  202. school.anchor.setTo(0.2, 0.7);
  203. //Trees and Rocks
  204. this.rocks = {
  205. 'x': [156, 275, 276, 441, 452, 590, 712],
  206. 'y': [309, 543, 259, 156, 419, 136, 316]
  207. }
  208. this.r_types = [1, 1, 2, 1, 2, 2, 2];
  209. for(var i=0; i<this.r_types.length; i++){
  210. if(this.r_types[i]==1){
  211. var sprite = game.add.image(this.rocks.x[i], this.rocks.y[i], 'rock');
  212. sprite.scale.setTo(0.32);
  213. sprite.anchor.setTo(0.5, 0.95);
  214. }else if(this.r_types[i]==2){
  215. var sprite = game.add.image(this.rocks.x[i], this.rocks.y[i], 'birch');
  216. sprite.scale.setTo(0.4);
  217. sprite.anchor.setTo(0.5, 0.95);
  218. }
  219. }
  220. this.trees = {
  221. 'x': [105, 214, 354, 364, 570, 600, 740, 779],
  222. 'y': [341, 219, 180, 520, 550, 392, 488, 286]
  223. }
  224. this.t_types = [2, 4, 3, 4, 1, 2, 4, 4];
  225. for(var i=0; i<this.t_types.length; i++){
  226. var sprite = game.add.image(this.trees.x[i], this.trees.y[i], 'tree'+this.t_types[i]);
  227. sprite.scale.setTo(0.6);
  228. sprite.anchor.setTo(0.5, 0.95);
  229. }
  230. // places
  231. for (var p = 1; p < this.points.x.length -1; p++){
  232. var place;
  233. if(p<twoPosition)
  234. place = game.add.image(this.points.x[p], this.points.y[p], 'place_b');
  235. else if (twoMove && p==twoPosition)
  236. place = game.add.image(this.points.x[p], this.points.y[p], 'place_b');
  237. else
  238. place = game.add.image(this.points.x[p], this.points.y[p], 'place_a');
  239. place.anchor.setTo(0.5, 0.5);
  240. place.scale.setTo(0.3);
  241. var sign = game.add.image(this.points.x[p]-20, this.points.y[p]-60, 'sign');
  242. sign.anchor.setTo(0.5, 1);
  243. sign.scale.setTo(0.4);
  244. if(p>0 && p<this.points.x.length-1){
  245. var text = game.add.text(this.points.x[p]-23, this.points.y[p]-84, p, stylePlace);
  246. text.anchor.setTo(0.35, 0.5);
  247. }
  248. }
  249. // Kid start position
  250. this.kid = game.add.sprite(this.points.x[twoPosition], this.points.y[twoPosition], 'kid_run');
  251. this.kid.anchor.setTo(0.5,1);
  252. this.kid.scale.setTo(0.5);
  253. game.physics.arcade.enable(this.kid);
  254. this.kid.animations.add('run');
  255. this.kid.animations.play('run', 6, true);
  256. // Delay to next level
  257. this.count = 0;
  258. this.wait = 60;
  259. },
  260. update: function() {
  261. // Wait before moving or staring a game
  262. this.count ++;
  263. if(this.count<=this.wait) return;
  264. // If movement is stopped or position is 5 (final), load game
  265. if(twoPosition==5){
  266. twoMove = false;
  267. }
  268. if(!twoMove){
  269. this.loadGame();
  270. }
  271. // If momevent is enabled, move to next point from actual
  272. if(twoMove){
  273. game.physics.arcade.moveToXY(
  274. this.kid,
  275. this.points.x[twoPosition+1],
  276. this.points.y[twoPosition+1],
  277. 100
  278. );
  279. // I tractor reached the end, stop movement
  280. if(Math.ceil(this.kid.x)==this.points.x[twoPosition+1] || Math.ceil(this.kid.y)==this.points.y[twoPosition+1]){
  281. twoMove=false;
  282. twoPosition += 1; //Update position
  283. }
  284. }
  285. },
  286. //Navigation functions
  287. showOption: function(){
  288. m_info.text = this.message;
  289. },
  290. loadState: function(){
  291. this.beep.play();
  292. game.state.start(this.state);
  293. },
  294. //MapLoading function
  295. loadGame: function(){
  296. beepSound.play();
  297. if(twoPosition<5){
  298. game.state.start('gameSTwo');
  299. }else{
  300. game.state.start('endSTwo');
  301. }
  302. }
  303. };
  304. /****************************** GAME ****************************/
  305. var sizeA, sizeB, valueA, valueB;
  306. var clickA, clickB, animateA, animateB, result, animate, cDelay, eDelay;
  307. var blocksA, blocksB, auxblqA, auxblqB;
  308. var labelA, fractionA, separatorA, labelB, fractionB, separatorB;
  309. var kid, kidDirection, equals, counter, endCounter;
  310. var xA, yA, xB, yB, blockW, blockH;
  311. var okImg, errorImg;
  312. var gameSquareTwo={
  313. create: function() {
  314. //timer
  315. totalTime = 0;
  316. timer = game.time.create(false);
  317. timer.loop(1000, this.updateCounter, this);
  318. timer.start();
  319. points = [2,4,6,8,9,10,12,14,15,16,18,20];
  320. // Creating sound variable
  321. beepSound = game.add.audio('sound_beep');
  322. okSound = game.add.audio('sound_ok');
  323. errorSound = game.add.audio('sound_error');
  324. // Reading dictionary
  325. var words = game.cache.getJSON('dictionary');
  326. // Background
  327. game.add.image(0, 0, 'bgimage');
  328. //Clouds
  329. game.add.image(300, 100, 'cloud');
  330. game.add.image(660, 80, 'cloud');
  331. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  332. // Styles for labels
  333. var stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  334. var styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  335. var styleFraction = { font: '20px Arial', fill: '#000080', align: 'center'};
  336. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  337. //Floor
  338. for(var i=0;i<9;i++){
  339. game.add.image(i*100, 501, 'floor');
  340. }
  341. //kid
  342. kid = game.add.sprite(100, 470, 'kid_walk');
  343. kid.anchor.setTo(0.5, 0.7);
  344. kid.scale.setTo(0.8);
  345. kid.animations.add('right',[0,1,2,3,4,5,6,7,8,9,10,11]);
  346. kid.animations.add('left',[23,22,21,20,19,18,17,16,15,14,13,12]);
  347. kidDirection = 'right';
  348. kid.animations.play('right', 6, true);
  349. //Control variables
  350. sizeA = 0; //Size of first block
  351. sizeB = 0; //Size of second block
  352. valueA = 0; //Number of clicked blocks for a
  353. valueB = 0; //Number of clicked blocks for b
  354. clickA = false; //If block A is clicked
  355. clickB = false; //If block B is clicked
  356. animateA = false; //Animate A selected blocks to position
  357. animateB = false; //Animate B selected blocks to position
  358. result = false; //Game is correct
  359. animate = null; //Final animation sequence
  360. //generator
  361. console.log("Diff " + twoDifficulty + ", ini " + ((twoDifficulty-1)*2+1) + ", end " + ((twoDifficulty-1)*2+3));
  362. var rPoint = game.rnd.integerInRange((twoDifficulty-1)*2+1,(twoDifficulty-1)*2+3);
  363. sizeA = points[rPoint];
  364. console.log("Rpoint " + rPoint + ", val " + sizeA);
  365. sizeB = this.getRndDivisor(sizeA);
  366. blockB = game.rnd.integerInRange(1, sizeB);
  367. blockA = (sizeA/sizeB) * blockB;
  368. console.log("SA " + sizeA + ", SB " + sizeB + ", BA " + blockA + ", BB " + blockB );
  369. //Blocks and fractions group
  370. blocksA = game.add.group(); //Main blocks A
  371. blocksB = game.add.group(); //Main blocks B
  372. auxblqA = game.add.group(); //Auxiliar blocks A
  373. auxblqB = game.add.group(); //Auxiliar blocks B
  374. //Creating blocks
  375. blockW = 400;
  376. blockH = 50;
  377. if(twoType!="C"){
  378. xA=230, yA=90;
  379. xB=xA, yB=yA+3*blockH+30;
  380. }else{
  381. xB=230, yB=90;
  382. xA=xB, yA=yB+3*blockH+30;
  383. }
  384. //Blocks A
  385. var widthA = blockW/sizeA;
  386. var lineColor = 0x1e2f2f;
  387. var fillColor = 0x83afaf;
  388. var fillColorS = 0xe0ebeb;
  389. for(var i=0; i<sizeA; i++){
  390. //console.log("Block A"+i+": x:"+(xA+i*widthA)+", y:"+yA);
  391. var block = game.add.graphics(xA+i*widthA, yA);
  392. block.anchor.setTo(0.5, 0.5);
  393. block.lineStyle(2, lineColor);
  394. block.beginFill(fillColor);
  395. block.drawRect(0, 0, widthA, blockH);
  396. block.alpha = 0.5;
  397. block.endFill();
  398. block.inputEnabled = true;
  399. block.input.useHandCursor = true;
  400. block.events.onInputDown.add(this.clickSquare, {who: 'A',indice: i});
  401. block.events.onInputOver.add(this.overSquare, {who: 'A',indice: i});
  402. block.events.onInputOut.add(this.outSquare, {who: 'A',indice: i});
  403. blocksA.add(block);
  404. //aux blocks
  405. var xAux = xA+i*widthA, yAux = yA+blockH+10;
  406. if(twoType == 'C') yAux = yA;
  407. var block = game.add.graphics(xAux, yAux );
  408. block.anchor.setTo(0.5, 0.5);
  409. block.lineStyle(1, lineColor);
  410. block.beginFill(fillColorS);
  411. block.drawRect(0, 0, widthA, blockH);
  412. if(twoType!='A') block.alpha = 0;
  413. else block.alpha = 0.2;
  414. auxblqA.add(block);
  415. }
  416. //label block A
  417. var labelX = xA+blockW+30;
  418. var labelY = yA+blockH/2;
  419. labelA = game.add.text(labelX, labelY, sizeA , styleFraction);
  420. labelA.anchor.setTo(0.5, 0.41);
  421. //label fraction
  422. labelX = xA+(blockA*widthA)+40;
  423. labelY = yA+blockH+34;
  424. fractionA = game.add.text(labelX, labelY, "0\n"+sizeA , styleFraction);
  425. fractionA.anchor.setTo(0.5, 0.41);
  426. separatorA = game.add.sprite(labelX, labelY, 'separator');
  427. separatorA.anchor.setTo(0.5, 0.5);
  428. fractionA.alpha = 0;
  429. separatorA.alpha = 0;
  430. //Blocks B
  431. var widthB = blockW/sizeB;
  432. lineColor = 0x260d0d;
  433. fillColor = 0xd27979;
  434. fillColorS = 0xf2d9d9;
  435. for(var i=0; i<sizeB; i++){
  436. var block = game.add.graphics(xB+i*widthB, yB);
  437. block.anchor.setTo(0.5, 0.5);
  438. block.lineStyle(2, lineColor);
  439. block.beginFill(fillColor);
  440. block.drawRect(0, 0, widthB, blockH);
  441. block.endFill();
  442. block.inputEnabled = true;
  443. block.input.useHandCursor = true;
  444. block.events.onInputDown.add(this.clickSquare, {who: 'B',indice: i});
  445. block.events.onInputOver.add(this.overSquare, {who: 'B',indice: i});
  446. block.events.onInputOut.add(this.outSquare, {who: 'B',indice: i});
  447. blocksB.add(block);
  448. //aux blocks
  449. var xAux = xB+i*widthB, yAux = yB+blockH+10;
  450. if(twoType == 'C') yAux = yB;
  451. var block = game.add.graphics(xAux, yAux);
  452. block.anchor.setTo(0.5, 0.5);
  453. block.lineStyle(1, lineColor);
  454. block.beginFill(fillColorS);
  455. block.drawRect(0, 0, widthB, blockH);
  456. if(twoType!='A') block.alpha = 0;
  457. else block.alpha = 0.2;
  458. auxblqB.add(block);
  459. }
  460. //label block B
  461. labelX = xA+blockW+30;
  462. labelY = yB+blockH/2;
  463. labelB = game.add.text(labelX, labelY, sizeB , styleFraction);
  464. labelB.anchor.setTo(0.5, 0.41);
  465. //label fraction
  466. labelX = xA+(blockB*widthB)+40;
  467. labelY = yB+blockH+34;
  468. fractionB = game.add.text(labelX, labelY, "0\n"+sizeB , styleFraction);
  469. fractionB.anchor.setTo(0.5, 0.41);
  470. separatorB = game.add.sprite(labelX, labelY, 'separator');
  471. separatorB.anchor.setTo(0.5, 0.5);
  472. fractionB.alpha = 0;
  473. separatorB.alpha = 0;
  474. //information label
  475. m_info = game.add.text(14, 53, "", { font: "20px Arial", fill: "#330000", align: "center" });
  476. if(twoMenu){
  477. // Return to language button
  478. m_world = game.add.sprite(10, 10, 'world');
  479. m_world.inputEnabled = true;
  480. m_world.input.useHandCursor = true;
  481. m_world.events.onInputDown.add(this.loadState, {state: "boot", beep: beepSound});
  482. m_world.events.onInputOver.add(this.showOption, {message: words.menu_world});
  483. m_world.events.onInputOut.add(this.showOption, {message: ""});
  484. // Return to menu button
  485. m_list = game.add.sprite(60, 10, 'list');
  486. m_list.inputEnabled = true;
  487. m_list.input.useHandCursor = true;
  488. m_list.events.onInputDown.add(this.loadState, {state: "menu", beep: beepSound});
  489. m_list.events.onInputOver.add(this.showOption, {message: words.menu_list});
  490. m_list.events.onInputOut.add(this.showOption, {message: ""});
  491. // Return to diffculty
  492. m_back = game.add.sprite(110, 10, 'back');
  493. m_back.inputEnabled = true;
  494. m_back.input.useHandCursor = true;
  495. m_back.events.onInputDown.add(this.loadState, {state: "menuSTwo", beep: beepSound});
  496. m_back.events.onInputOver.add(this.showOption, {message: words.menu_back});
  497. m_back.events.onInputOut.add(this.showOption, {message: ""});
  498. }
  499. //ok and error images
  500. okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  501. okImg.anchor.setTo(0.5);
  502. okImg.alpha = 0;
  503. errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  504. errorImg.anchor.setTo(0.5);
  505. errorImg.alpha = 0;
  506. counter = 0;
  507. endCounter = 100;
  508. cDelay = 0;
  509. eDelay = 60;
  510. },
  511. updateCounter: function() {
  512. totalTime++;
  513. },
  514. overSquare: function(){
  515. if(!clickA && this.who=="A"){
  516. for(var i=0;i<sizeA;i++){
  517. if(i<=this.indice){
  518. blocksA.children[i].alpha = 1;
  519. }else{
  520. blocksA.children[i].alpha = 0.5;
  521. }
  522. }
  523. fractionA.x = xA+((this.indice +1)*(blockW/sizeA))+40;
  524. fractionA.alpha = 1;
  525. fractionA.setText(this.indice +1);
  526. }
  527. if(!clickB && this.who=="B"){
  528. for(var i=0;i<sizeB;i++){
  529. if(i<=this.indice){
  530. blocksB.children[i].alpha = 1;
  531. }else{
  532. blocksB.children[i].alpha = 0.5;
  533. }
  534. }
  535. fractionB.x = xB+((this.indice +1)*(blockW/sizeB))+40;
  536. fractionB.alpha = 1;
  537. fractionB.setText(this.indice +1);
  538. }
  539. },
  540. outSquare: function(){
  541. if(!clickA && this.who=="A"){
  542. for(var i=0;i<=this.indice;i++){
  543. blocksA.children[i].alpha = 0.5;
  544. }
  545. fractionA.alpha = 0;
  546. }
  547. if(!clickB && this.who=="B"){
  548. for(var i=0;i<=this.indice;i++){
  549. blocksB.children[i].alpha = 0.5;
  550. }
  551. fractionB.alpha = 0;
  552. }
  553. },
  554. clickSquare: function(){
  555. if(!clickA && this.who=="A"){
  556. for(var i=0;i<sizeA;i++){
  557. blocksA.children[i].inputEnabled = false;
  558. if(i<=this.indice){
  559. blocksA.children[i].alpha = 1;
  560. }else{
  561. blocksA.children[i].alpha = 0.5;
  562. auxblqA.children[i].alpha = 0;
  563. }
  564. }
  565. labelA.alpha = 0;
  566. beepSound.play();
  567. clickA = true;
  568. valueA = this.indice+1;
  569. fractionA.x = xA+(valueA*(blockW/sizeA))+40;
  570. separatorA.x = fractionA.x
  571. animateA = true;
  572. }
  573. if(!clickB && this.who=="B"){
  574. for(var i=0;i<sizeB;i++){
  575. blocksB.children[i].inputEnabled = false;
  576. if(i<=this.indice){
  577. blocksB.children[i].alpha = 1;
  578. }else{
  579. blocksB.children[i].alpha = 0.5;
  580. auxblqB.children[i].alpha = 0;
  581. }
  582. }
  583. labelB.alpha = 0;
  584. beepSound.play();
  585. clickB = true;
  586. valueB = this.indice+1;
  587. fractionB.x = xB+(valueB*(blockW/sizeB))+40;
  588. separatorB.x = fractionB.x
  589. animateB = true;
  590. }
  591. },
  592. postScore: function (){
  593. var abst = "numBlocksA:"+sizeA+", valueA: " + valueA +", numBlocksB: " + sizeB + ", valueB: " + valueB;
  594. var hr = new XMLHttpRequest();
  595. // Create some variables we need to send to our PHP file
  596. var url = "resource/cn/save.php";
  597. var vars = "s_ip="+hip+"&s_name="+name+"&s_lang="+lang+"&s_game="+twoShape+"&s_mode="+twoType;
  598. vars += "&s_oper=Equal&s_leve="+twoDifficulty+"&s_posi="+twoPosition+"&s_resu="+result+"&s_time="+totalTime+"&s_deta="+abst;
  599. hr.open("POST", url, true);
  600. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  601. hr.onreadystatechange = function() {
  602. console.log(hr);
  603. if(hr.readyState == 4 && hr.status == 200) {
  604. var return_data = hr.responseText;
  605. console.log(return_data);
  606. }
  607. }
  608. // Send the data to PHP now... and wait for response to update the status div
  609. hr.send(vars); // Actually execute the request
  610. console.log("processing...");
  611. },
  612. update: function() {
  613. if(!(clickA && clickB) && !animate){
  614. if(kidDirection=='right'){
  615. kid.x += 1;
  616. if(kid.x>=800){
  617. kidDirection='left';
  618. kid.animations.play('left', 8, true);
  619. }
  620. }else{
  621. kid.x -= 1;
  622. if(kid.x<=100){
  623. kidDirection='right';
  624. kid.animations.play('right', 8, true);
  625. }
  626. }
  627. }
  628. if(animateA){ //If clicked A only, animate
  629. for(var i=0;i<valueA;i++){
  630. blocksA.children[i].y +=2;
  631. }
  632. if(blocksA.children[0].y>=auxblqA.children[0].y){
  633. animateA = false;
  634. fractionA.alpha = 1;
  635. fractionA.setText(valueA+"\n"+sizeA);
  636. separatorA.alpha = 1;
  637. }
  638. }
  639. if(animateB){ //If clicked B only, animate
  640. for(var i=0;i<valueB;i++){
  641. blocksB.children[i].y +=2;
  642. }
  643. if(blocksB.children[0].y>=auxblqB.children[0].y){
  644. animateB = false;
  645. fractionB.alpha = 1;
  646. fractionB.setText(valueB+"\n"+sizeB);
  647. separatorB.alpha = 1;
  648. }
  649. }
  650. if(clickA && clickB && !this.animate){
  651. //Check result
  652. timer.stop();
  653. cDelay++;
  654. if(cDelay>=eDelay){
  655. if((valueA/sizeA) == (valueB/sizeB)){
  656. result = true;
  657. twoMove = true;
  658. okSound.play();
  659. okImg.alpha = 1;
  660. }else{
  661. result = false;
  662. twoMove = false;
  663. errorSound.play();
  664. kid.animations.stop();
  665. errorImg.alpha = 1;
  666. }
  667. this.postScore();
  668. clickA = false;
  669. clickB = false;
  670. animate = true;
  671. }
  672. }
  673. if(animate){
  674. counter++;
  675. if(result){
  676. kid.x += 2;
  677. kidDirection='right';
  678. kid.animations.play('right', 8, true);
  679. }
  680. if(counter>endCounter){
  681. game.state.start('mapSTwo');
  682. }
  683. }
  684. },
  685. //Navigation functions,
  686. showOption: function(){
  687. m_info.text = this.message;
  688. },
  689. loadState: function(){
  690. this.beep.play();
  691. game.state.start(this.state);
  692. },
  693. //MapLoading function
  694. loadGame: function(){
  695. beepSound.play();
  696. if(onePosition<5){
  697. game.state.start('gameSOne');
  698. }else{
  699. game.state.start('endSOne');
  700. }
  701. },
  702. //Calculation help functions
  703. getRndDivisor: function(number){ //Get random divisor for a number
  704. var div = []; //Divisors found
  705. var p = 0; //current dividor index
  706. for(var i=2; i<number;i++){
  707. if(number%i==0){
  708. div[p] = i;
  709. p++;
  710. }
  711. }
  712. var x = game.rnd.integerInRange(0,p-1);
  713. return div[x];
  714. }
  715. };
  716. /****************************** END ****************************/
  717. var endSquareTwo={
  718. create: function() {
  719. // Creating sound variable
  720. beepSound = game.add.audio('sound_beep');
  721. okSound = game.add.audio('sound_ok');
  722. errorSound = game.add.audio('sound_error');
  723. // Reading dictionary
  724. var words = game.cache.getJSON('dictionary');
  725. // Background
  726. game.add.image(0, 0, 'bgimage');
  727. //Clouds
  728. game.add.image(300, 100, 'cloud');
  729. game.add.image(660, 80, 'cloud');
  730. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  731. // Styles for labels
  732. var stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  733. var styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  734. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  735. //Floor
  736. for(var i=0;i<9;i++){
  737. game.add.image(i*100, 501, 'floor');
  738. }
  739. // Progress bar
  740. for(var p=1;p<=5;p++){
  741. var block = game.add.image(672+(p-1)*30, 10, 'block');
  742. block.scale.setTo(2, 1); //Scaling to double width
  743. }
  744. game.add.text(820, 10, '100%', styleMenu);
  745. game.add.text(660, 10, words.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  746. game.add.image(670, 10, 'pgbar');
  747. //School and trees
  748. game.add.sprite(600, 222 , 'school').scale.setTo(0.7);
  749. game.add.sprite(30, 280 , 'tree4');
  750. game.add.sprite(360, 250 , 'tree2');
  751. //kid
  752. this.kid = game.add.sprite(0, 460 , 'kid_run');
  753. this.kid.anchor.setTo(0.5,0.5);
  754. this.kid.scale.setTo(0.7);
  755. this.kid.animations.add('walk', [0,1,2,3,4,5,6,7,8,9,10,11]);
  756. this.kid.animations.play('walk', 6, true);
  757. },
  758. update: function() {
  759. if(this.kid.x<=700){
  760. this.kid.x += 2;
  761. }else{
  762. if(twoMenu){
  763. game.state.start('menu');
  764. }else{
  765. this.kid.animations.stop();
  766. }
  767. }
  768. },
  769. verPrincipal: function(){
  770. game.state.start('welcome');
  771. },
  772. verMenu: function(){
  773. if(twoMenu){
  774. game.state.start('menu');
  775. }
  776. }
  777. };