squareTwo.js 37 KB

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