squareTwo.js 35 KB

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