squareTwo.js 32 KB

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