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

2016/08/12 15:43:05

MixJuice GET URL : kidspod.club/mj/97

スイカ割り

その名の通り、スイカを割るゲームです。
スマートフォン本体を前に振ってスイカを割ります。
スイカの大きさに合わせて割る強さを加減しましょう。
遊び方がわからない場合は、ヘルプを見てください。

SOURCE CODE

var res = {
  warusuica:"http://masaharu.azurewebsites.net/images/suica-waru2.png",
  waru:"sound/shododen/se_002.m4a",
  gucha:"http://masaharu.azurewebsites.net/images/suica-gucha.png",
  giza1:"http://masaharu.azurewebsites.net/images/suica-giza1.png",
  giza2:"http://masaharu.azurewebsites.net/images/suica-giza2.png",
  giza3:"http://masaharu.azurewebsites.net/images/suica-giza3.png",
  help:"http://masaharu.azurewebsites.net/images/suica-help.png",
};

var MyLayer = cc.KidspodLayer.extend({
  
  _touchFlag:-1,
  _level:0,
  _changeFlag:0,
  _changeFlag2:0,
  _acc:0,
  _acc2:0,
  _power:2,
  _xx:[160, 160, 160, 420, -100, 160],
  _scale:[0.2, 0.3, 0.1, 0.2, 0.1, 0.7],
  _finalSuicaFlag:0,
  _helpFlag:0,
  
  init:function(){
    this._super();
    this.scheduleUpdate();
    var size = cc.director.getWinSize();
    
    var back = cc.Sprite.create("http://masaharu.azurewebsites.net/images/suicaback.png");
    back.setPosition(size.width/2, size.height/2);
    back.setTag(100);
    this.addChild(back);
    
    var suica = ccui.Button.create("http://masaharu.azurewebsites.net/images/suica.png", "", "");
    suica.setPosition(size.width/2, size.height/2);
    suica.setScale(0.2);
    suica.setTag(0);
    this.addChild(suica);
    var ml = this;
    suica.addTouchEventListener(
      function(button, type){
        if(type == ccui.Widget.TOUCH_ENDED){
          console.log("タッチをはなしました。");
          ml._touchFlag = 5;
          ml.removee();
        }
      }
    );
    
    var help = ccui.Button.create(res.help, "", "");
    help.setPosition(50, 50);
    help.setTag(500);
    help.setScale(0.1);
    this.addChild(help);
    var mll = this;
    help.addTouchEventListener(
      function(button, type){
        if(type == ccui.Widget.TOUCH_ENDED){
          if (mll._helpFlag === 0) {
            mll.help();
          } else if (mll._helpFlag === 1) {
            mll.help0();
          }
        }
      }
    );
    
    var touchLabel = cc.LabelTTF.create("スタート");
    touchLabel.setPosition(size.width/2, size.height/2);
    touchLabel.setFontSize(32);
    touchLabel.setColor(cc.color(255, 0, 0, 0));
    touchLabel.setTag(1);
    this.addChild(touchLabel);
    
    var title = cc.Sprite.create("http://masaharu.azurewebsites.net/images/suicatitle.png");
    title.setPosition(size.width/2, size.height-70);
    title.setScale(0.3);
    this.addChild(title);
    
    var stick = cc.Sprite.create("http://masaharu.azurewebsites.net/images/stick.png");
    stick.setPosition(size.width/2, 0);
    stick.setScale(0.3);
    stick.setRotation(90);
    stick.setTag(2);
    this.addChild(stick);
    
    this.schedule(this.flash, 0.5);
    this.schedule(this.wave, 4);
    this.schedule(this.acc, 0.5);
    return true;
  },
  
  help0:function(){
    var suica = this.getChildByTag(0);
    suica.setVisible(true);
    var label = this.getChildByTag(1);
    label.setVisible(true);
    var stick = this.getChildByTag(2);
    stick.setVisible(true);
    this._helpFlag = 0;
    var label1 = this.getChildByTag(777);
    label1.removeFromParent(true);
  },
  
  help:function(){
    var suica = this.getChildByTag(0);
    suica.setVisible(false);
    var label = this.getChildByTag(1);
    label.setVisible(false);
    var stick = this.getChildByTag(2);
    stick.setVisible(false);
    this._helpFlag = 1;
    
    var size = cc.director.getWinSize();
    var label1 = cc.LabelTTF.create("   携帯本体を下に振ると、\n スイカを割ることができます。\n  スイカの大きさによって、\n 割る強さを加減してください。\n周りに注意して遊んでください。");
    label1.setPosition(size.width/2+5, size.height/2-70);
    label1.setColor(cc.color(0, 0, 0, 255));
    label1.setFontSize(20);
    label1.setTag(777);
    this.addChild(label1);
  },
  
  acc:function(){
    var suica = this.getChildByTag(0);
    var size = cc.director.getWinSize();
    if (Math.abs(Math.floor(this._acc-this._acc2))>this._power && suica) {
      if (this._level == 3 && Math.abs(Math.floor(this._acc-this._acc2))>2) {
        suica.removeFromParent(true);
        var suisuica = cc.Sprite.create(res.gucha);
        suisuica.setPosition(size.width/2, size.height/2-50);
        suisuica.setScale(0.2);
        suisuica.setTag(200);
        this.addChild(suisuica);
        this.scheduleOnce(this.gameOver, 1);
        return;
      } else if (this._level == 4) {
        var ssPos = suica.getPosition();
        if (ssPos.x < 197.5 && ssPos.x > 122.5) {
          this._changeFlag2 = 0;
        } else {
          return;
        }
      } else if (this._level == 5) {
          var ssPoss = suica.getPosition();
          if (Math.abs(Math.floor(this._acc-this._acc2))>2) {
            suica.removeFromParent(true);
            var ssuica = cc.Sprite.create(res.gucha);
            ssuica.setPosition(size.width/2, size.height/2-50);
            ssuica.setScale(0.2);
            ssuica.setTag(200);
            this.addChild(ssuica);
            this.scheduleOnce(this.gameOver, 1);
            return;
        }
        if (ssPoss.x < 197.5 && ssPoss.x > 122.5) {
          this._changeFlag2 = 0;
        } else {
          return;
        }
      }
      if (this._finalSuicaFlag == 1) {
        suica.setTexture(res.giza1);
        this._finalSuicaFlag = 2;
        return;
      } else if (this._finalSuicaFlag == 2) {
        suica.setTexture(res.giza2);
        this._finalSuicaFlag = 3;
        return;
      }
      suica.removeFromParent(true);
      var sprite;
      if (this._finalSuicaFlag == 3) {
        sprite = res.giza3;
      } else {
        sprite = res.warusuica;
      }
      var suica2 = cc.Sprite.create(sprite);
      suica2.setPosition(size.width/2, size.height/2-50);
      suica2.setScale(this._scale[this._level-1]);
      suica2.setTag(5);
      this.addChild(suica2);
      cc.audioEngine.playEffect(res.waru);
      this.scheduleOnce(this.nextLevel2, 1);
    }
    this._acc2 = this._acc;
  },
  
  wave:function(){
    cc.audioEngine.playEffect("sound/shododen/se_025.m4a");
  },
  
  removee:function(){
    var suica = this.getChildByTag(0);
    suica.removeFromParent(true);
    var label = this.getChildByTag(1);
    label.removeFromParent(true);
    var stick = this.getChildByTag(2);
    stick.removeFromParent(true);
    var help = this.getChildByTag(500);
    help.setVisible(false);
    this.nextLevel();
  },
  
  flash:function(){
    var suica = this.getChildByTag(0);
    if (!suica || this._touchFlag == 5) return;
    if (this._touchFlag == -1) {
      suica.setScale(0.2);
      this._touchFlag = -2;
    } else if (this._touchFlag == -2) {
      suica.setScale(0.25);
      this._touchFlag = -1;
    } else {
      this.removee();
    }
  },
  
  gameOver:function(){
    var size = cc.director.getWinSize();
    this.unschedule(this.flash);
    this.unschedule(this.acc);
    this.unscheduleUpdate();
    var stick = this.getChildByTag(2);
    stick.removeFromParent(true);
    var label = cc.LabelTTF.create("Game Over");
    label.setPosition(size.width/2, size.height/2);
    label.setFontSize(48);
    label.setColor(cc.color(0, 0, 255));
    label.setTag(999);
    this.addChild(label);
    cc.audioEngine.playEffect("sound/shododen/se_053.m4a");
    var reloadB = ccui.Button.create("res/jewel/je01.png", "", "");
    reloadB.setPosition(size.width/2, 80);
    reloadB.setTag(1010);
    this.addChild(reloadB);
    var reloadL = cc.LabelTTF.create("もう一回");
    reloadL.setPosition(size.width/2, 80);
    reloadL.setTag(1011);
    reloadL.setColor(cc.color(0, 0, 0, 255));
    this.addChild(reloadL);
    var ml = this;
    reloadB.addTouchEventListener(
      function(button, type){
        if(type == ccui.Widget.TOUCH_ENDED){
          ml.reStart();
        }
      }
    );
  },
  
  gameClear:function(){
    this.unschedule(this.flash);
    this.unschedule(this.acc);
    this.unscheduleUpdate();
    this._changeFlag = 0;
    var size = cc.director.getWinSize();
    var label = cc.LabelTTF.create("Game Clear!");
    label.setPosition(size.width/2, size.height/2);
    label.setFontSize(48);
    label.setColor(cc.color(255, 0, 0, 255));
    label.setTag(999);
    this.addChild(label);
    var stick = this.getChildByTag(2);
    stick.removeFromParent(true);
    cc.audioEngine.playEffect("sound/gamese/se_fanfare.m4a");
    var reloadB = ccui.Button.create("res/jewel/je01.png", "", "");
    reloadB.setPosition(size.width/2, size.height/2-100);
    reloadB.setTag(1010);
    this.addChild(reloadB);
    var sprite = this.getChildByTag(5);
    sprite.setTag(200);
    var reloadL = cc.LabelTTF.create("もう一回");
    reloadL.setPosition(size.width/2, size.height/2-100);
    reloadL.setTag(1011);
    reloadL.setColor(cc.color(0, 0, 0, 255));
    this.addChild(reloadL);
    var ml = this;
    reloadB.addTouchEventListener(
      function(button, type){
        if(type == ccui.Widget.TOUCH_ENDED){
          ml.reStart();
        }
      }
    );
  },
  
  reStart:function(){
    var size = cc.director.getWinSize();
    var button = this.getChildByTag(1010);
    button.removeFromParent(true);
    var label = this.getChildByTag(1011);
    label.removeFromParent(true);
    var help = this.getChildByTag(500);
    help.setVisible(true);
    var suica2 = this.getChildByTag(200);
    suica2.removeFromParent(true);
    var label2 = this.getChildByTag(999);
    label2.removeFromParent(true);
    this._level = 0;
    this._touchFlag = -1;
    this._changeFlag = 0;
    this._changeFlag2 = 0;
    this._acc = 0;
    this._acc2 = 0;
    this._power = 2;
    this._finalSuicaFlag = 0;
    this._helpFlag = 0;
    this.schedule(this.flash, 0.5);
    this.schedule(this.acc, 0.5);
    this.scheduleUpdate();
    var suica = ccui.Button.create("http://masaharu.azurewebsites.net/images/suica.png", "", "");
    suica.setPosition(size.width/2, size.height/2);
    suica.setScale(0.2);
    suica.setTag(0);
    this.addChild(suica);
    var touchLabel = cc.LabelTTF.create("スタート");
    touchLabel.setPosition(size.width/2, size.height/2);
    touchLabel.setFontSize(32);
    touchLabel.setColor(cc.color(255, 0, 0, 0));
    touchLabel.setTag(1);
    this.addChild(touchLabel);
    var stick = cc.Sprite.create("http://masaharu.azurewebsites.net/images/stick.png");
    stick.setPosition(size.width/2, 0);
    stick.setScale(0.3);
    stick.setRotation(90);
    stick.setTag(2);
    this.addChild(stick);
    var ml = this;
    suica.addTouchEventListener(
      function(button, type){
        if(type == ccui.Widget.TOUCH_ENDED){
          console.log("タッチをはなしました。");
          ml._touchFlag = 5;
          ml.removee();
        }
      }
    );
  },
  
  nextLevel2:function(){
    var suica = this.getChildByTag(5);
    if (this._finalSuicaFlag != 3) {
      suica.removeFromParent(true);
    }
    this.nextLevel();
  },
  
  nextLevel:function(){
    this._level += 1;
    var size = cc.director.getWinSize();
    if (this._level > 6) {
      this.gameClear();
      return;
    } else if (this._level == 2) {
      this._power = 3;
    } else if (this._level == 3) {
      this._power = 1;
    } else if (this._level == 4) {
      this._power = 2;
    } else if (this._level == 5) {
      this._power = 1;
    } else if (this._level == 6) {
      this._power = 3;
      this._finalSuicaFlag = 1;
    }
    var levelLabel = cc.LabelTTF.create(this._level);
    levelLabel.setPosition(size.width/2+30, size.height/2);
    levelLabel.setVisible(false);
    levelLabel.setFontSize(64);
    levelLabel.setColor(cc.color(0, 0, 0, 255));
    levelLabel.setTag(3);
    this.addChild(levelLabel);
    
    var suica = cc.Sprite.create("http://masaharu.azurewebsites.net/images/suica.png");
    suica.setPosition(-300, size.height/2);
    suica.setTag(4);
    suica.setScale(0.2);
    this.addChild(suica);
    
    var label = cc.LabelTTF.create("Level:");
    label.setPosition(size.width/2-70, size.height/2);
    label.setFontSize(48);
    label.setColor(cc.color(0, 0, 0, 255));
    label.setTag(5);
    this.addChild(label);
    this._changeFlag = 1;
    
    if (this._level > 1) {
      var sstick = this.getChildByTag(2);
      sstick.removeFromParent(true);
    }
  },

  update:function(dt){
    // console.log(this._acc-this._acc2);
    if (this._changeFlag == 1) {
      var size = cc.director.getWinSize();
      var suica = this.getChildByTag(4);
      var sPosX = suica.getPosition().x;
      var sRot = suica.getRotation();
      suica.setPositionX(sPosX-5);
      suica.setRotation(sRot - 3);
      if (sPosX < size.width/2+30) {
        var levelLabel = this.getChildByTag(3);
        levelLabel.setVisible(true);
      }
      if (sPosX < -600) {
        this._changeFlag = 0;
        suica.removeFromParent(true);
        var level2Label = this.getChildByTag(3);
        level2Label.removeFromParent(true);
        var label = this.getChildByTag(5);
        label.removeFromParent(true);
        var suisuica = cc.Sprite.create("http://masaharu.azurewebsites.net/images/suica.png");
        suisuica.setPosition(this._xx[this._level-1], size.height/2-50);
        suisuica.setScale(this._scale[this._level-1]);
        suisuica.setTag(0);
        this.addChild(suisuica);
        var stick = cc.Sprite.create("http://masaharu.azurewebsites.net/images/stick.png");
        stick.setPosition(size.width/2, 0);
        stick.setScale(0.3);
        stick.setRotation(90);
        stick.setTag(2);
        this.addChild(stick);
        if (this._level == 4) {
          this._changeFlag2 = 2;
        } else if (this._level == 5) {
          this._changeFlag2 = 3;
        }
      }
    }
    if (this._changeFlag2 == 2) {
      var suica = this.getChildByTag(0);
      var sPos = suica.getPosition();
      suica.setPositionX(sPos.x-5);
      suica.setRotation(suica.getRotation()-3);
      if (sPos.x == -100) {
        suica.setPositionX(420);
      }
    } else if (this._changeFlag2 == 3) {
      var suica = this.getChildByTag(0);
      var sPos = suica.getPosition();
      suica.setPositionX(sPos.x+15);
      suica.setRotation(suica.getRotation()+9);
      if (sPos.x > 720) {
        suica.setPositionX(-400);
      }
    }
  },
 
  onMouseDown:function(event){
    
  },

  onKeyPressed:function(key, event){
    // if(key == cc.KEY.up)
    // {
        // キーボードの上キーが押されたとき
      // this._acc2 = 2;
      // this._acc = this._power+3;
    // }
    // else if(key == cc.KEY.down)
    // {
    //   // キーボードの下キーが押されたとき
    // }
    // else if(key == cc.KEY.left)
    // {
    //   // キーボードの左キーが押されたとき
    // }
    // else if(key == cc.KEY.right)
    // {
    //   // キーボードの右キーが押されたとき
    // }
    
  },
  
  onTouchBegan:function(touch, event){
      
  },

  onAccelerometer:function(accelero, event){
    var ay = accelero.z;
    this._acc = ay;
  },
  
});



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

COMMENT