プログラミングをもっと楽しく!
Kidspod;はこどものプログラマーを
応援するための投稿サイトです。
はじめての人はこちらから
新規メンバー登録
メンバーの人はこちらから

2015/11/04 10:51:44

MixJuice GET URL : kidspod.club/mj/38

WE ARE ANIMAL

WE ARE ANIMAL(ぼくたちは動物)
クリック(タップ)するたびに動物があらわれます。
マイアプリで効果音を鳴らして遊んじゃおう!

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

SOURCE CODE

var res = {
  // 効果音をプリロードします
  se_animal:"sound/se_017.m4a",
  se_word:"sound/se_022.m4a",
  se_finish:"sound/se_049.m4a"
};

var nTag = 0;
var nReadyTag = ++nTag;
var nFlashTag = ++nTag;

var arrAnimal = [
  "buta",
  "coara",
  "hebi",
  "hitsuji",
  "inu",
  "kitsune",
  "lion",
  "neko",
  "nezumi",
  "panda",
  "pengin",
  "risu",
  "saru",
  "tanuki",
  "tori2",
  "usa",
];

var arrChar = [
  ["96","84"],
  ["80","91","84"],
  ["80","8d","88","8c","80","8b"]
];

var MyLayer = cc.KidspodLayer.extend({
  nAnimal:0,
  nWord:0,

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

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

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

    return true;
  },

  showWord:function(dt){
    var lblReady = this.getChildByTag(nReadyTag);
    if(lblReady) lblReady.removeFromParent(true);

	cc.audioEngine.playEffect(res.se_word);    // 効果音がなります
    this.addSprites(arrChar[this.nWord], 320 - 80 * this.nWord);

    this.nWord++;
    if(this.nWord == arrChar.length)
    {
      cc.audioEngine.playEffect(res.se_finish);
      this.unschedule(this.showWord);

      var lblFlash = this.getChildByTag(nFlashTag);
      if(lblFlash) lblFlash.setVisible(true);
    }
  },

  addSprites:function(arrFile, Y){
    var size = cc.director.getWinSize();
    var width_char = 32;
    var x_init = size.width/2 - (arrFile.length * width_char)/2 + width_char/2;
    for(var i = 0; i < arrFile.length; i++)
    {
      var sp = cc.Sprite.create("res/pancake/" + arrFile[i] + ".png");
      sp.setPosition(x_init + 32 * i, Y);
      this.addChild(sp);
    }
  },

  onMouseDown:function(event) {
    if(this.nAnimal >= arrAnimal.length) return;

    var sp = cc.Sprite.create("res/animal/" + arrAnimal[this.nAnimal] + ".png");
    sp.setScale(0.5);
    sp.setPosition(this.getAnimalPosition());
    this.addChild(sp);
    cc.audioEngine.playEffect(res.se_animal);

    this.nAnimal++;
    if(this.nAnimal == arrAnimal.length)
    {
      this.schedule(this.showWord, 0.5);
    }
  },

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

  getAnimalPosition:function(){
    var size  = cc.director.getWinSize();
    var nHeight = 80;
    var nWidth  = 75;
    
    var x, y;
    if(this.nAnimal < arrAnimal.length/2)
    {
      x = nWidth/2;
    }
    else
    {
      x = size.width - nWidth/2;
    }

    y = size.height - nHeight * (this.nAnimal % (arrAnimal.length/2));

    return cc.p(x, y);
  },  
});


var MyScene = cc.Scene.extend({
  onEnter:function (){
    this._super();
    var layer = new MyLayer();
    layer.init();
    this.addChild(layer);
  }
});

COMMENT