enchant(); window.focus(); var _DIR_ = SwitchApps.scriptDir(); var SCREEN_W = 800; var SCREEN_H = 600; var BUTTON_SIZE = 130; var BUTTON_SCALE = 1; var BUTTON_NUM = 5; var FISH_SIZE = 130; var FISH_SCALE = SCREEN_W / FISH_SIZE / 10; var TANK_W = SCREEN_W * 4 / 5; var TANK_H = SCREEN_H * 2 / 3; var TANK_X = 0; var TANK_Y = SCREEN_H * 3 / 8; var CAT_SPEED = 50; var TANK_IMG = _DIR_ + '/image/suisou_side.png'; var FISH_IMGS = [_DIR_ + '/image/fish_noback.gif', _DIR_ + '/image/piranha_noback.gif', _DIR_ + '/image/seadragon_noback.png', _DIR_ + '/image/hugu_noback.png', _DIR_ + '/image/ayu_noback.png', _DIR_ + '/image/mutsugoro_noback.png', _DIR_ + '/image/namazu_noback.png', _DIR_ + '/image/unagi_noback.png']; var FISH_SE = _DIR_ + '/sound/water_volup.mp3'; var CAT_SE = _DIR_ + '/sound/cat1a.mp3'; //ゲームオブジェクトとなる変数を宣言 var game = null; //オートスキャン用オブジェクト var scanner = null; var selected_cor = false; var selectCount = 0; // ボタンを押した回数 var correctCount = 0; // 正解を押した回数 //イベントキャラクター var fishes = []; // スキャンの設定 function initScanner(scanner) { scanner.scanSE = 1; scanner.selectSE = 1; scanner.firstWait = 0; scanner.scanInterval = 1.0; scanner.selectWait = 1.0; scanner.frameWidth = 10; scanner.selectFrameWidth = 15; scanner.scanRepaet = -1; scanner.focusType = 'frame'; scanner.focusColor = '#ff0000'; scanner.scanType = 'auto'; scanner.resumeFromNextTarget = true; } // 配列をシャッフル function shuffle(array) { for (var i = array.length - 1; i >= 0; i--) { let rand = Math.floor(Math.random() * (i + 1)); // 配列の数値を入れ替える [array[i], array[rand]] = [array[rand], array[i]] } return array; } // 範囲を指定して乱数を生成 function randRangeInt(min, max) { return Math.floor(Math.random() * (max + 1 - min) + min); } // 範囲を指定して乱数を生成 function randRangeDec(min, max) { return Math.random() * (max + 1 - min) + min; } // ボタンの画像をランダムに入れ替える function buttonPlace(buttons, fishImage) { var correctButtonNum = Math.floor(Math.random() * BUTTON_NUM); for (var i = 0; i < BUTTON_NUM; i++) { if (i == correctButtonNum) { buttons[i].image = game.assets[fishImage]; } else { buttons[i].image = null; } } } window.onload = function () { //ゲームオブジェクトの生成 game = new Core(SCREEN_W, SCREEN_H); config = new Group(SCREEN_W, SCREEN_H / 20); //FPSの指定 game.fps = 16; //画像読み込み game.preload( TANK_IMG, _DIR_ + '/image/fish_noback.gif', _DIR_ + '/image/piranha_noback.gif', _DIR_ + '/image/seadragon_noback.png', _DIR_ + '/image/hugu_noback.png', _DIR_ + '/image/ayu_noback.png', _DIR_ + '/image/mutsugoro_noback.png', _DIR_ + '/image/namazu_noback.png', _DIR_ + '/image/unagi_noback.png', _DIR_ + '/image/cat_noback.gif', FISH_SE, CAT_SE ); //オートスキャン用オブジェクトの作成 scanner = new SpriteScanner(); initScanner(scanner); var buttons = []; // ボタンのインスタンスを格納する配列 // 猫画像の上げ下げに使うフラグ //var catUp = false; //var catDown = false; var nextFish; // 次に生成する魚の画像 //ロード時に呼ばれる処理 game.onload = function () { //背景色の設定 game.rootScene.backgroundColor = "#e1f4f0"; // 背景画像 var tank = new Sprite(SCREEN_W, SCREEN_H); tank.image = game.assets[TANK_IMG]; tank.originY = 0; tank.scaleY = 0.6; tank.opacity = 0.5; tank.moveTo(0, TANK_Y); game.rootScene.addChild(tank); // ネコ var cat = new Sprite(500, 500); cat.image = game.assets[_DIR_ + '/image/cat_noback.gif']; cat.scaleX = 2; cat.scaleY = 2; cat.originX = 0; cat.originY = 0; cat.x = 0; cat.y = SCREEN_H + 20; // スコア表示 var scoreLabel = new Label(); scoreLabel.font = "20px Palatino"; scoreLabel.x = 25; scoreLabel.y = 45; scoreLabel.color = "rgba(0,0,0,0.1)" var scoreLabel20 = new Label(); scoreLabel20.font = "20px Palatino"; scoreLabel20.x = 300; scoreLabel20.y = 45; scoreLabel20.color = "rgba(0,0,0,0.1)" /* var countLabel = new Label(); countLabel.font = "35px Palatino"; countLabel.x = SCREEN_W/2 - countLabel.width/2; countLabel.y = 40; countLabel.color = "rgba(0,127,256,1)"; countLabel.textAlign = "center"; countLabel.text = 0; */ var scoreArray = []; // 各選択肢(ボタン)の処理 for (var i = 0; i < BUTTON_NUM; i++) { //ボタンを生成・配置 buttons[i] = new Button((i * SCREEN_W / BUTTON_NUM) + ((SCREEN_W / BUTTON_NUM) - (BUTTON_SIZE * BUTTON_SCALE)) / 2, SCREEN_H/8); //ボタン選択時処理 buttons[i].addEventListener("scanselect", function () { selectCount += 1; // 選択回数をカウント if (scoreArray.length >= 20) { scoreArray.shift(); } //正解のボタンが選択されたとき if (this.image != null) { correctCount += 1; // 正解数をカウント scoreArray.push(1); //魚を水槽に追加する newFish = new Fish(nextFish, this.x, this.y, 0); // 魚を追加 newFish.scaleX = BUTTON_SCALE; newFish.scaleY = BUTTON_SCALE; speed = randRangeDec(1, 2); game.rootScene.addChild(newFish); // 魚のアニメーション newFish.tl.moveTo((SCREEN_W/2) - ((SCREEN_W/2)-this.x)*0.8, randRangeInt(tank.y + 30, tank.y + tank.height * tank.scaleY - 200 ), game.fps*1, enchant.Easing.BACK_EASEOUT) //.and() //.scaleTo(FISH_SCALE, FISH_SCALE, game.fps*0.5) .then(function(){ // 水槽への移動が完了後 this.speed = speed; nextFish = FISH_IMGS[randRangeInt(0, FISH_IMGS.length - 1)]; //次の魚を決める buttonPlace(buttons, nextFish); //ボタンを並べ直す this.tl.clear(); this.swiming = true; }); this.image = null; //選択したボタンの画像を消す fishes.push(newFish); /* fishes.push(new Fish(nextFish, randRangeInt(SCREEN_W * 1 / 4, SCREEN_W * 3 / 4), randRangeInt(tank.y + 100, tank.y + tank.height * tank.scaleY - 100 ), randRangeDec(1, 2))); // 魚を追加 game.rootScene.addChild(fishes[fishes.length-1]); */ scanner.scanTargetsIndex = -1; } else { scoreArray.push(0); } console.log(scoreArray); scanner.scanStart(3); }); //オブジェクトを表示 game.rootScene.addChild(buttons[i]); } nextFish = FISH_IMGS[randRangeInt(0, FISH_IMGS.length - 1)]; // ボタン画像を設定 buttonPlace(buttons, nextFish); //オートスキャンの対象を設定 scanner.addScanTargets(buttons); // 毎フレームの処理 game.addEventListener(Event.ENTER_FRAME, function () { if (selectCount > 0) { scoreLabel.text = "全体の正解率:" + Math.floor(100 * (correctCount / selectCount)) + "% (" + correctCount + "/" + selectCount + ")"; if (scoreArray.length >= 20) { scoreLabel20.text = "直近20回の正解率:" + Math.floor(100 * scoreArray.reduce((sum, num) => sum + num, 0) / 20) + "% (" + scoreArray.reduce((sum, num) => sum + num, 0) + "/" + 20 + ")"; } } else { scoreLabel.text = "全体の正解率:" + 0 + "% (" + correctCount + "/" + selectCount + ")"; } //魚が10匹まで増えたら全部消す if (fishes.length >= 10) { game.assets[CAT_SE].play(); //猫の鳴き声を再生 game.rootScene.addChild(cat); //猫の移動 cat.tl.moveTo(0, 0, game.fps).then(function(){ // 猫が現れたら魚を消す for (var i = 0; i < fishes.length; i++) { game.rootScene.removeChild(fishes[i]); } fishes = []; }); cat.tl.moveTo(0, SCREEN_H + 20, game.fps).then(function(){ game.rootScene.removeChild(cat); }); } }); // スキャナーのタッチ有効領域を画面全体ではなくゲーム背景領域のみにする scanner.touchEnabled = true; $('html').off(('ontouchstart' in document) ? 'touchstart' : 'click'); // スキャンスピード調整ボタン var speedDown = new UIButton(SCREEN_W / 20, 0, SCREEN_W / 2.5, SCREEN_H / 15, "遅くする"); var speedUp = new UIButton(SCREEN_W - SCREEN_W / 2.5 - SCREEN_W / 20, 0, SCREEN_W / 2.5, SCREEN_H / 15, "速くする"); var speedLabel = new Label(scanner.scanInterval + "秒"); speedLabel.font = "20px Palatino" speedLabel.x = SCREEN_W / 2 - 35; speedLabel.y = 10; speedLabel.width = 70; speedLabel.textAlign = "center"; speedLabel.tl.hide(); //スピードダウンボタンがクリックされたとき speedDown.ontouchstart = function () { if (scanner.scanInterval < 2) { scanner.scanInterval += 0.25; } speedLabel.text = scanner.scanInterval + "秒"; speedLabel.textAlign = "center"; speedLabel.tl.clear().show().delay(2).fadeOut(game.fps * 0.5); } //スピードアップボタンがクリックされたとき speedUp.ontouchstart = function () { if (scanner.scanInterval > 0.25) { scanner.scanInterval -= 0.25; } speedLabel.text = scanner.scanInterval + "秒"; speedLabel.tl.clear().show().delay(2).fadeOut(game.fps * 0.5); } // scan開始 scanner.scanStart(); //オートスキャンを表示 game.rootScene.addChild(scanner); game.rootScene.addChild(scoreLabel); game.rootScene.addChild(scoreLabel20); //game.rootScene.addChild(countLabel); config.addChild(speedDown); config.addChild(speedUp); config.addChild(speedLabel); game.rootScene.addChild(config); }; //ゲームの開始 game.start(); }; // ボタンのクラス var Button = Class.create(Sprite, { initialize: function (x, y) { Sprite.call(this, BUTTON_SIZE, BUTTON_SIZE); this.x = x; this.y = y; this.originX = 0; this.originY = 0; this.scaleX = BUTTON_SCALE; this.scaleY = BUTTON_SCALE; var backGround = new Sprite(BUTTON_SIZE, BUTTON_SIZE); backGround.x = this.x; backGround.y = this.y; backGround.originX = 0; backGround.originY = 0; backGround.scaleX = BUTTON_SCALE; backGround.scaleY = BUTTON_SCALE; var bgImage = new Surface(BUTTON_SIZE*BUTTON_SCALE, BUTTON_SIZE*BUTTON_SCALE); bgImage.context.fillStyle = "white"; bgImage.context.fillRect(0, 0, BUTTON_SIZE, BUTTON_SIZE); backGround.image = bgImage; game.rootScene.addChild(backGround); }, }); // 魚のクラス var Fish = Class.create(Sprite, { initialize: function (image, x, y, speed) { Sprite.call(this, FISH_SIZE, FISH_SIZE); this.image = game.assets[image]; this.speed = speed this.x = x; this.y = y; this.originX = 0; this.originY = 0; var direction = -1; // 魚の向き(1:右 -1:左) this.scaleX = -1 * direction * FISH_SCALE; //元画像が左右逆なので-1倍して反転させている this.scaleY = FISH_SCALE; this.swiming = false; var count = 0; //this.tl.moveTo(x, y+30, game.fps*0.5).moveTo(x, y, game.fps*0.5); //fish_se.play(); game.assets[FISH_SE].play(); this.addEventListener(Event.ENTER_FRAME, function () { if(this.swiming ==true){ //魚が水槽端に来たら反転 if (direction == 1 && this.x >= SCREEN_W - 50 ) { direction = -1; this.scaleX *= (-1); this.x -= Math.abs(this.scaleX) * this.width; //this.y -= Math.sin(Math.PI * (this.x * 10) / SCREEN_H)+2; } else if (direction == -1 && this.x <= 50) { direction = 1; this.scaleX *= (-1); this.x += Math.abs(this.scaleX) * this.width; //this.y -= Math.sin(Math.PI * (this.x * 10) / SCREEN_H)+2; } this.x += this.speed * direction; this.y += Math.sin(Math.PI * (count * 10)/SCREEN_H); count++; } }); } }); var UIButton = Class.create(Sprite, { initialize: function (x, y, width, height, text) { Sprite.call(this,width, height); this.x = x; this.y = y; this.originX = 0; this.originY = 0; //this.className = 'touchable'; var backGround = new Sprite(width, height); backGround.x = this.x; backGround.y = this.y; backGround.originX = 0; backGround.originY = 0; var bgImage = new Surface(width,height); bgImage.context.fillStyle = "rgba(50,50,50,0.05)"; bgImage.context.fillRect(0, 0, width, height); backGround.image = bgImage; var label = new Label(text); label.font ="16px Palatino" label.x = x + width/2 - (text.length/2)*16; label.y = y + height/2 - 8; label.color = "rgba(0,0,0,0.1)"; game.rootScene.addChild(backGround); game.rootScene.addChild(label); }, });