這次用繼承自LSprite的類別來實現簡單的rpg的demo
先看一下最後的程式碼與as的相似度
var backLayer; //地图 var mapimg; //人物 var playerimg; var loader var imageArray; var loadIndex = 0; var imgData = new Array({name:"back.jpg",img:null},{name:"1.png",img:null},{name:"2.png",img:null}); var chara; var charaList; function main(){ loadImage(); } function loadImage(){ if(loadIndex >= imgData.length){ gameInit(); return; } loader = new LLoader(); loader.addEventListener(LEvent.COMPLETE,loadComplete); loader.load(imgData[loadIndex].name,"bitmapData"); } function loadComplete(event){ imgData[loadIndex].img = loader.content; loadIndex++; loadImage(); } function gameInit(event){ var bitmapdata; bitmapdata = new LBitmapData(imgData[0].img); mapimg = new LBitmap(bitmapdata); document.getElementById("inittxt").innerHTML=""; backLayer = new LSprite(); addChild(backLayer); backLayer.addChild(mapimg); bitmapdata = new LBitmapData(imgData[1].img,0,0,70,92); imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,8,8); playerimg = new LBitmap(bitmapdata); chara = new CharacterSprite(true,playerimg,imageArray,0,0); backLayer.addChild(chara); charaList = new Array(); for(var i=0;i<10;i++){ bitmapdata = new LBitmapData(imgData[2].img,0,0,80,91); imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,8,8); playerimg = new LBitmap(bitmapdata); var npcx = parseInt(Math.random()*800/3)*3; var npcy = parseInt(Math.random()*480/3)*3; var npc = new CharacterSprite(false,playerimg,imageArray,npcx,npcy); backLayer.addChild(npc); charaList.push(npc); } backLayer.addEventListener(LEvent.ENTER_FRAME, onframe) backLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown); } function onframe(){ chara.onframe(); for(var i=0;i<charaList.length;i++){ charaList[i].onframe(); } } function onmousedown(event){ chara.toCoordinate.x = parseInt(event.selfX/3)*3; chara.toCoordinate.y = parseInt(event.selfY/3)*3; }
應該還算可以吧?
看一下成果吧,看不到效果的請下載支援html5的瀏覽器
http://fsanguo.comoj.com/html5/jstoas03/index.html
下面說一說如何繼承,繼承的話,js沒有辦法像as那樣繼承,
js的繼承看下面
function base(derive,baseSprite,baseArgs){ baseSprite.apply(derive,baseArgs); for(prop in baseSprite.prototype){ var proto = derive.constructor.prototype; if(!proto[prop]){ proto[prop] = baseSprite.prototype[prop]; } } }
三個參數分別是child,base,base建構器參數數組
這個方法可以實現js的完美繼承
現在來建立一個繼承自LSprite的類別CharacterSprite
只需要在建構器裡呼叫base(this,LSprite,[])就可以實現繼承
而且CharacterSprite因為繼承了LSprite的方法,所以它有addChild等方法
CharacterSprite類別程式碼如下
function CharacterSprite(ishero,bitmap,imageArray,x,y){ base(this,LSprite,[]); var self = this; self.x = x; self.y = y; self.toCoordinate = {x:x,y:y}; self.ishero = ishero; self.animeIndex = 0; self.dirindex = 0; self.dirmark = {"0,1":0,"-1,0":1,"1,0":2,"0,-1":3,"-1,1":4,"1,1":5,"-1,-1":6,"1,-1":7}; self.bitmap = bitmap; self.imageArray = imageArray; self.addChild(bitmap); } CharacterSprite.prototype.onframe = function (){ var self = this; self.animeIndex++; if(self.animeIndex >= self.imageArray[0].length){ self.animeIndex = 0; } var markx = 0,marky = 0; var l = 3; if(self.x > self.toCoordinate.x){ self.x -= l; markx = -1; }else if(self.x < self.toCoordinate.x){ self.x += l; markx = 1; } if(self.y > self.toCoordinate.y){ self.y -= l; marky = -1; }else if(self.y < self.toCoordinate.y){ self.y += l; marky = 1; } if(markx !=0 || marky != 0){ var mark = markx+","+marky; self.dirindex = self.dirmark[mark]; }else if(!self.ishero){ if(self.index > 0){ self.index -= 1; }else{ self.index = parseInt(Math.random()*300); self.toCoordinate.x = parseInt(Math.random()*800/3)*3; self.toCoordinate.y = parseInt(Math.random()*480/3)*3; } } self.bitmap.bitmapData.setCoordinate(self.imageArray[self.dirindex][self.animeIndex].x,self.imageArray[self.dirindex][self.animeIndex].y); }
以上就是用仿ActionScript的文法來寫html5-第四篇,繼承簡單的rpg的內容,更多相關內容請關注PHP中文網(www.php.cn)!