Using the syntax of ActionScript to write the second html5--, using a sprite to achieve animation

Source: Internet
Author: User
Tags addchild

This paper is the official HTML5 training course for Brother Lian it educational institution, mainly introduces: writing html5--with the syntax of ActionScript, using Sprite to realize animation

In the previous article, I have imitated as, joined the Lbitmap and Lbitmapdata classes, and used them to achieve the display of static pictures.
This time use a sprite to animate the picture.
Still follow the previous idea of handling the display object, add the Lsprite class, and append the Show method, as follows:

[JavaScript]View PlainCopy
  1. function Lsprite () {
  2. var = this ;
  3. Self.type = "Lsprite";
  4. self.x = 0;
  5. SELF.Y = 0;
  6. Self.visible=true;
  7. Self.childlist = new Array ()
  8. }
  9. Lsprite.prototype = {
  10. Show:function (cood) {
  11. if (cood==null) cood={x:0,y:0};
  12. var = this ;
  13. if (!self.visible)return;
  14. Lglobal.show (Self.childlist,{x:self.x+cood.x,y:self.y+cood.y});
  15. },
  16. AddChild:function (displayobject) {
  17. var = this ;
  18. Self.childList.push (Displayobject);
  19. }
  20. }



Because the sprite can have other visible objects such as pictures, I added Childlist in its constructor to hold all the objects above it. Then, when invoking its own Show method, it lglobal its sub-objects in a loop.
In this way, the code for the image shown in our previous article can also be displayed with a Sprite, with the following code:

[JavaScript]View PlainCopy
  1. function Main () {
  2. Loader = new Lloader ();
  3. Loader.addeventlistener (Levent.complete,loadbitmapdata);
  4. Loader.load ("1.png","BitmapData");
  5. }
  6. function Loadbitmapdata (event) {
  7. var bitmapdata = new Lbitmapdata (loader.content);
  8. var mapimg = new Lbitmap (BitmapData);
  9. var backlayer = new Lsprite ();
  10. AddChild (Backlayer);
  11. Backlayer.addchild (MAPIMG);
  12. }



We know that the sprite in ActionScript can add the Enterframe event to animate the picture, and I'll imitate it here, because the Show method in the Lsprite class is constantly looping, so I just need to call a method in the Show method constantly, You can make it loop.
I assume that there is an array that stores all the methods that are constantly looping, and then I can loop through the array in the Show method, so that the loop of all the methods is reached, see below

[JavaScript]View PlainCopy
  1. function Lsprite () {
  2. var = this ;
  3. Self.type = "Lsprite";
  4. self.x = 0;
  5. SELF.Y = 0;
  6. Self.visible=true;
  7. Self.childlist = new Array ()
  8. Self.framelist = new Array ();
  9. }
  10. Lsprite.prototype = {
  11. Show:function (cood) {
  12. if (cood==null) cood={x:0,y:0};
  13. var = this ;
  14. if (!self.visible)return;
  15. Lglobal.show (Self.childlist,{x:self.x+cood.x,y:self.y+cood.y});
  16. Self.loopframe ();
  17. },
  18. Loopframe:function () {
  19. var = this ;
  20. var key;
  21. For (key in self.framelist) {
  22. Self.framelist[key] ();
  23. }
  24. },
  25. AddChild:function (displayobject) {
  26. var = this ;
  27. Self.childList.push (Displayobject);
  28. }
  29. }



The light hypothesis is certainly not possible, we need to have a way to add this loop event, so we also need to addeventlistener the method, and the RemoveEventListener method to remove the event

[JavaScript]View PlainCopy
  1. AddEventListener:function (type,listener) {
  2. var = this ;
  3. if (type = = Levent.enter_frame) {
  4. Self.frameList.push (listener);
  5. }
  6. },
  7. RemoveEventListener:function (type,listener) {
  8. var = this ;
  9. var i,length = self.frameList.length;
  10. For (i=0;i<length;i++) {
  11. if (type = = Levent.enter_frame) {
  12. Self.frameList.splice (i,1);
  13. Break ;
  14. }
  15. }
  16. }



The addition is added, then, to simply implement a character's walking diagram
First, add a few methods to the BitmapData class to change the location of the image display area, etc.

[JavaScript]View PlainCopy
  1. Lbitmapdata.prototype = {
  2. SetProperties:function (x,y,width,height) {
  3. var = this ;
  4. self.x = x;
  5. Self.y = y;
  6. Self.width = width;
  7. Self.height = height;
  8. },
  9. Setcoordinate:function (x, y) {
  10. var = this ;
  11. self.x = x;
  12. Self.y = y;
  13. }
  14. }



All right, now, prepare a picture of a man's walk, and let it move.

[JavaScript]View PlainCopy
    1. var list = new Array ();
    2. var index = 0;
    3. var mapimg;
    4. var loader
    5. var Imagearray;
    6. var animeindex = 0;
    7. var dirindex = 0;
    8. var Dirarr = New Array ({x:0,y:1},{x:-1,y:0},{x:1,y:0},{x:0,y:-1});
    9. function Main () {
    10. Loader = new Lloader ();
    11. Loader.addeventlistener (Levent.complete,loadbitmapdata);
    12. Loader.load ("1.png","BitmapData");
    13. }
    14. function Loadbitmapdata (event) {
    15. var bitmapdata = new Lbitmapdata (loader.content,0,0,70,92);
    16. Imagearray = Lglobal.dividecoordinate (bitmapdata.image.width,bitmapdata.image.height,8,8);
    17. mapimg = new Lbitmap (BitmapData);
    18. mapimg.x = 100;
    19. Mapimg.bitmapData.setCoordinate (0,0);
    20. index = 0;
    21. var backlayer = new Lsprite ();
    22. AddChild (Backlayer);
    23. Backlayer.addchild (MAPIMG);
    24. Backlayer.addeventlistener (Levent.enter_frame, Onframe)
    25. }
    26. function Onframe () {
    27. index++;
    28. if (index >= imagearray[0].length) {
    29. index = 0;
    30. }
    31. Mapimg.bitmapData.setCoordinate (IMAGEARRAY[DIRINDEX][INDEX].X,IMAGEARRAY[DIRINDEX][INDEX].Y);
    32. mapimg.x + = dirarr[dirindex].x*3;
    33. Mapimg.y + = dirarr[dirindex].y*3;
    34. if (animeindex++ >) {
    35. dirindex++;
    36. if (Dirindex > 3) dirindex = 0;
    37. Animeindex = 0;
    38. }
    39. }

Using the syntax of ActionScript to write the second html5--, using a sprite to achieve animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.