squareTwo.js 31 KB

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