Write html5--seventh in the syntax of ActionScript and customize the button

Source: Internet
Author: User

This paper is the official HTML5 training course for Brother Lian it educational institution, mainly introduces: writing html5--seventh with the syntax of ActionScript, custom button

Seventh, custom button


This time, get a simple, custom button.
In fact, with the previously defined Lsprite,lbitmap class, the definition of the button is very convenient.
Here is the code to add the button,

[JavaScript]View PlainCopy
  1. function Gameinit (event) {
  2. Backlayer = new Lsprite ();
  3. AddChild (Backlayer);
  4. Btn01 = New Lbutton (New Lbitmap (Newlbitmapdata (imglist["replay_button_up")),new Lbitmap (   New Lbitmapdata (imglist["Replay_button_over")));
  5. btn01.x = 76;
  6. BTN01.Y = 50;
  7. Backlayer.addchild (BTN01);
  8. BTN02 = New Lbutton (New Lbitmap (Newlbitmapdata (imglist["quit_button_up")),new Lbitmap (new  Lbitmapdata (imglist["Quit_button_over")));
  9. btn02.x = 76;
  10. BTN02.Y = 100;
  11. Backlayer.addchild (BTN02);
  12. Btn01.addeventlistener (Lmouseevent.mouse_down, Onmousedown01);
  13. Btn02.addeventlistener (Lmouseevent.mouse_down, ONMOUSEDOWN02);
  14. }
  15. function Onmousedown01 (event) {
  16. Alert ("Btn01 on Click");
  17. }
  18. function Onmousedown02 (event) {
  19. Alert ("btn02 on Click");
  20. }



Principle: Establish a Lbutton class that inherits from Lsprite, set two pictures for the button, then listen to the mouse position, when the mouse moves to the button, the Change button state, is a simple button.


Here, I use MouseMove to listen to the mouse position, add a buttonlist array to the Lglobal class, when the button is created, the button is added to the buttonlist, and then when the mouse is moved, You can determine whether the mouse is on the button from the Buttonlist array, and then remove the button from the Buttonlist array when the button is deleted.


Some modifications:
1, modify the Lsprite class, add Die method, each lsprite when be removechild, call their own Die method, die method Put some is removed is necessary to deal with the event, such as the button, to remove from the buttonlist.
2, add Objectindex to each constructor to differentiate each object.
3, modify the Addchild method, add displayobject.parent = Self, is to assign the parent object to each object.


Ready to start creating the button class Lbutton.

[JavaScript]View PlainCopy
  1. function Lbutton (bitmap_up,bitmap_over) {
  2. Base (this,lsprite,[]);
  3. var = this ;
  4. Self.type = "Lbutton";
  5. Self.bitmap_up = bitmap_up;
  6. Self.addchild (BITMAP_UP);
  7. if (bitmap_over = = null) {
  8. Bitmap_over = bitmap_up;
  9. }else{
  10. Self.addchild (Bitmap_over);
  11. }
  12. Self.bitmap_over = Bitmap_over;
  13. Self.bitmap_over.visible = false;
  14. Self.bitmap_up.visible = true;
  15. LGlobal.buttonList.push (self);
  16. }
  17. LButton.prototype.buttonModeChange = function () {
  18. var = this ;
  19. var cood={x:0,y:0};
  20. var parent = self.parent;
  21. While (parent! = "root") {
  22. Cood.x + = Parent.x;
  23. Cood.y + = Parent.y;
  24. parent = parent.parent;
  25. }
  26. if (Self.ismouseon (Lglobal.mousemoveevent,cood)) {
  27. Self.bitmap_up.visible = false;
  28. Self.bitmap_over.visible = true;
  29. }else{
  30. Self.bitmap_over.visible = false;
  31. Self.bitmap_up.visible = true;
  32. }
  33. }
  34. LButton.prototype.die = function () {
  35. var = this ;
  36. Arguments.callee.  Super.die.call (this);
  37. For (var i=0;i<lglobal.buttonlist.length;i++) {
  38. if (Lglobal.buttonlist[i].objectindex = = Self.objectindex) {
  39. LGlobal.buttonList.splice (i,1);
  40. Break ;
  41. }
  42. }
  43. }




Look at the results, you can't see the effect. Download the browser that supports HTML5

Http://fsanguo.comoj.com/html5/jstoas06/index.html

Write html5--seventh in the syntax of ActionScript and customize the button

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.