In the previous article, I have imitated as, added the LBitmap and LBitmapData classes, and used them to display static images.
This time, Sprite is used to dynamically display images.
Still following the idea of processing display objects in the previous article, add the LSprite class and add the show method, as follows:
function LSprite(){ var self = this; self.type = "LSprite"; self.x = 0; self.y = 0; self.visible=true; self.childList = new Array() } LSprite.prototype = { show:function (cood){ if(cood==null)cood={x:0,y:0}; var self = this; if(!self.visible)return; LGlobal.show(self.childList,{x:self.x+cood.x,y:self.y+cood.y}); }, addChild:function (DisplayObject){ var self = this; self.childList.push(DisplayObject); } }
Because Sprite can have pictures and other displayable objects, so I In its constructor, childList is added to save all objects on it. Then when calling its own show method, loop its LGlobal to display its sub-objects.
In this way, the code for displaying images in our previous article can also be displayed using Sprite. The code is as follows:
function main(){ loader = new LLoader(); loader.addEventListener(LEvent.COMPLETE,loadBitmapdata); loader.load("1.png","bitmapData"); } function loadBitmapdata(event){ var bitmapdata = new LBitmapData(loader.content); var mapimg = new LBitmap(bitmapdata); var backLayer = new LSprite(); addChild(backLayer); backLayer.addChild(mapimg); }
We know that the Sprite in actionscript can add the EnterFrame event for dynamic Displaying pictures, I will imitate it here, because the show method in the LSprite class is constantly looping, so I only need to continuously call a method in the show method to make it loop.
I assume there is an array that stores all the methods that continuously loop. Then I can loop this array in the show method, so that the loop of all methods is achieved. See below
function LSprite(){ var self = this; self.type = "LSprite"; self.x = 0; self.y = 0; self.visible=true; self.childList = new Array() self.frameList = new Array(); } LSprite.prototype = { show:function (cood){ if(cood==null)cood={x:0,y:0}; var self = this; if(!self.visible)return; LGlobal.show(self.childList,{x:self.x+cood.x,y:self.y+cood.y}); self.loopframe(); }, loopframe:function (){ var self = this; var key; for(key in self.frameList){ self.frameList[key](); } }, addChild:function (DisplayObject){ var self = this; self.childList.push(DisplayObject); } }
Light Assuming that is of course not possible, we need a method to add this loop event, so we also need the addEventListener method, and the removeEventListener method to remove this event
addEventListener:function (type,listener){ var self = this; if(type == LEvent.ENTER_FRAME){ self.frameList.push(listener); } }, removeEventListener:function (type,listener){ var self = this; var i,length = self.frameList.length; for(i=0;i<length;i++){ if(type == LEvent.ENTER_FRAME){ self.frameList.splice(i,1); break; } } }
Everything that should be added has been added, next, here Simple implementation of a character's walking chart
First add a few methods to the BitmapData class to change the area and position of the picture display, etc.
LBitmapData.prototype = { setProperties:function (x,y,width,height){ var self = this; self.x = x; self.y = y; self.width = width; self.height = height; }, setCoordinate:function (x,y){ var self = this; self.x = x; self.y = y; } }
Okay, now prepare a character's walking chart, this is Let it move
var list = new Array(); var index = 0; var mapimg; 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}); function main(){ loader = new LLoader(); loader.addEventListener(LEvent.COMPLETE,loadBitmapdata); loader.load("1.png","bitmapData"); } function loadBitmapdata(event){ var bitmapdata = new LBitmapData(loader.content,0,0,70,92); imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,8,8); mapimg = new LBitmap(bitmapdata); mapimg.x = 100; mapimg.bitmapData.setCoordinate(0,0); index = 0; var backLayer = new LSprite(); addChild(backLayer); backLayer.addChild(mapimg); backLayer.addEventListener(LEvent.ENTER_FRAME, onframe) } function onframe(){ index++; if(index >= imageArray[0].length){ index = 0; } mapimg.bitmapData.setCoordinate(imageArray[dirindex][index].x,imageArray[dirindex][index].y); mapimg.x += dirarr[dirindex].x*3; mapimg.y += dirarr[dirindex].y*3; if(animeIndex++ > 20){ dirindex++; if(dirindex > 3)dirindex = 0; animeIndex = 0; } }
The above is about using ActionScript-like syntax to write html5 - the second part, using Sprite to realize animation content. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com )!