標籤:style http color io os ar java for 檔案
主要基於 -------easeljs-0.7.1.min.js----- 去製作這個遊戲
思路:主要思路是以行為單位 綁定可點擊行 選中則講 移動最外層容器繼續綁定可點擊行的下一行 否則結束遊戲
HTML頁面配置
<script src="js/easeljs-0.7.1.min.js"></script><script src="easeljs/drawZfx.js"></script><body><div class="abs" id="time">0</div> <section class="section-box"> <canvas id="canvasId" width="320" height="440">不支援canvas元素</canvas> <a href=" " class="remove">重新開始</a> </section></body>
CSS
*{margin:0;padding:0} body{ font-family: "微軟雅黑"; } .section-box{ width: 320px; height: 480px; margin: 0 auto; text-align: center; } .abs{ position: absolute; width: 100%; text-align: center; font-size: 2rem; font-weight: bold; color: #f00; } .remove{ display: inline-block; width: 100px; height: 26px; text-align: center; line-height: 26px; background: #f00; color: #fff; }
drawZfx.js檔案------ 這個檔案主要來建立矩形方塊 白色和黑色---
/**********************繪製黑白矩形**************************************/function drawZfx(w,h){ createjs.Shape.call(this); var typeValue = 1; this.setType = function(type){ typeValue = type; switch (type){ case 1: this.getDraw1(); break; case 2: this.getDraw2(); break; } } this.getDraw1 = function(){ this.graphics.s("#000").beginFill("#fff").drawRect(0,0,w/4,h/4).endFill(); } this.getDraw2 = function(){ this.graphics.s("#000").beginFill("#000").drawRect(0,0,w/4,h/4).endFill(); } this.getType = function(){ return typeValue; } this.setType(typeValue);}drawZfx.prototype = new createjs.Shape();
核心javascript
var stage = new createjs.Stage("canvasId"); //擷取canvas createjs.Ticker.setFPS(30); //設定幀數頻率createjs.Ticker.addEventListener("tick",stage); //事件var drawView = new createjs.Container(); //建立最外層容器stage.addChild(drawView); //容器添加到canvas中var timeFn; //計時器var text = 0; //時間初始化值0/*****初始化布局*******/function init(w,h,size){ //初始化介面 (寬,高,行容器層數) var view = []; var current = 1; //能點擊的位置預設為倒數第一行 for(var n = size;n>=0;n--){ view[n] = new createjs.Container(); //定義容器為一個行容器 view[n].y = (3-n)*h/4; //給行容器定位 var black = parseInt(Math.random()*4); //隨機黑色塊 for(var l = 0;l <4;l++){ var zfx = new drawZfx(w,h); //建立色塊(預設為白色色塊) zfx.x = l*w/4; //給色塊定位 if(black == l){ zfx.setType(2); //填充為黑色色塊 } view[n].addChild(zfx); //將色塊放入一個行容器中 } if(n == current){ //判斷可點擊行 addCurrent(current,view,h); } drawView.addChild(view[n]); //將行容器放人最外層容器 }}/*****一個過度方法 由這個事件過度到點擊事件******/function addCurrent(current,view,h){ for(var i = 0; i < 4; i++){ clickFn(i,current,view,h); }}/****綁定點擊事件****/function clickFn(i,current,view,h){ view[current].getChildAt(i).addEventListener("click",function(){ /****************** 值1:點中了白色塊 遊戲結束 ******************************/ if(view[current].getChildAt(i).getType() == 1){ alert("遊戲結束,您的成績是:"+text.toFixed(1)+"秒點擊了"+(current-1)+"次黑塊"); clearInterval(timeFn); /****************** 值2:點中了黑色塊 繼續遊戲******************************/ }else if(view[current].getChildAt(i).getType() == 2){ /**********(current == 1)是啟動始化計*****************/ if(current == 1){ text = 0; timeFn = setInterval(function(){ text += 0.1; document.getElementById("time").innerHTML = text.toFixed(1); },100); } drawView.y +=h/4; //最外層容器向下移動 current++; //設定可綁定點擊的行 addCurrent(current,view,h); //回調過度事件 } });}init(320,440,1000); //初始化啟動遊戲
遊戲效果如下
自學easeljs 根據別踩白塊遊戲規則自己寫的代碼