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

2016/07/07 14:52:59

MixJuice GET URL : kidspod.club/mj/89

織姫を救出せよ!(倉庫番)

ハートを受け皿に移動してね!

要は倉庫番だよ!w

操作方法(スマホ)
 上:画面上タップ
 下:画面下タップ
 左:画面左タップ
 右:画面右タップ

操作方法(PC)
 上:キーボード↑
 下:キーボード↓
 左:キーボード←
 右:キーボード→

マップデータはカスタマイズ可能!(var mapを編集)
 X:壁
 P:彦星
 1:ハート
 9:ハート受け皿

SOURCE CODE

///////////////////////////////////////
// ハートを受け皿に移動させて
// 織姫を救出せよ!
///////////////////////////////////////

var res = {
  block:"res/pancake/5e.png",
  floor:"res/pancake/52.png",
  heart:"res/pancake/45.png",
  heartplace:"res/pancake/7a.png",
  reset:"res/pancake/46.png",

  hikoboshi:"res/pancake/05.png",
  orihime:"res/pancake/07.png",

  //congratulations!
  c:"res/pancake/82.png",
  o:"res/pancake/8e.png",
  n:"res/pancake/8d.png",
  g:"res/pancake/86.png",
  r:"res/pancake/91.png",
  a:"res/pancake/80.png",
  t:"res/pancake/93.png",
  u:"res/pancake/95.png",
  l:"res/pancake/8b.png",
  i:"res/pancake/88.png",
  s:"res/pancake/92.png",
  ex:"res/pancake/a4.png",

};

///////////////////////////////////////
//視覚的に(横縦は同じ数で書く max:10x10)
// X:壁 P:彦星 1:ハート 9:ハート受け皿
///////////////////////////////////////

// var map = [
//   "XXXXXXXXXX",
//   "X........X",
//   "X........X",
//   "X........X",
//   "X........X",
//   "X........X",
//   "X........X",
//   "X........X",
//   "XP19.....X",
//   "XXXXXXXXXX",
// ];
// var map = [
//   "XXXXXXXXXX",
//   "X........X",
//   "X......9.X",
//   "X........X",
//   "X........X",
//   "X........X",
//   "X........X",
//   "X.1......X",
//   "XP.......X",
//   "XXXXXXXXXX",
// ];
var map = [
  "XXXXXXXX",
  "XXXXXXXX",
  "XXXX...X",
  "X9.1.9.X",
  "X.X1X.XX",
  "X..P..XX",
  "XXXXXXXX",
  "XXXXXXXX",
];

