Design and Development of game scripts-Chapter 6 buttons, pause and tag of scripts

Source: Internet
Author: User
Button

The button is essential in any program. This time, let's take a look at how to implement various functions of the button. Several scripts to be implemented in this article are as follows.

/* Chapter 6 of game script design and development * // Add a button. add (layer01, button01, null, 50, 50, OK _button_up, OK _button_over, null); function function_test01 (); // Remove button. remove (button01); endfunction; // remove the display layer. remove (layer01); // Add an event button to the button. mousedown (button01, function_test01 );

The following is the complete code of the scriptbutton class, which is used to parse the above script.

/** Scriptbutton. JS **/var scriptbutton = function () {}; scriptbutton. analysis = function (value) {var start = value. indexof ("("); var end = value. indexof (")"); Switch (value. substr (0, start) {Case "button. add ": // Add button scriptbutton. addbutton (value, start, end); break; Case "button. remove ": // Delete the scriptbutton button. removebutton (value, start, end); break; Case "button. mousedown ": // click the event scriptbutton. mouseevent (value, start, E Nd, lmouseevent. mouse_down); break; Case "button. mouseup ": // trigger the scriptbutton event with the mouse. mouseevent (value, start, end, lmouseevent. mouse_up); break; Case "button. mousemove ": // scriptbutton. mouseevent (value, start, end, lmouseevent. mouse_move); break; default: lglobal. script. analysis () ;}};/** Add button script to parse the button. add (layer01, button01, null, 50, 50, OK _button_up, OK _button_over, null); */scriptbutton. addbutton = function (value, sta RT, end) {var script = lglobal. script; var layer; // obtain the parameter var Larr = value. substring (start + 1, end ). split (","); var layerstr = Larr [0]; // display layer name var namestr = Larr [1]; // button name var labelstr = Larr [2]; // text on the button. If it is set to null, the text var x = parseint (Larr [3]) is not displayed. // button coordinate var y = parseint (Larr [4]); // button coordinate var dataup = Larr [5]; // The bitmapdata Object Name var dataover = Larr [6]; // The bitmapdata Object Name of the style after the button is clicked // obtain the bitmapdata object var upimg of the style popped up and pressed = Script. scriptarray. bitmapdatalist [dataup]; var overimg = script. scriptarray. bitmapdatalist [dataover]; // The lspritevar uplayer = new lsprite (); uplayer. addchild (New lbitmap (upimg); // The lspritevar overlayer = new lsprite (); overlayer. addchild (New lbitmap (overimg); // If button text is set, add an ltextfield object on the button to display the text if (labelstr & labelstr! = "Null") {var uptext = new ltextfield (); uptext. TEXT = labelstr; uptext. size = upimg. height * 0.5; uptext. X = (upimg. width-uptext. getwidth () * 0.5; uptext. y = upimg. height * 0.2; uplayer. addchild (uptext); var overtext = new ltextfield (); overtext. TEXT = labelstr; overtext. size = upimg. width * 0.5; overtext. X = (upimg. width-uptext. getwidth () * 0.5 + 2; overtext. y = upimg. height * 0.2 + 2; overlayer. addchild (overtext); // The text color of the button if (Larr. length> 7) {uptext. color = Larr [7]; overtext. color = Larr [7] ;}/// create an lbutton object var BTN = new lbutton (uplayer, overlayer) using the two statuses of the button; BTN. X = x; BTN. y = y; // obtain the display layer Layer = script. scriptarray. layerlist [layerstr]; // Save the script button. scriptarray. btnlist [namestr] = BTN; BTN. name = namestr; // Add the button to the display layer. addchild (BTN); script. analysis () ;};/** delete button script parsing button. remove (button01); */scriptbutton. removebutton = function (value, start, end) {// obtain the parameter var Larr = value. substring (start + 1, end ). split (","); var namestr = Larr [0]; // button name var script = lglobal. script; // obtain the button var BTN = script. scriptarray. btnlist [namestr]; // If the button does not exist, parse the next line of script if (BTN = NULL) {script. scriptarray. btnlist [namestr] = NULL; script. analysis (); return;} // Remove button BTN. parent. removechild (BTN); script. scriptarray. btnlist [namestr] = NULL; script. analysis () ;};/*** button event script parses the button. mousedown (button01, function_test01); */scriptbutton. mouseevent = function (value, start, end, e) {var script = lglobal. script; // obtain the parameter var Larr = value. substring (start + 1, end ). split (","); var namestr = Larr [0]; // button name var funstr = Larr [1]; // function name // get button var BTN = script. scriptarray. btnlist [namestr]; // Add an anonymous function, and use the call script class in the anonymous function to call the corresponding function var fun = function (event) {scriptfunction. analysis ("call. "+ funstr +" (); ") ;}; // Add event BTN for the button. addeventlistener (E, fun); script. analysis ();};

I added very detailed code in the above Code, basically I don't need to explain anything more. Let's take a look at the use of these scripts and modify main. ls as follows.

Button); // Add button. add (layer01, button01, null, 50, 50, OK _button_up, OK _button_over); button. add (layer01, button02, test 1,150, 50, OK _button_up, OK _button_over, #880000); button. add (layer01, button03, test 2,250, 50, OK _button_up, OK _button_over, #008800); // declare a function to use function function_test01 () for button events; // remove the button. remove (button01); endfunction; function function_test02 (); button. remove (button02); endfunction; function function_test03 (); button. remove (button03); endfunction; // Add an event button to the button. mousedown (button01, function_test01); button. mousedown (button02, function_test02); button. mousedown (button03, function_test03 );

Test connection

Http://lufylegend.com/demo/test/lsharp/06/index01.html

In the above test, I used the script to add three buttons, and added click events for the three buttons respectively. After the button is clicked, the clicked button will be removed.

Script pause

During the running of the game script, if the script is not suspended and the parsing is continuously performed, it will be completely out of control. When will the script end, the entire program will end. This is obviously not in line with our wishes, and in the game, we will also encounter a situation where we click the mouse to continue the following program, which will be paused using the script, it means that the parsing is paused after the script is executed to a certain line. Wait for the user's command before parsing the script in the next line.

The script to be defined below is as follows:

// Pause wait for 1 second. time (1000); // wait for wait to be clicked. click (); // wait for the Running Control script to run wait. CTRL (); // end wait. ctrl, continue to parse wait. play ();

The following is a complete scriptwait. js class used to parse various operations related to pausing the script.

/** Scriptwait. JS **/var scriptwait = function () {}; scriptwait. analysis = function (value) {var start = value. indexof ("("); var end = value. indexof (")"); Switch (value. substr (0, start) {Case "Wait. click ": // pause. Wait for the mouse to click scriptwait. waitclick (); break; Case "Wait. CTRL ": // pause, wait for the run script if (INT (value. substring (start + 1, end)> 0) lglobal. script. linelist. unshift ("Wait. CTRL () "); break; Case" Wait. play ": // The Script continues to run lglobal. script. analysis (); break; Case "Wait. time ": // script pause for a period of time scriptwait. timeid = setTimeout (function () {scriptwait. timeid = NULL; lglobal. script. analysis (); }, 1000); break; Case "Wait. clickover ": // end wait click script (wait. click) lglobal. script. scriptlayer. removeeventlistener (lmouseevent. mouse_up, scriptwait. clickevent); lglobal. script. analysis (); break; Case "Wait. timeover ": // End Time pause script (wait. time) scriptwait. timeover (); break; Case "Wait. over ": // end all pause scripts lglobal. script. scriptlayer. removeeventlistener (lmouseevent. mouse_up, scriptwait. clickevent); scriptwait. timeover (); break; default: lglobal. script. analysis () ;}};/** End Time pause script (wait. time) **/scriptwait. timeover = function () {If (scriptwait. timeid) {cleartimeout (scriptwait. timeid); scriptwait. timeid = NULL;} lglobal. script. analysis () ;};/** pause, wait for the mouse **/scriptwait. waitclick = function () {var layer = lglobal. script. scriptlayer; // Add a mouse click event. When you click the screen, call the clickevent function to start running the script layer. addeventlistener (lmouseevent. mouse_up, scriptwait. clickevent) ;};/** click to run the script **/scriptwait. clickevent = function (event) {lglobal. script. scriptlayer. removeeventlistener (lmouseevent. mouse_up, scriptwait. clickevent); lglobal. script. analysis ();};

The above Code also adds a detailed comment. Below we will test these scripts and modify the script file as follows:

Button. add (layer01, button01, null, 50,200, OK _button_up, OK _button_over); function function_test01 (); wait. play (); endfunction; button. mousedown (button01, function_test01); text. label (layer01, txt01, script paused, you can click OK to continue parsing the script, 000000, 20, #); wait. CTRL (); button. remove (button01); text. label (layer01, txt01, script end, 0,120, 20, #000000 );

The test connection is as follows:

Http://lufylegend.com/demo/test/lsharp/06/index02.html

As you can see, all the above scripts are running normally in the program.

Tag

The tag function, similar to the Go statement in some programs, is to directly jump to a code position and start to execute. Below I still implement the following functions in the script, first define two scripts, as follows.

// Set the label mark. drawroundrect; // jump to the drawroundrect label position mark. Goto (drawroundrect );

When parsing a script, it will be automatically skipped when it encounters an unknown script. Therefore, when setting the tag, skip it without any processing. Let's take a look at scriptmark. how to find specific labels in JS classes.

/** Scriptmark. JS **/var scriptmark = function () {}; scriptmark. analysis = function (value) {var start = value. indexof ("("); var end = value. indexof (")"); Switch (value. substr (0, start) {Case "mark. goto ": // jump to the tag location scriptmark. goto (value, start, end); break; default: lglobal. script. analysis () ;}}; scriptmark. goto = function (value, start, end) {var mark = lmath. trim (value. substring (start + 1, end); // copylist is a copy of the script sequence being parsed, and then copy a copy of the script sequence var copyarray = lglobal. script. copylist. concat (); var foundstr; while (copyarray. length) {// search for a tag from the copied script sequence. If no row is found, delete the tag foundstr = copyarray. shift (); If (foundstr. indexof ("mark. "+ mark)> = 0) {// If the tag is found, replace the script sequence currently being parsed with the Copy Sequence lglobal. script. linelist = copyarray; lglobal. script. analysis (); Return ;}/// if no tag is found, do nothing and parse lglobal in the next script. script. analysis ();};

Finally, let's test it. Modify the main. ls script file as follows:

Layer. add (-, layer01, 0, 0); // jump to the label drawtrianglemark. goto (drawtriangle); // draw a rectangular layer. drawrect (layer01, 0xff0000); layer. drawrectline (layer01, 0,100,100, 60, 0xff0000, 5); // set the drawroundrect tag mark. drawroundrect; // draw a rectangular layer with rounded corners. drawroundrect (layer01, 880000, x); layer. drawroundrectline (layer01, 150,100,100, 60, 10, 0x88133, 5); // jump to the label overmark. goto (over); // set the drawtriangle tag mark. drawtriangle; // draw a triangle layer. drawtriangle (layer01, 60, 0xff0000); layer. drawtriangleline (layer01, 350,100,300,160,400,160, 0xff0000, 5); // jump to the label drawroundrectmark. goto (drawroundrect); // sets the over tag mark. over;

Explain the above script. Without these tag operations, the script will draw two rectangles, two rounded Rectangles and two triangles in sequence, but the script will jump to the label at the beginning, jump to drawtriangle, start to draw a triangle, draw a triangle, jump to the drawroundrect label, start to draw a rounded rectangle, after drawing a rounded rectangle, directly jump to the over label, thus the script ends, therefore, the first two rectangles are not drawn.

The test connection is as follows:

Http://lufylegend.com/demo/test/lsharp/06/index03.html

This is the Running Effect

The above is the content of this chapter. In the next chapter, expand the text script to display the text of the typewriter effect. Then, the first part of the script engine is about to be discussed, I will try to use these scripts to make a small game for you.


The source code of lufylegend. lsharp. js as of this chapter is as follows:

Http://lufylegend.com/demo/test/lsharp/06/lufylegend.lsharp.js

Game script design and development articles

Http://blog.csdn.net/lufy_legend/article/details/8888787

This chapter is here. Welcome to continue to follow my blog

Reprinted Please note:From lufy_legend's blog http://blog.csdn.net/lufy_legend

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.