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
- function Gameinit (event) {
- Backlayer = new Lsprite ();
- AddChild (Backlayer);
- Btn01 = New Lbutton (New Lbitmap (Newlbitmapdata (imglist["replay_button_up")),new Lbitmap ( New Lbitmapdata (imglist["Replay_button_over")));
- btn01.x = 76;
- BTN01.Y = 50;
- Backlayer.addchild (BTN01);
- BTN02 = New Lbutton (New Lbitmap (Newlbitmapdata (imglist["quit_button_up")),new Lbitmap (new Lbitmapdata (imglist["Quit_button_over")));
- btn02.x = 76;
- BTN02.Y = 100;
- Backlayer.addchild (BTN02);
- Btn01.addeventlistener (Lmouseevent.mouse_down, Onmousedown01);
- Btn02.addeventlistener (Lmouseevent.mouse_down, ONMOUSEDOWN02);
- }
- function Onmousedown01 (event) {
- Alert ("Btn01 on Click");
- }
- function Onmousedown02 (event) {
- Alert ("btn02 on Click");
- }
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
- function Lbutton (bitmap_up,bitmap_over) {
- Base (this,lsprite,[]);
- var = this ;
- Self.type = "Lbutton";
- Self.bitmap_up = bitmap_up;
- Self.addchild (BITMAP_UP);
- if (bitmap_over = = null) {
- Bitmap_over = bitmap_up;
- }else{
- Self.addchild (Bitmap_over);
- }
- Self.bitmap_over = Bitmap_over;
- Self.bitmap_over.visible = false;
- Self.bitmap_up.visible = true;
- LGlobal.buttonList.push (self);
- }
- LButton.prototype.buttonModeChange = function () {
- var = this ;
- var cood={x:0,y:0};
- var parent = self.parent;
- While (parent! = "root") {
- Cood.x + = Parent.x;
- Cood.y + = Parent.y;
- parent = parent.parent;
- }
- if (Self.ismouseon (Lglobal.mousemoveevent,cood)) {
- Self.bitmap_up.visible = false;
- Self.bitmap_over.visible = true;
- }else{
- Self.bitmap_over.visible = false;
- Self.bitmap_up.visible = true;
- }
- }
- LButton.prototype.die = function () {
- var = this ;
- Arguments.callee. Super.die.call (this);
- For (var i=0;i<lglobal.buttonlist.length;i++) {
- if (Lglobal.buttonlist[i].objectindex = = Self.objectindex) {
- LGlobal.buttonList.splice (i,1);
- Break ;
- }
- }
- }
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