(cocos2d-js遊戲)選擇字型的顏色,cocos2d-js字型

來源:互聯網
上載者:User

(cocos2d-js遊戲)選擇字型的顏色,cocos2d-js字型

  遊戲的基本玩法:從"紅色","黃色","綠色","藍色","紫色","黑色",中隨機選出一個字,然後將字的顏色設定成與文字不同的顏色,如:文字是紅色,字型的顏色是黃色,然後讓玩家選擇字型的顏色,測試反應時間。

     代碼如下:

<span style="font-family:Courier New;">var MainLayer = cc.LayerColor.extend({init:function(){//初始化介面 //0:點擊開始介面 1:表示等待介面,2:表示在點擊介面 3:結果介面 4:Too soon介面this.flag = 0;this._super();this.size = cc.Director.getInstance().getWinSize();this.setColor(cc.c4(180,170,160,255));this.showToStart();//可觸摸this.setTouchEnabled(true);//一些常量this.redColor = new Array(255, 0, 0);this.yellowColor = new Array(255, 255, 0);this.greenColor = new Array(0, 255, 0);this.blueColor = new Array(0, 0, 255 );this.purpleColor = new Array(139, 0, 255);this.blackColor = new Array(0,0,0);this.allColor = new Array(this.redColor,this.yellowColor,this.greenColor,this.blueColor,this.purpleColor,this.blackColor);this.allColorText = new Array("紅色","黃色","綠色","藍色","紫色","黑色");},///////////////////////////////////////////////////處理觸摸事件/////////////////////////////////////////////////onTouchesEnded:function(touches,event){},onTouchesBegan:function(touches,event){cc.log("onTouchsBegan",this.flag);switch(this.flag){case 0:this.showGame();break;//case 1:this.showToSoon();break;case 2:this.showToStart();break;}},//顯示點擊開始介面showToStart:function(){this.sprite = cc.Sprite.create(s_ClickToStart);this.sprite.setPosition(this.size.width/2,this.size.height/2);this.addChild(this.sprite,1);//click to start 動畫this.startAnim = cc.Sprite.create(s_ClickToStartAnim);this.startAnim.setPosition(this.size.width/2,this.size.height/2);this.addChild(this.startAnim,1);var action = cc.Sequence.create(cc.FadeOut.create(1.0),cc.FadeIn.create(1.0));var rep = cc.RepeatForever.create(action);this.startAnim.runAction(rep);this.flag = 0;},//顯示遊戲介面showGame:function(){//顯示背景 // this.sprite = cc.Sprite.create(s_Background); // this.sprite.setPosition(this.size.width/2,this.size.height/2); this.setTouchEnabled(false); this.startDate = new Date(); //產生兩個隨機數 var randomNum1 = Math.floor(Math.random()*6); var randomNum2 = Math.floor(Math.random()*6); //保證兩個隨機數不一樣 while(randomNum1 == randomNum2) { randomNum2 = Math.floor(Math.random()*6); } this.removeAllChildren();//顯示一個文字this.textLabel = cc.LabelTTF.create(this.allColorText[randomNum2],"微軟雅黑",50);this.textLabel.setColor(cc.c3(this.allColor[randomNum1][0], this.allColor[randomNum1][1], this.allColor[randomNum1][2]));this.textLabel.setPosition(this.size.width/2,this.size.height*2/3);this.addChild(this.textLabel,2);//顯示底部按鈕,保證隨機顯示在左右兩邊var randomNum3 = Math.floor(Math.random()*10)%2;var tmp1 = 1;var tmp2 = 2;if(randomNum3 == 0){tmp1 = 1;tmp2 = 2;}else{tmp1 = 2;tmp2 = 1;}cc.log("randomNum3",randomNum3);var rightBtn = cc.MenuItemImage.create(            "res/btn.png",            "res/btn.png",            function () {                          this.showResult(1);            },this);rightBtn.setColor(cc.c3(this.allColor[randomNum1][0], this.allColor[randomNum1][1], this.allColor[randomNum1][2]));        rightBtn.setAnchorPoint(0.5, 0.5);        rightBtn.setPosition(this.size.width*tmp1/3,this.size.height/3);        var errorBtn = cc.MenuItemImage.create(            "res/btn.png",            "res/btn.png",            function () {                              this.showResult(0);            },this);        errorBtn.setAnchorPoint(0.5, 0.5);        errorBtn.setColor(cc.c3(this.allColor[randomNum2][0], this.allColor[randomNum2][1], this.allColor[randomNum2][2]));        errorBtn.setPosition(this.size.width*tmp2/3,this.size.height/3);        var menu = cc.Menu.create(rightBtn,errorBtn);        menu.setPosition(0,0);        this.addChild(menu, 1);      this.flag = 1;},showResult:function(tag){this.setTouchEnabled(true);this.removeAllChildren();var str;this.endDate = new Date();//記錄點擊時間time = this.endDate.getTime() - this.startDate.getTime();this.sprite = cc.Sprite.create(s_Result);this.sprite.setPosition(this.size.width/2,this.size.height/2);this.addChild(this.sprite,1);if(tag == 1){str = time+"ms";}else{str = "ERROR!";}cc.log("showResult",time);this.timeLabel = cc.LabelTTF.create(str,"Arial",70);this.timeLabel.setColor(255,255,255);this.timeLabel.setPosition(this.size.width/2,this.size.height/2)this.addChild(this.timeLabel,1);this.resultAnim = cc.Sprite.create(s_ResultAnim);this.resultAnim.setPosition(this.size.width/2,this.size.height/2-200);this.addChild(this.resultAnim,1);var action = cc.Sequence.create(cc.FadeOut.create(1.0),cc.FadeIn.create(1.0));var rep = cc.RepeatForever.create(action);this.resultAnim.runAction(rep);document.title = window.wxData.desc = "我的反應速度是"+time+"ms!來試試你的吧!";this.flag = 2;}});///////////////////////////////////////////////////var MainScene = cc.Scene.extend({onEnter:function(){this._super();var layer = new MainLayer();layer.init()this.addChild(layer);}});</span>

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.