Article 3: mouse events and game character Movement
Write HTML5 using the syntax similar to ActionScript-the first article shows an image
Http://blog.csdn.net/lufy_legend/article/details/6753032
Write HTML5 using the syntax similar to ActionScript -- Article 2: Use sprite to implement animation
Http://blog.csdn.net/lufy_legend/article/details/6760812
I. Hypothesis
Assume that all the objects that can add mouse events have a mouseevent method. The added mouse event is called like this mouseevent.
In this case, to add a mouse event, you only need to add a mouse event to the canvas, and then loop through childlist in the lglobal class, that is, loop through all the visible objects. If a mouse event is added, then the corresponding method is called.
II. Implementation
1. append the mouseevent method to the lglobal class, and modify the setcanvas of the lglobal class to add and call the canvas mouse event.
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. Add the mouselist array to the lsprite class to save the added mouse event, and then add the mouseevent method.
In the mouseevent method, we need to perform two operations,
1), judge whether you have added a mouse event. If no mouse event is added, cycle its childlist
2) if a mouse event is added to determine whether it is clicked, lsprite is visible, but it is invisible at present, what we can see is the bitmap above it. To be accurate, it is bitmapdata In the bitmap class. To be more accurate, it is the image in the bitmapdata class, so we can determine whether we have been clicked, check whether the visual object in childlist in lsprite is clicked. If clicked, call the corresponding method.
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 method, used to determine whether a user is clicked
In the lbitmap class, you also need to determine whether you have been clicked.
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;}}
When a mouse event is added, it imitates the ActionScript syntax.
backLayer.addEventListener(LMouseEvent.MOUSE_DOWN, onmousedown);
Next, prepare a map, a walking chart of a person, and use mouse events to control the movement of the person,
Init (80, "back", 800,480, main); var list = new array (); var Index = 0; var backlayer; // map var mapimg; // var playerimg; var loadervar 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, "": 2, "0,-1": 3, "-": 4, "": 5, "-1,-1": 6, "1,-1": 7}; // move the target var tox = 0; var toy = 0; f Unction 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 L Bitmapdata (loader. content,); 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. en Ter_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 ;}
Let's take a look at the results.
Http://fsanguo.comoj.com/html5/jstoas02/index.html
Next, study inheritance.