var MyLayer = cc.KidspodLayer.extend({

  _hikoboshi:null,
  _pos:cc.PointZero,
  _nBlock:0,

  _arrHeart:[],
  _arrSet:[],

  _bClear:false,

  init:function(){
    this._super();

    for (var i = 0; i < map.length; i++) {

      var hori = map[i];

      for (var j = 0; j < hori.length; j++) {
        var obj = null;
        var type = hori.substr(j, 1);
        var x = cc.winSize.width/2 - 32*((map.length-1)/2-j);
        var y = cc.winSize.height/2 + 32*((map.length-1)/2-i);

        if (type == "X") {
          //block
          obj = new cc.Sprite(res.block);
          obj.setPosition(cc.p(x, y));
          this.addChild(obj);
        } else {
          //floar
          var floor = new cc.Sprite(res.floor);
          floor.setPosition(cc.p(x, y));
          this.addChild(floor);

          if (type == ".") {
            //none
          } else if (type == "1") {
            //heart
            obj = new cc.Sprite(res.heart);
            obj.setPosition(cc.p(x, y));
            obj.setUserData(cc.p(j, i));
            this.addChild(obj, 100);

            this._arrHeart.push(obj);
          } else if (type == "9") {
            //heartplace
            obj = new cc.Sprite(res.heartplace);
            obj.setPosition(cc.p(x, y));
            obj.setUserData(cc.p(j, i));
            this.addChild(obj);

            this._arrSet.push(obj);
          } else if (type == "P") {
            //player
            this._hikoboshi = new cc.Sprite(res.hikoboshi);
            this._hikoboshi.setPosition(cc.p(x, y));
            this.addChild(this._hikoboshi, 100);

            this._pos = cc.p(j, i);
          }
        }
      }
    }

    // var buttonReset = ccui.Button.create(res.reset);
    // buttonReset.setPosition(cc.p(cc.winSize.width/2, cc.winSize.height/2 - 224));
    // buttonReset.addTouchEventListener(function(sender, type) {
    //   switch (type) {
    //     case ccui.Widget.TOUCH_ENDED:
    //       this.reset();
    //     break;
    //   }
    // }, this);
    // this.addChild(buttonReset);

    for (var i = 0; i < this._arrHeart.length; i++) {
      var heart = this._arrHeart[i];
    }

    return true;
  },

  reset:function(){
    if (this._hikoboshi) this._hikoboshi.runAction(cc.removeSelf());
    this._hikoboshi = null;
    this._pos = cc.PointZero;
    this._nBlock = 0;

    this._arrHeart.length = 0;
    this._arrHeart = [];
    this._arrSet.length = 0;
    this._arrSet = [];

    this._bClear = false;

    var tran = cc.TransitionFade.create(0.3, new MyScene())
    cc.director.runScene(tran);
  },

  checkClear:function(){
    for (var i = 0; i < this._arrHeart.length; i++) {
      var heart = this._arrHeart[i];
      var flg = false;
      for (var j = 0; j < this._arrSet.length; j++) {
        var setter = this._arrSet[j];
        if (setter.getUserData().x == heart.getUserData().x && setter.getUserData().y == heart.getUserData().y) {
          flg = true;
        }
      }
      if (!flg) return;
    }

    //clear
    this.clear();
  },

  clear:function(){
    //touch/keyboard無効化
    this._bClear = true;

    var layer = new cc.LayerColor(cc.color(0, 0, 0, 0));
    this.addChild(layer, 200);
    layer.runAction(cc.fadeTo(1, 180));

    var sprite = null;
    for (var i = 0; i < 16; i++) {

      // congratulations!
      if (i == 0) sprite = new cc.Sprite(res.c);
      else if (i == 1) sprite = new cc.Sprite(res.o);
      else if (i == 2) sprite = new cc.Sprite(res.n);
      else if (i == 3) sprite = new cc.Sprite(res.g);
      else if (i == 4) sprite = new cc.Sprite(res.r);
      else if (i == 5) sprite = new cc.Sprite(res.a);
      else if (i == 6) sprite = new cc.Sprite(res.t);
      else if (i == 7) sprite = new cc.Sprite(res.u);
      else if (i == 8) sprite = new cc.Sprite(res.l);
      else if (i == 9) sprite = new cc.Sprite(res.a);
      else if (i == 10) sprite = new cc.Sprite(res.t);
      else if (i == 11) sprite = new cc.Sprite(res.i);
      else if (i == 12) sprite = new cc.Sprite(res.o);
      else if (i == 13) sprite = new cc.Sprite(res.n);
      else if (i == 14) sprite = new cc.Sprite(res.s);
      else if (i == 15) sprite = new cc.Sprite(res.ex);

      sprite.setPosition(cc.p(cc.winSize.width/2 - 135 + i*(16+2), cc.winSize.height/2 + 150));
      sprite.setScale(0.75);
      sprite.setOpacity(0);
      layer.addChild(sprite);

      sprite.runAction(cc.sequence(cc.delayTime(i*0.05), cc.fadeTo(0.1, 255)));

      // heart
      sprite = new cc.Sprite(res.heart);
      sprite.setPosition(cc.p(cc.winSize.width/2 + 80*Math.cos(360/16*i/180*Math.PI), cc.winSize.height/2 + 80*Math.sin(360/16*i/180*Math.PI)));
      sprite.setScale(0.75);
      sprite.setOpacity(0);
      layer.addChild(sprite);

      sprite.runAction(cc.repeat(cc.sequence(cc.fadeTo((Math.floor(Math.random()*6)+5)/10, 255), cc.fadeTo((Math.floor(Math.random()*6)+5)/10, 0)), 10000));
    }

    var hikoboshi = new cc.Sprite(res.hikoboshi);
    hikoboshi.setPosition(cc.p(cc.winSize.width/2 - 15, cc.winSize.height/2));
    hikoboshi.setOpacity(0);
    layer.addChild(hikoboshi);

    hikoboshi.runAction(cc.fadeTo(0.5, 255));

    var orihime = new cc.Sprite(res.orihime);
    orihime.setPosition(cc.p(cc.winSize.width/2 + 15, cc.winSize.height/2));
    orihime.setOpacity(0);
    layer.addChild(orihime);

    orihime.runAction(cc.fadeTo(0.5, 255));

    //
    cc.audioEngine.playEffect("sound/gamese/se_fanfare.m4a");
  },

  getHeart:function(nextPos){
    for (var i = 0; i < this._arrHeart.length; i++) {
      var heart = this._arrHeart[i];
      if (nextPos.x == heart.getUserData().x && nextPos.y == heart.getUserData().y) {
        return heart;
      }
    }
    return null;
  },

  onAction:function(nextPosAdd) {
    if (this._hikoboshi.getNumberOfRunningActions() > 0) return;

    var nextPos = cc.p(this._pos.x + nextPosAdd.x, this._pos.y + nextPosAdd.y);
    var nextType = map[nextPos.y][nextPos.x];

    if (nextType == "X") {
      console.log("ドゥ1 x%d, y:%d", nextPos.x, nextPos.y);
      cc.audioEngine.playEffect("sound/piano/c1_"+(this._nBlock%10+1)+".m4a");
      ++this._nBlock;
      return;
    }

    var heart = this.getHeart(nextPos);
    if (heart) {
      var nextPos2 = cc.p(nextPos.x + nextPosAdd.x, nextPos.y + nextPosAdd.y);
      var nextType2 = map[nextPos2.y][nextPos2.x];

      var heart2 = this.getHeart(nextPos2);
      if (heart2) {
        console.log("ドゥ2 x%d, y:%d", nextPos2.x, nextPos2.y);
        cc.audioEngine.playEffect("sound/piano/c1_"+(this._nBlock%10+1)+".m4a");
        ++this._nBlock;
        return;
      }

      if (nextType2 == "X") {
        console.log("ドゥ3 x%d, y:%d", nextPos2.x, nextPos2.y);
        cc.audioEngine.playEffect("sound/piano/c1_"+(this._nBlock%10+1)+".m4a");
        ++this._nBlock;
        return;
      }

      // heart
      heart.runAction(cc.moveTo(0.3, cc.p(cc.winSize.width/2 - 32*((map.length-1)/2-nextPos2.x), cc.winSize.height/2 + 32*((map.length-1)/2-nextPos2.y))));
      heart.setUserData(cc.p(heart.getUserData().x + nextPosAdd.x, heart.getUserData().y + nextPosAdd.y));

      //player
      this._hikoboshi.runAction(cc.moveTo(0.3, cc.p(cc.winSize.width/2 - 32*((map.length-1)/2-nextPos.x), cc.winSize.height/2 + 32*((map.length-1)/2-nextPos.y))));
      this._pos = nextPos;

      //check
      this.runAction(cc.sequence(cc.delayTime(0.3),
                                 cc.callFunc(this.checkClear, this)));
    } else {
      //player
      this._hikoboshi.runAction(cc.moveTo(0.3, cc.p(cc.winSize.width/2 - 32*((map.length-1)/2-nextPos.x), cc.winSize.height/2 + 32*((map.length-1)/2-nextPos.y))));
      this._pos = nextPos;

      //check
      this.runAction(cc.sequence(cc.delayTime(0.3),
                                 cc.callFunc(this.checkClear, this)));
    }
  },

  onKeyPressed:function(key, event){
    if (this._bClear) return;

    var nextPosAdd = cc.PointZero;
    if      (key == cc.KEY.up) nextPosAdd = cc.p(0, -1);
    else if (key == cc.KEY.down) nextPosAdd = cc.p(0, 1);
    else if (key == cc.KEY.left) nextPosAdd = cc.p(-1, 0);
    else if (key == cc.KEY.right) nextPosAdd = cc.p(1, 0);

    this.onAction(nextPosAdd);
  },

  onTouchBegan:function(touch, event){
    if (this._bClear) return;

    var x = touch.getLocationInView().x - cc.winSize.width/2;
    var y = touch.getLocationInView().y - cc.winSize.height/2;

    var nextPosAdd = cc.PointZero;
    if      (x < 80 && x > -80 && y > 0 && y < 160) nextPosAdd = cc.p(0, -1);
    else if (x < 80 && x > -80 && y < 0 && y > -160) nextPosAdd = cc.p(0, 1);
    else if (x < 0 && x > -160 && y < 80 && y > -80) nextPosAdd = cc.p(-1, 0);
    else if (x > 0 && x < 160 && y < 80 && y > -80) nextPosAdd = cc.p(1, 0);

    this.onAction(nextPosAdd);
  },

});

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

COMMENT

Kidspod運営チーム
Kidspod運営チーム2016/11/24 09:06:25

思っていた以上に難しかったです。ステージを簡単にカスタマイズできるところがとても良いです!