Enjoy programming more!
Kidspod is a post site which supports young programmers.
First step to join us.
Register as a member
For members

2015/11/27 20:12:38

MixJuice GET URL : kidspod.club/mj/39

ARE WE JEWEL?

ARE WE JEWEL?(ぼくたちは宝石?)
クリック(タップ)するたびに宝石が落ちてきます。
はたして、その正体は……?

※音が鳴るから気をつけてね。

SOURCE CODE

var res = {
	je01:"res/jewel/je01.png",
	je02:"res/jewel/je02.png",
	je03:"res/jewel/je03.png",
	je04:"res/jewel/je04.png",
	maru01:"res/mochilith/maru01.png",
	maru02:"res/mochilith/maru02.png",
	maru03:"res/mochilith/maru03.png",
	maru04:"res/mochilith/maru04.png",
};

var nClickTag = 100;
var nFlashTag = 101;

var MyLayer = cc.KidspodLayer.extend({
	nTotalColor:4,
	nTotalNum:35,
	nYokoMax:5,

	fDelayTime:0.5,
	nTag:0,

	bEnable:true,

	init:function(){
		this._super();
		this.scheduleUpdate();
		var size = cc.director.getWinSize();

		var lblClick = cc.LabelTTF.create("クリック クリック !!");
		lblClick.setPosition(size.width/2, size.height/2);
		lblClick.setTag(nClickTag);
		this.addChild(lblClick);

		var spFlash = cc.Sprite.create("res/roaddragon/back.png");
		spFlash.setPosition(size.width/2, size.height/2);
		spFlash.setVisible(false);
		spFlash.setTag(nFlashTag);
		this.addChild(spFlash);

		return true;
	},

	update:function(dt){
		if(!this.bEnable) this.bEnable = true;
	},

	getColorNumber:function(nTag){
		return nTag % this.nTotalColor + 1;
	},

	playSound:function(nTag){
		var nID = nTag + 54;
		cc.audioEngine.playEffect("sound/se_0" + nID + ".m4a");
	},

	fallJewel:function(nTag){
		var size = cc.director.getWinSize();
		var spJewel = cc.Sprite.create("res/jewel/je0" + this.getColorNumber(nTag) + ".png");
		var fScale = (size.width/this.nYokoMax)/spJewel.getContentSize().width;
		var sizeJewel = cc.size(spJewel.getContentSize().width * fScale, spJewel.getContentSize().height * fScale);
		var posX = sizeJewel.width/2 + sizeJewel.width * (nTag % this.nYokoMax);
		spJewel.setScale(fScale);
		spJewel.setTag(nTag);
		spJewel.setPosition(posX, size.height + sizeJewel.height);
		this.addChild(spJewel);

		var posY = sizeJewel.height/2 + sizeJewel.height * Math.floor(nTag/this.nYokoMax);
		var pos	= cc.p(posX, posY);
		// spJewelをfDelayTime秒かけてposまで動かします
		spJewel.runAction(cc.MoveTo.create(this.fDelayTime, pos));
	},

	becomeMaru:function(){
		var lblClick = this.getChildByTag(nClickTag);
		if(lblClick) lblClick.removeFromParent(true);

		for(var i = 0; i < this.nTotalNum; i++)
		{
			var spJewel = this.getChildByTag(i);
			if(spJewel)
			{
				var spMaru = cc.Sprite.create("res/mochilith/maru0" + this.getColorNumber(i) + ".png");
				spMaru.setScale(spJewel.getScale());
				spMaru.setPosition(spJewel.getPosition());

				spJewel.removeFromParent(true);
				this.addChild(spMaru);
			}
		}

		cc.audioEngine.playEffect("sound/se_049.m4a");
		var spFlash = this.getChildByTag(nFlashTag);
		if(spFlash) spFlash.setVisible(true);
	},

	onMouseDown:function(event){
		if(!this.bEnable) return;


		this.bEnable = false;

		if(this.nTag < this.nTotalNum)
		{
			this.playSound(this.nTag);
			this.fallJewel(this.nTag);
		}
		else if(this.nTag == this.nTotalNum)
		{
			this.playSound(this.nTag);
			this.scheduleOnce(this.becomeMaru, this.fDelayTime);
		}

		this.nTag++;
	},

	onTouchBegan:function(touch, event){
		this.onMouseDown(event);
	},
});

COMMENT