(cocos2d-js game) test your response speed----------basic logic (ON)

Source: Internet
Author: User
Tags addchild date1

Gameplay: Click to start the game, wait for a random time, then the background color will change (at T1 moment), this time you need to click on the screen (at T2 moment), the game is over. Your reaction time is the day t2-t1.

Game logic:

The game logic is simple, as shown in. A total of five interface, in fact, with five pictures can be replaced

Main code:

var mainlayer = cc. Layercolor.extend ({init:function () {///Initialize interface///0: Click on Start interface 1: Indicates the wait interface, 2: Indicates the interface in Click 3: Result interface 4:too soon interface This.flag = 0;this._super (); this.size = cc. Director.getinstance (). Getwinsize (); This.setcolor (cc.c4 (180,170,160,255)); This.showtostart ();// Touch this.settouchenabled (TRUE);},///////////////////////////////////////////////////handles touch events///////////////////// Ontouchesended:function (touches,event) {},ontouchesbegan:function (touches,event) { Cc.log ("Ontouchsbegan", This.flag), switch (this.flag) {case 0:this.showwaite (); break;case 1:this.showtosoon (); ; case 2:this.showresult (); break;case 3:this.showwaite (); break;case 4:this.showwaite (); break;}},// Display Click Start Interface 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); This.flag = 0;},//Display Wait interface showwaite:function () {Cc.log ("Showwaite"); This.removeallchildren ();// Clear all controls This.unscheduleallcallbacks ();//Clear Timer this.sprite = cc. SprIte.create (S_waiteforgreen); this.sprite.setPosition (THIS.SIZE.WIDTH/2,THIS.SIZE.HEIGHT/2); This.addchild ( this.sprite,1); This.anim = cc. Sprite.create (S_waiteforgreenanim) this.anim.setPosition (this.size.width/2,this.size.height/2+200); This.addchild (this.anim,1); var action = cc. Sequence.create (CC. Fadeout.create (1.0), CC. Fadein.create (1.0)); var rep = cc. Repeatforever.create (Action), This.anim.runAction (rep), This.flag = 1;//output 1-10 random number randomtime = Math.floor ( Math.random () *10+1); This.schedule (this.showgreen,randomtime,1,0); Add Timer cc.log ("Randomtime", Randomtime)},//display the clicked Page Showgreen:function () {Cc.log ("Showgreen"); this.sprite = cc. Sprite.create (S_click); this.sprite.setPosition (THIS.SIZE.WIDTH/2,THIS.SIZE.HEIGHT/2); This.addchild (this.sprite , 1); This.flag = 2;this.date1 = new Date ();//After color change, record a time},showtosoon:function () {Cc.log ("Showtosoon"); This.unscheduleallcallbacks (); this.sprite = cc. Sprite.create (S_toosoon); this.sprite.setPosition (THIS.SIZE.WIDTH/2,THIS.SIZE.HEIGHT/2); This.addchild ( this.sprite,1); This.flag = 3;},//Display Result interface Showresult:function () {this.unscheduleallcallbacks (); this.date2 = new Date ();//Record Click Time This.date2.getTime ()-this.date1.getTime (); this.sprite = cc. Sprite.create (S_result); this.sprite.setPosition (THIS.SIZE.WIDTH/2,THIS.SIZE.HEIGHT/2); This.addchild ( this.sprite,1); Cc.log ("Showresult", time); This.timelabel = cc. Labelttf.create (time+ "MS", "Arial", This.timeLabel.setPosition), This.timeLabel.setColor (255,255,255); THIS.SIZE.WIDTH/2,THIS.SIZE.HEIGHT/2) This.addchild (this.timelabel,1); this.flag = 4;}}); var mainscene = cc. Scene.extend ({onenter:function () {this._super (); var layer = new Mainlayer (); Layer.init () this.addchild (layer);}});



(cocos2d-js game) test your response speed----------basic logic (ON)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.