(cocos2d-js游戏)选择字体的颜色

前端之家收集整理的这篇文章主要介绍了(cocos2d-js游戏)选择字体的颜色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

游戏的基本玩法:从"红色","黄色","绿色","蓝色","紫色","黑色",中随机选出一个字,然后将字的颜色设置成与文字不同的颜色,如:文字是红色,字体的颜色是黄色,然后让玩家选择字体的颜色,测试反应时间。

代码如下:

  1. <span style="font-family:Courier New;">var MainLayer = cc.LayerColor.extend({
  2. init:function()
  3. {
  4. //初始化界面
  5. //0:点击开始界面 1:表示等待界面,2:表示在点击界面 3:结果界面 4:Too soon界面
  6. this.flag = 0;
  7. this._super();
  8. this.size = cc.Director.getInstance().getWinSize();
  9. this.setColor(cc.c4(180,170,160,255));
  10. this.showToStart();
  11. //可触摸
  12. this.setTouchEnabled(true);
  13.  
  14. //一些常量
  15. this.redColor = new Array(255,0);
  16. this.yellowColor = new Array(255,255,0);
  17. this.greenColor = new Array(0,0);
  18. this.blueColor = new Array(0,255 );
  19. this.purpleColor = new Array(139,255);
  20. this.blackColor = new Array(0,0);
  21.  
  22. this.allColor = new Array(this.redColor,this.yellowColor,this.greenColor,this.blueColor,this.purpleColor,this.blackColor);
  23. this.allColorText = new Array("红色","黑色");
  24. },/////////////////////////////////////////////////
  25. //处理触摸事件
  26. /////////////////////////////////////////////////
  27. onTouchesEnded:function(touches,event)
  28. {
  29.  
  30. },onTouchesBegan:function(touches,event)
  31. {
  32. cc.log("onTouchsBegan",this.flag);
  33. switch(this.flag)
  34. {
  35. case 0:this.showGame();break;
  36. //case 1:this.showToSoon();break;
  37. case 2:this.showToStart();break;
  38. }
  39.  
  40. },//显示点击开始界面
  41. showToStart:function()
  42. {
  43. this.sprite = cc.Sprite.create(s_ClickToStart);
  44. this.sprite.setPosition(this.size.width/2,this.size.height/2);
  45. this.addChild(this.sprite,1);
  46. //click to start 动画
  47. this.startAnim = cc.Sprite.create(s_ClickToStartAnim);
  48. this.startAnim.setPosition(this.size.width/2,this.size.height/2);
  49. this.addChild(this.startAnim,1);
  50. var action = cc.Sequence.create(cc.FadeOut.create(1.0),cc.FadeIn.create(1.0));
  51. var rep = cc.RepeatForever.create(action);
  52. this.startAnim.runAction(rep);
  53.  
  54. this.flag = 0;
  55. },//显示游戏界面
  56. showGame:function()
  57. {
  58. //显示背景
  59. // this.sprite = cc.Sprite.create(s_Background);
  60. // this.sprite.setPosition(this.size.width/2,this.size.height/2);
  61. this.setTouchEnabled(false);
  62. this.startDate = new Date();
  63. //生成两个随机
  64. var randomNum1 = Math.floor(Math.random()*6);
  65. var randomNum2 = Math.floor(Math.random()*6);
  66. //保证两个随机数不一样
  67. while(randomNum1 == randomNum2)
  68. {
  69. randomNum2 = Math.floor(Math.random()*6);
  70. }
  71. this.removeAllChildren();
  72. //显示一个文字
  73. this.textLabel = cc.LabelTTF.create(this.allColorText[randomNum2],"微软雅黑",50);
  74. this.textLabel.setColor(cc.c3(this.allColor[randomNum1][0],this.allColor[randomNum1][1],this.allColor[randomNum1][2]));
  75. this.textLabel.setPosition(this.size.width/2,this.size.height*2/3);
  76. this.addChild(this.textLabel,2);
  77.  
  78. //显示底部按钮,保证随机显示在左右两边
  79. var randomNum3 = Math.floor(Math.random()*10)%2;
  80. var tmp1 = 1;
  81. var tmp2 = 2;
  82. if(randomNum3 == 0)
  83. {
  84. tmp1 = 1;
  85. tmp2 = 2;
  86. }else
  87. {
  88. tmp1 = 2;
  89. tmp2 = 1;
  90. }
  91. cc.log("randomNum3",randomNum3);
  92. var rightBtn = cc.MenuItemImage.create(
  93. "res/btn.png","res/btn.png",function () {
  94. this.showResult(1);
  95. },this);
  96. rightBtn.setColor(cc.c3(this.allColor[randomNum1][0],this.allColor[randomNum1][2]));
  97. rightBtn.setAnchorPoint(0.5,0.5);
  98. rightBtn.setPosition(this.size.width*tmp1/3,this.size.height/3);
  99.  
  100. var errorBtn = cc.MenuItemImage.create(
  101. "res/btn.png",function () {
  102. this.showResult(0);
  103. },this);
  104. errorBtn.setAnchorPoint(0.5,0.5);
  105. errorBtn.setColor(cc.c3(this.allColor[randomNum2][0],this.allColor[randomNum2][1],this.allColor[randomNum2][2]));
  106. errorBtn.setPosition(this.size.width*tmp2/3,this.size.height/3);
  107. var menu = cc.Menu.create(rightBtn,errorBtn);
  108.  
  109. menu.setPosition(0,0);
  110. this.addChild(menu,1);
  111. this.flag = 1;
  112. },showResult:function(tag)
  113. {
  114. this.setTouchEnabled(true);
  115. this.removeAllChildren();
  116. var str;
  117. this.endDate = new Date();//记录点击时间
  118. time = this.endDate.getTime() - this.startDate.getTime();
  119. this.sprite = cc.Sprite.create(s_Result);
  120. this.sprite.setPosition(this.size.width/2,1);
  121. if(tag == 1)
  122. {
  123. str = time+"ms";
  124. }else
  125. {
  126. str = "ERROR!";
  127. }
  128.  
  129. cc.log("showResult",time);
  130. this.timeLabel = cc.LabelTTF.create(str,"Arial",70);
  131. this.timeLabel.setColor(255,255);
  132. this.timeLabel.setPosition(this.size.width/2,this.size.height/2)
  133. this.addChild(this.timeLabel,1);
  134.  
  135. this.resultAnim = cc.Sprite.create(s_ResultAnim);
  136. this.resultAnim.setPosition(this.size.width/2,this.size.height/2-200);
  137. this.addChild(this.resultAnim,cc.FadeIn.create(1.0));
  138. var rep = cc.RepeatForever.create(action);
  139. this.resultAnim.runAction(rep);
  140.  
  141. document.title = window.wxData.desc = "我的反应速度是"+time+"ms!来试试你的吧!";
  142. this.flag = 2;
  143. }
  144.  
  145. });
  146.  
  147. ///////////////////////////////////////////////////
  148. var MainScene = cc.Scene.extend({
  149. onEnter:function(){
  150. this._super();
  151. var layer = new MainLayer();
  152. layer.init()
  153. this.addChild(layer);
  154. }
  155. });</span>

gtihub地址:https://github.com/iloster/PickTextColor

猜你在找的Cocos2d-x相关文章