第九篇,仿URLLoader讀取檔案
先看看最後的程式碼
function readFile(){ urlloader = new LURLLoader(); urlloader.addEventListener(LEvent.COMPLETE,readFileOk); urlloader.load("../file/test.txt","text"); } function readFileOk(){ mytxt.text = urlloader.data; }
基本上已經實作了Actionscript的模仿了。
效果和程式碼看這裡,看不到效果的請下載支援HTML5的瀏覽器
http://fsanguo.comoj.com/html5/jstoas09/index.html
其實說實作過程
其實說實作過程
if(!file_exists($_POST["file"])){ echo ""; exit; } $file = fopen($_POST["file"],"r"); $filemsg = ""; while (!feof($file)) { $line = fgets($file); $filemsg = $line; } fclose($file); echo $filemsg;把這個php放到你喜歡的位置,然後在legend.js裡面設定路徑LEGEND_FILE_PHP指向你放的位置 關於javascript調用php,當然可以自己寫,因為它不算複雜,但是我是一個很懶的人,所以我直接用jQuery來呼叫了,jquery是什麼?估計不用我解釋了關於LURLLoader的構造,和LLoader基本上一樣,只有load方法不一樣,下面是LURLLoader類的完整代碼,裡面調用了之前準備的php來獲取要讀取的文本
function LURLLoader(){ var self = this; self.objectindex = ++LGlobal.objectIndex; self.type="LURLLoader"; self.loadtype = ""; self.content = null; self.oncomplete = null; self.event = {}; } LURLLoader.prototype = { addEventListener:function(type,listener){ var self = this; if(type == LEvent.COMPLETE){ self.oncomplete = listener; } }, load:function (path,loadtype){ var self = this; self.loadtype = loadtype; if(self.loadtype == "text"){ $.post(LEGEND_FILE_PHP, { flg:"read", file:path },function(data){ if(self.oncomplete){ self.event.currentTarget = data; self.data = data; self.oncomplete(self.event); } }); } } }關於上面的例子,我加了一個按鈕,一個LTextField,程式碼看下面
init(40,"mylegend",600,500,main); var loadingLayer; var backLayer; var urlloader var mytxt; function main(){ legendLoadOver(); var readBtn = addButton("读取",20); readBtn.x = 10; readBtn.y = 20; addChild(readBtn); readBtn.addEventListener(LMouseEvent.MOUSE_DOWN, readFile); mytxt = new LTextField(); mytxt.x = 10; mytxt.y = 50; mytxt.text = ""; mytxt.width = 300; mytxt.height = 200; mytxt.setType(LTextFieldType.INPUT); addChild(mytxt); } function readFileOk(){ mytxt.text = urlloader.data; } function readFile(){ urlloader = new LURLLoader(); urlloader.addEventListener(LEvent.COMPLETE,readFileOk); urlloader.load("../file/test.txt","text"); } function addButton(lbl,x){ var up = new LSprite(); up.graphics.drawRect(1,"black",[0, 0, 80, 20],true,"#999999"); var txt = new LTextField(); txt.x = x; txt.text = lbl; up.addChild(txt); var over = new LSprite(); over.graphics.drawRect(1,"black",[0, 0, 80, 20],true,"#cccccc"); var txt1 = new LTextField(); txt1.x = x; txt1.text = lbl; over.addChild(txt1); var btn = new LButton(up,over); return btn; }以上就是用仿ActionScript的語法來編寫html5——第九篇,仿URLLoader讀取文件的內容,更多相關內容請關注PHP中文網(www.php.cn)!