squareTwo.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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(660+(p-1)*30, 10, 'block');
  174. block.scale.setTo(2, 1); //Scaling to double width
  175. }
  176. game.add.text(820, 10, percentText+'%', styleMenu);
  177. game.add.text(650, 10, lang.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  178. game.add.image(660, 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_lost');
  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. kid.animations.add('front', [3,4,5])
  333. kidDirection = 'front';
  334. kidLeftLimit = 100;
  335. kidRightLimit = 800;
  336. kid.animations.play('front', 6, true);
  337. //Control variables
  338. sizeA = 0; //Size of first block
  339. sizeB = 0; //Size of second block
  340. valueA = 0; //Number of clicked blocks for a
  341. valueB = 0; //Number of clicked blocks for b
  342. clickA = false; //If block A is clicked
  343. clickB = false; //If block B is clicked
  344. animateA = false; //Animate A selected blocks to position
  345. animateB = false; //Animate B selected blocks to position
  346. result = false; //Game is correct
  347. animate = null; //Final animation sequence
  348. //generator
  349. console.log("----------");
  350. console.log("Diff " + twoDifficulty + ", ini " + ((twoDifficulty-1)*2+1) + ", end " + ((twoDifficulty-1)*2+3));
  351. var rPoint = game.rnd.integerInRange((twoDifficulty-1)*2+1,(twoDifficulty-1)*2+3);
  352. sizeA = points[rPoint];
  353. console.log("Rpoint " + rPoint + ", val " + sizeA);
  354. sizeB = this.getRndDivisor(sizeA);
  355. blockB = game.rnd.integerInRange(1, sizeB);
  356. blockA = (sizeA/sizeB) * blockB;
  357. console.log("SA " + sizeA + ", SB " + sizeB + ", BA " + blockA + ", BB " + blockB );
  358. //Blocks and fractions group
  359. blocksA = game.add.group(); //Main blocks A
  360. blocksB = game.add.group(); //Main blocks B
  361. auxblqA = game.add.group(); //Auxiliar blocks A
  362. auxblqB = game.add.group(); //Auxiliar blocks B
  363. //Creating blocks
  364. blockW = 400;
  365. blockH = 50;
  366. if(twoType!="C"){
  367. xA=230, yA=90;
  368. xB=xA, yB=yA+3*blockH+30;
  369. }else{
  370. xB=230, yB=90;
  371. xA=xB, yA=yB+3*blockH+30;
  372. }
  373. //Blocks A
  374. var widthA = blockW/sizeA;
  375. var lineColor = 0x1e2f2f;
  376. var fillColor = 0x83afaf;
  377. var fillColorS = 0xe0ebeb;
  378. for(var i=0; i<sizeA; i++){
  379. //console.log("Block A"+i+": x:"+(xA+i*widthA)+", y:"+yA);
  380. var block = game.add.graphics(xA+i*widthA, yA);
  381. block.anchor.setTo(0.5, 0.5);
  382. block.lineStyle(2, lineColor);
  383. block.beginFill(fillColor);
  384. block.drawRect(0, 0, widthA, blockH);
  385. block.alpha = 0.5;
  386. block.endFill();
  387. block.inputEnabled = true;
  388. block.input.useHandCursor = true;
  389. block.events.onInputDown.add(this.clickSquare, {who: 'A',indice: i});
  390. block.events.onInputOver.add(this.overSquare, {who: 'A',indice: i});
  391. block.events.onInputOut.add(this.outSquare, {who: 'A',indice: i});
  392. blocksA.add(block);
  393. //aux blocks
  394. var xAux = xA+i*widthA, yAux = yA+blockH+10;
  395. if(twoType == 'C') yAux = yA;
  396. var block = game.add.graphics(xAux, yAux );
  397. block.anchor.setTo(0.5, 0.5);
  398. block.lineStyle(1, lineColor);
  399. block.beginFill(fillColorS);
  400. block.drawRect(0, 0, widthA, blockH);
  401. if(twoType!='A') block.alpha = 0;
  402. else block.alpha = 0.2;
  403. auxblqA.add(block);
  404. }
  405. //label block A
  406. var labelX = xA+blockW+30;
  407. var labelY = yA+blockH/2;
  408. labelA = game.add.text(labelX, labelY, sizeA , styleFraction);
  409. labelA.anchor.setTo(0.5, 0.41);
  410. //label fraction
  411. labelX = xA+(blockA*widthA)+40;
  412. labelY = yA+blockH+34;
  413. fractionA = game.add.text(labelX, labelY, "0\n"+sizeA , styleFraction);
  414. fractionA.anchor.setTo(0.5, 0.41);
  415. separatorA = game.add.sprite(labelX, labelY, 'separator');
  416. separatorA.anchor.setTo(0.5, 0.5);
  417. fractionA.alpha = 0;
  418. separatorA.alpha = 0;
  419. //Blocks B
  420. var widthB = blockW/sizeB;
  421. lineColor = 0x260d0d;
  422. fillColor = 0xd27979;
  423. fillColorS = 0xf2d9d9;
  424. for(var i=0; i<sizeB; i++){
  425. var block = game.add.graphics(xB+i*widthB, yB);
  426. block.anchor.setTo(0.5, 0.5);
  427. block.lineStyle(2, lineColor);
  428. block.beginFill(fillColor);
  429. block.drawRect(0, 0, widthB, blockH);
  430. block.endFill();
  431. block.inputEnabled = true;
  432. block.input.useHandCursor = true;
  433. block.events.onInputDown.add(this.clickSquare, {who: 'B',indice: i});
  434. block.events.onInputOver.add(this.overSquare, {who: 'B',indice: i});
  435. block.events.onInputOut.add(this.outSquare, {who: 'B',indice: i});
  436. blocksB.add(block);
  437. //aux blocks
  438. var xAux = xB+i*widthB, yAux = yB+blockH+10;
  439. if(twoType == 'C') yAux = yB;
  440. var block = game.add.graphics(xAux, yAux);
  441. block.anchor.setTo(0.5, 0.5);
  442. block.lineStyle(1, lineColor);
  443. block.beginFill(fillColorS);
  444. block.drawRect(0, 0, widthB, blockH);
  445. if(twoType!='A') block.alpha = 0;
  446. else block.alpha = 0.2;
  447. auxblqB.add(block);
  448. }
  449. //label block B
  450. labelX = xA+blockW+30;
  451. labelY = yB+blockH/2;
  452. labelB = game.add.text(labelX, labelY, sizeB , styleFraction);
  453. labelB.anchor.setTo(0.5, 0.41);
  454. //label fraction
  455. labelX = xA+(blockB*widthB)+40;
  456. labelY = yB+blockH+34;
  457. fractionB = game.add.text(labelX, labelY, "0\n"+sizeB , styleFraction);
  458. fractionB.anchor.setTo(0.5, 0.41);
  459. separatorB = game.add.sprite(labelX, labelY, 'separator');
  460. separatorB.anchor.setTo(0.5, 0.5);
  461. fractionB.alpha = 0;
  462. separatorB.alpha = 0;
  463. //ok and error images
  464. okImg = game.add.image(game.world.centerX, game.world.centerY, 'h_ok');
  465. okImg.anchor.setTo(0.5);
  466. okImg.alpha = 0;
  467. errorImg = game.add.image(game.world.centerX, game.world.centerY, 'h_error');
  468. errorImg.anchor.setTo(0.5);
  469. errorImg.alpha = 0;
  470. //error text
  471. errorTextA = game.add.text(game.world.centerX, game.world.centerY-225, "", {font: "20px Arial", fill: "#330000", align: "center" });
  472. errorTextA.anchor.setTo(0.5, 0.5);
  473. errorTextB = game.add.text(game.world.centerX, game.world.centerY-45, "", {font: "20px Arial", fill: "#330000", align: "center" });
  474. errorTextB.anchor.setTo(0.5, 0.5);
  475. counter = 0;
  476. endCounter = 100;
  477. cDelay = 0;
  478. eDelay = 60;
  479. },
  480. update: function()
  481. {
  482. if (game.physics.arcade.distanceToPointer(kid, game.input.activePointer) > 20 )
  483. {
  484. var xPos = game.input.mousePointer.x;
  485. /*if (xPos < kid.x + 10){
  486. kidDirection='right';
  487. kid.animations.play('right', 8, true);
  488. }else if (xPos > kid.x){
  489. kidDirection='right';
  490. kid.animations.play('right', 8, true);
  491. }*/
  492. //set limit to the arrow
  493. if (xPos < kidLeftLimit){
  494. xPos = kidLeftLimit;
  495. }
  496. if (xPos > kidRightLimit){
  497. xPos = kidRightLimit;
  498. }
  499. kid.x = xPos;
  500. }
  501. //If clicked A only, animate
  502. if(animateA){
  503. for(var i=0;i<valueA;i++){
  504. blocksA.children[i].y +=2;
  505. }
  506. if(blocksA.children[0].y>=auxblqA.children[0].y){
  507. animateA = false;
  508. fractionA.alpha = 1;
  509. fractionA.setText(valueA+"\n"+sizeA);
  510. separatorA.alpha = 1;
  511. }
  512. }
  513. //If clicked B only, animate
  514. if(animateB){
  515. for(var i=0;i<valueB;i++){
  516. blocksB.children[i].y +=2;
  517. }
  518. if(blocksB.children[0].y>=auxblqB.children[0].y){
  519. animateB = false;
  520. fractionB.alpha = 1;
  521. fractionB.setText(valueB+"\n"+sizeB);
  522. separatorB.alpha = 1;
  523. }
  524. }
  525. //if clicked A and B
  526. if(clickA && clickB && !this.animate){
  527. //Check result
  528. timer.stop();
  529. cDelay++;
  530. if(cDelay>=eDelay){
  531. //fractions are equivalent : correct
  532. if((valueA/sizeA) == (valueB/sizeB)){
  533. result = true;
  534. twoMove = true;
  535. if(audioStatus){
  536. okSound.play();
  537. }
  538. kid.animations.stop();
  539. okImg.alpha = 1;
  540. //fractions are not equivalent
  541. }else{
  542. result = false;
  543. twoMove = false;
  544. if(audioStatus){
  545. errorSound.play();
  546. }
  547. kid.animations.stop();
  548. errorImg.alpha = 1;
  549. }
  550. this.postScore();
  551. clickA = false;
  552. clickB = false;
  553. animate = true;
  554. }
  555. }
  556. if(animate){
  557. counter++;
  558. if(result){
  559. // kid.x += 2;
  560. // kidDirection='right';
  561. // kid.animations.play('right', 8, true);
  562. }
  563. if(counter>endCounter){
  564. game.state.start('mapSTwo');
  565. }
  566. }
  567. },
  568. updateCounter: function() {
  569. totalTime++;
  570. },
  571. overSquare: function(){
  572. if(!clickA && this.who=="A"){
  573. if(this.indice == sizeA-1){
  574. if(yA==90){
  575. errorTextA.setText(lang.error_msg);
  576. errorTextB.setText("");
  577. }else{
  578. errorTextA.setText("");
  579. errorTextB.setText(lang.error_msg);
  580. }
  581. }else{
  582. errorTextA.setText("");
  583. errorTextB.setText("");
  584. for(var i=0;i<sizeA;i++){
  585. if(i<=this.indice){
  586. blocksA.children[i].alpha = 1;
  587. }else{
  588. blocksA.children[i].alpha = 0.5;
  589. }
  590. }
  591. fractionA.x = xA+((this.indice +1)*(blockW/sizeA))+40;
  592. fractionA.alpha = 1;
  593. fractionA.setText(this.indice +1);
  594. }
  595. }
  596. if(!clickB && this.who=="B"){
  597. if(this.indice == sizeB-1){
  598. if(yA==90){
  599. errorTextA.setText("");
  600. errorTextB.setText(lang.error_msg);
  601. }else{
  602. errorTextA.setText(lang.error_msg);
  603. errorTextB.setText("");
  604. }
  605. }else{
  606. errorTextA.setText("");
  607. errorTextB.setText("");
  608. for(var i=0;i<sizeB;i++){
  609. if(i<=this.indice){
  610. blocksB.children[i].alpha = 1;
  611. }else{
  612. blocksB.children[i].alpha = 0.5;
  613. }
  614. }
  615. fractionB.x = xB+((this.indice +1)*(blockW/sizeB))+40;
  616. fractionB.alpha = 1;
  617. fractionB.setText(this.indice +1);
  618. }
  619. }
  620. },
  621. outSquare: function(){
  622. if(!clickA && this.who=="A"){
  623. for(var i=0;i<=this.indice;i++){
  624. blocksA.children[i].alpha = 0.5;
  625. }
  626. fractionA.alpha = 0;
  627. }
  628. if(!clickB && this.who=="B"){
  629. for(var i=0;i<=this.indice;i++){
  630. blocksB.children[i].alpha = 0.5;
  631. }
  632. fractionB.alpha = 0;
  633. }
  634. },
  635. clickSquare: function(){
  636. if(!clickA && this.who=="A" && this.indice!=sizeA-1){
  637. for(var i=0;i<sizeA;i++){
  638. blocksA.children[i].inputEnabled = false;
  639. if(i<=this.indice){
  640. blocksA.children[i].alpha = 1;
  641. }else{
  642. blocksA.children[i].alpha = 0.5;
  643. auxblqA.children[i].alpha = 0;
  644. }
  645. }
  646. labelA.alpha = 0;
  647. if(audioStatus){
  648. beepSound.play();
  649. }
  650. clickA = true;
  651. valueA = this.indice+1;
  652. fractionA.x = xA+(valueA*(blockW/sizeA))+40;
  653. separatorA.x = fractionA.x
  654. animateA = true;
  655. }
  656. if(!clickB && this.who=="B" && this.indice!=sizeB-1){
  657. for(var i=0;i<sizeB;i++){
  658. blocksB.children[i].inputEnabled = false;
  659. if(i<=this.indice){
  660. blocksB.children[i].alpha = 1;
  661. }else{
  662. blocksB.children[i].alpha = 0.5;
  663. auxblqB.children[i].alpha = 0;
  664. }
  665. }
  666. labelB.alpha = 0;
  667. if(audioStatus){
  668. beepSound.play();
  669. }
  670. clickB = true;
  671. valueB = this.indice+1;
  672. fractionB.x = xB+(valueB*(blockW/sizeB))+40;
  673. separatorB.x = fractionB.x
  674. animateB = true;
  675. }
  676. },
  677. postScore: function (){
  678. var abst = "numBlocksA:"+sizeA+", valueA: " + valueA +", numBlocksB: " + sizeB + ", valueB: " + valueB;
  679. var lang_str = "pt_BR"; //TODO NAO esta pegando a lingua definida pelo usuario!
  680. var hr = new XMLHttpRequest();
  681. // Create some variables we need to send to our PHP file
  682. var url = "assets/cn/save.php";
  683. var vars = "s_ip="+hip+"&s_name=" + username + "&s_lang=" + lang_str + "&s_game=" + twoShape + "&s_mode=" + twoType;
  684. vars += "&s_oper=Equal&s_leve=" + twoDifficulty + "&s_posi=" + twoPosition + "&s_resu=" + result + "&s_time=" + totalTime + "&s_deta=" + abst;
  685. hr.open("POST", url, true);
  686. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  687. hr.onreadystatechange = function() {
  688. console.log(hr);
  689. if(hr.readyState == 4 && hr.status == 200) {
  690. var return_data = hr.responseText;
  691. console.log(return_data);
  692. }
  693. }
  694. // Send the data to PHP now... and wait for response to update the status div
  695. hr.send(vars); // Actually execute the request
  696. console.log("processing...");
  697. },
  698. //Calculation help functions
  699. getRndDivisor: function(number){ //Get random divisor for a number
  700. var div = []; //Divisors found
  701. var p = 0; //current dividor index
  702. for(var i=2; i<number;i++){
  703. if(number%i==0){
  704. div[p] = i;
  705. p++;
  706. }
  707. }
  708. var x = game.rnd.integerInRange(0,p-1);
  709. return div[x];
  710. },
  711. };
  712. /****************************** END ****************************/
  713. var endSquareTwo = {
  714. create: function() {
  715. // Creating sound variable
  716. beepSound = game.add.audio('sound_beep');
  717. okSound = game.add.audio('sound_ok');
  718. errorSound = game.add.audio('sound_error');
  719. // Background
  720. game.add.image(0, 0, 'bgimage');
  721. //Clouds
  722. game.add.image(300, 100, 'cloud');
  723. game.add.image(660, 80, 'cloud');
  724. game.add.image(110, 85, 'cloud').scale.setTo(0.8);
  725. // Styles for labels
  726. var stylePlace = { font: '26px Arial', fill: '#400080', align: 'center'};
  727. var styleLabel = { font: '26px Arial', fill: '#000080', align: 'center'};
  728. var styleMenu = { font: '30px Arial', fill: '#000000', align: 'center'};
  729. //Floor
  730. for(var i=0;i<9;i++){
  731. game.add.image(i*100, 501, 'floor');
  732. }
  733. // Progress bar
  734. for(var p=1;p<=5;p++){
  735. var block = game.add.image(660+(p-1)*30, 10, 'block');
  736. block.scale.setTo(2, 1); //Scaling to double width
  737. }
  738. game.add.text(820, 10, '100%', styleMenu);
  739. game.add.text(650, 10, lang.difficulty + ' ' + oneDifficulty, styleMenu).anchor.setTo(1,0);
  740. game.add.image(660, 10, 'pgbar');
  741. //School and trees
  742. game.add.sprite(600, 222 , 'school').scale.setTo(0.7);
  743. game.add.sprite(30, 280 , 'tree4');
  744. game.add.sprite(360, 250 , 'tree2');
  745. //kid
  746. this.kid = game.add.sprite(0, 460 , 'kid_run');
  747. this.kid.anchor.setTo(0.5,0.5);
  748. this.kid.scale.setTo(0.7);
  749. this.kid.animations.add('walk', [0,1,2,3,4,5,6,7,8,9,10,11]);
  750. this.kid.animations.play('walk', 6, true);
  751. },
  752. update: function() {
  753. if(this.kid.x<=700){
  754. this.kid.x += 2;
  755. }else{
  756. if(twoMenu){
  757. game.state.start('menu');
  758. }else{
  759. this.kid.animations.stop();
  760. }
  761. }
  762. },
  763. verPrincipal: function(){
  764. game.state.start('welcome');
  765. },
  766. verMenu: function(){
  767. if(twoMenu){
  768. game.state.start('menu');
  769. }
  770. }
  771. };