(cocos2d-js游戏)測试你的反应速度----------基本逻辑(上)

   游戏玩法:点击開始游戏。等待一个随机时间。然后背景颜色会变(在t1时刻),这时候你须要点击屏幕(在t2时刻),游戏结束。你的反应时间就是天t2-t1。

 游戏逻辑:

   

游戏逻辑非常easy,如上图所看到的。

一共同拥有五个界面,其有用五张图片就能够取代

主要代码:

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);
	},

	/
	//处理触摸事件
	/
	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();break;
			case 2:this.showResult();break;
			case 3:this.showWaite();break;
			case 4:this.showWaite();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);
		this.flag = 0;
		
	},

	//显示等待界面
	showWaite:function()
	{
		cc.log("showWaite");
		this.removeAllChildren();//清除全部控件
		this.unscheduleAllCallbacks();//清除定时器
		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;
		//输出1-10的随机数
		randomTime = Math.floor(Math.random()*10+1);
		this.schedule(this.showGreen,randomTime,1,0); //加入定时器
		cc.log("randomTime",randomTime);
	},
	//显示点击的页面
	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();//颜色变化后。记录一个时间
	},

	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;
	},
	//显示结果界面
	showResult:function()
	{
		this.unscheduleAllCallbacks();
		this.date2 = new Date();//记录点击时间
		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",70);
		this.timeLabel.setColor(255,255,255);
		this.timeLabel.setPosition(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);
	}
});



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值