squareTwo.js 34 KB

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