第三篇,滑鼠事件與遊戲人物移動
一,假設
假設,所有可添加滑鼠事件的對象,都有一個mouseEvent方法,添加的滑鼠事件同過這個mouseEvent來呼叫。
這樣的話,加入滑鼠事件,其實只需要為canvas新增滑鼠事件,然後循環LGlobal類別裡的childList,也就是循環所有的視覺對象,如果被加入了滑鼠事件,那就呼叫它對應的方法。
二,實作
1,給LGlobal類別追加mouseEvent方法,然後修改LGlobal類別的setCanvas,實作canvas的滑鼠事件的新增與呼叫
LGlobal.setCanvas = function (id,width,height){ LGlobal.canvasObj = document.getElementById(id); if(width)LGlobal.canvasObj.width = width; if(height)LGlobal.canvasObj.height = height; LGlobal.width = LGlobal.canvasObj.width; LGlobal.height = LGlobal.canvasObj.height; LGlobal.canvas = LGlobal.canvasObj.getContext("2d"); LEvent.addEventListener(LGlobal.canvasObj,LMouseEvent.MOUSE_DOWN,function(event){ LGlobal.mouseEvent(event,LMouseEvent.MOUSE_DOWN); }); } LGlobal.mouseEvent = function(event,type){ var key; for(key in LGlobal.childList){ if(LGlobal.childList[key].mouseEvent){ LGlobal.childList[key].mouseEvent(event,type); } } }
2,給LSprite類別新增mouseList數組,用來保存所新增的滑鼠事件,然後添加mouseEvent方法
mouseEvent方法中,我們需要做2個處理,
1),判斷自己是否添加了滑鼠事件,如果沒有添加,則循環它的childList
2),如果添加了滑鼠事件,判斷自己是否被點擊,LSprite雖說意義上是可視類,但是其實目前它本身是不可見的,可見的是它上面的Bitmap,準確地說,是這個Bitmap類中的BitmapData,更準確地說,是這個BitmapData中的Image,所以判斷自己是否被點擊,需要判斷的是LSprite中的childList中的可視對像是否被點擊,如果被點擊,則調用相應的方法
mouseEvent:function (event,type,cood){ if(cood==null)cood={x:0,y:0}; var self = this; if(self.mouseList.length == 0){ for(key in self.childList){ if(self.childList[key].mouseEvent){ self.childList[key].mouseEvent(event,type,{x:self.x+cood.x,y:self.y+cood.y}); } } return; } if(self.childList.length == 0)return; var key; var isclick = false; for(key in self.childList){ isclick = self.childList[key].ismouseon(event,{x:self.x+cood.x,y:self.y+cood.y}); if(isclick)break; } if(isclick){ for(key in self.mouseList){ var obj = self.mouseList[key]; if(obj.type == type){ event.selfX = event.offsetX - (self.x+cood.x); event.selfY = event.offsetY - (self.y+cood.y); event.currentTarget = self; obj.listener(event); } } return; } }, ismouseon:function(event,cood){ var self = this; var key; var isclick = false; for(key in self.childList){ isclick = self.childList[key].ismouseon(event,{x:self.x+cood.x,y:self.y+cood.y}); if(isclick)break; } return isclick; }
ismouseon方法,用來判斷自己是否被點擊
LBitmap類別中也需要判斷是否自己被點擊,所以加入ismouseon
ismouseon:function(event,cood){ var self = this; if(event.offsetX >= self.x + cood.x && event.offsetX <= self.x + cood.x + self.width && event.offsetY >= self.y + cood.y && event.offsetY <= self.y + cood.y + self.height){ return true; }else{ return false; } }
加入滑鼠事件的時候,模仿ActionScript的語法
backLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown);
下面,準備一張地圖,一個人物行走圖,用滑鼠事件控制人物的走動,
init(80,"back",800,480,main); var list = new Array(); var index = 0; var backLayer; //地图 var mapimg; //人物 var playerimg; var loader var imageArray; var animeIndex = 0; var dirindex = 0; var dirarr = new Array({x:0,y:1},{x:-1,y:0},{x:1,y:0},{x:0,y:-1},{x:-1,y:1},{x:1,y:1},{x:-1,y:-1},{x:1,y:-1}); var 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}; //移动目标 var toX = 0; var toY = 0; function main(){ loader = new LLoader(); loader.addEventListener(LEvent.COMPLETE,loadBitmapdata); loader.load("back.jpg","bitmapData"); } function loadBitmapdata(event){ var bitmapdata = new LBitmapData(loader.content); mapimg = new LBitmap(bitmapdata); loader = new LLoader(); loader.addEventListener(LEvent.COMPLETE,loadOver); loader.load("1.png","bitmapData"); } function loadOver(event){ var bitmapdata = new LBitmapData(loader.content,0,0,70,92); imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,8,8); document.getElementById("inittxt").innerHTML=""; playerimg = new LBitmap(bitmapdata); playerimg.bitmapData.setCoordinate(0,0); index = 0; backLayer = new LSprite(); addChild(backLayer); backLayer.addChild(mapimg); backLayer.addChild(playerimg); backLayer.addEventListener(LEvent.ENTER_FRAME, onframe) backLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown); } function onframe(){ index++; if(index >= imageArray[0].length){ index = 0; } var markx = 0,marky = 0; var l = 3; if(playerimg.x > toX){ playerimg.x -= l; markx = -1; }else if(playerimg.x < toX){ playerimg.x += l; markx = 1; } if(playerimg.y > toY){ playerimg.y -= l; marky = -1; }else if(playerimg.y < toY){ playerimg.y += l; marky = 1; } if(markx !=0 || marky != 0){ var mark = markx+","+marky; dirindex = dirmark[mark]; } playerimg.bitmapData.setCoordinate(imageArray[dirindex][index].x,imageArray[dirindex][index].y); } function onmousedown(event){ toX = parseInt(event.selfX/3)*3; toY = parseInt(event.selfY/3)*3; }
以上就是用仿ActionScript的語法來編寫html5——第三篇,滑鼠事件與遊戲人物移動的內容,更多相關內容請關注PHP中文網(www.php.cn)!