Design and Development of game scripts-Chapter 8 army arrival on the battlefield

Source: Internet
Author: User
Tags addchild

The last time I spoke about how to quickly display a map of the battlefield, how can I add troops to the battlefield when there is no army on the battlefield. In general game games, there are three types of troops on the battlefield: our army, enemy army and friends army. Our army can be manipulated, and the enemy can be attacked. The friends army cannot be manipulated or attacked. The enemy and your friends will attack each other. Of course, in some more complex games, the attack relationships are determined by the hostility or alliance between them.

Production starts, troops come

The war games in this script are for reference by Cao Zhuan. The army is divided into our army, enemy army, and friends army. In the L # script, the script for adding troops is as follows.

// Add our army (number of our army personnel, orientation, coordinate X, coordinate Y, display or hide) sousouscharacter. addour (0, 0, 6, 6, 1); // Add the enemy (character number, appearance level, orientation, coordinate X, coordinate Y, show or hide, Action AI) sousouscharacter. addenemy (, 0); // Add (character serial number, appearance level, orientation, coordinate X, coordinate Y, show or hide, Action AI) sousouscharacter. addfriend (, 0 );

The above scripts are respectively used to add, and set parameters.

The scripts added by the army are also between the following scripts, and before the addmap script is required.

initialization.start;initialization.end;

That is to say, the script added by the military is in the correct format as follows.

initialization.start;SouSouSCharacter.addOur(0,0,6,6,1);SouSouSCharacter.addEnemy(1,1,0,8,4,1,0);SouSouSCharacter.addFriend(2,1,0,6,4,1,0);addMap(s01.smap);initialization.end;

The parsing process of these scripts is as follows:

switch(lineValue.substr(0,start)){case "addMap":LSouSouObject.sMap.addMap(lineValue.substring(start+1,end).split(","));break;case "SouSouSCharacter.addOur":LSouSouObject.sMap.addOurCharacter(lineValue.substring(start+1,end).split(","));break;case "SouSouSCharacter.addEnemy":LSouSouObject.sMap.addEnemyCharacter(lineValue.substring(start+1,end).split(","));break;case "SouSouSCharacter.addFriend":LSouSouObject.sMap.addFriendCharacter(lineValue.substring(start+1,end).split(","));break;default:LSouSouSMapScript.initialization();}

To add troops, you need to prepare character settings in advance and create a chara. JSON file as follows:

{"Peo1": {"Index": "1", "name": "Liu Bei", "face": "0", "R": "0 ", "S": "0", "arms": "1", "LV": "1", "exp": "1", "troops": "104 ", "strategy": "30", "maxtroops": "104", "maxstrategy": "30", "force": "78", "intelligence": "76 ", "Command": "72", "agile": "74", "luck": "100", "skill": "1", "Introduction ": "Liu Bei is the descendant of Liu Sheng, Sun Yat-sen king of Han, and the founding emperor of Shu Han in the Three Kingdoms period. "}," Peo2 ": {" Index ":" 2 "," name ":" Guan Yu "," face ":" 1 "," R ":" 1 ", "S": "1", "arms": "4", "LV": "1", "exp": "1", "troops": "107 ", "strategy": "13", "maxtroops": "107", "maxstrategy": "13", "force": "96", "intelligence": "90 ", "Command": "98", "agile": "68", "luck": "62", "skill": "2", "Introduction ": "Guan Yu was born, and then changed to yun long. The famous generals of the late Eastern Han dynasty began to follow Liu Bei since Liu Bei joined the crowd in his hometown. He was the brother of Liu Bei and one of Liu Bei's most trusted generals. "}," Peo3 ": {" Index ":" 3 "," name ":" Zhang Fei "," face ":" 2 "," R ":" 2 ", "S": "2", "arms": "4", "LV": "1", "exp": "1", "troops": "105 ", "strategy": "9", "maxtroops": "105", "maxstrategy": "9", "force": "99", "intelligence": "46 ", "Command": "74", "agile": "72", "luck": "76", "skill": "3", "Introduction": "Zhang Fei, ziyide, an important general of Shu Han in the Three Kingdoms period. He started to follow Liu Bei since Liu Bei joined the crowd in his hometown. He is the brother of Liu Bei and one of Liu Bei's most trusted generals. "}}

The chara. JSON file sets the data of all characters. This time, three characters are temporarily added. To facilitate the management of these characters, first add an lsousoumember class. The Code is as follows:

function LSouSouMember(data,flag){var self = this;if(data){self.data = data;}}LSouSouMember.prototype.getIndex = function(){return this.data.Index;};LSouSouMember.prototype.getS = function(){return this.data.S;};

Of course, the above is not a complete lsousoumember class, and will continue to expand slowly in the future. In the s battlefield script uploaded by Cao, the addition of our army is somewhat different from that of the enemy and friends army. In the settings for the appearance of the enemy and friends army, the first parameter is that the character is in chara. the serial number in JSON, and the arrival of our army is determined by the personnel of the selected army. Therefore, the first parameter is the serial number of our army personnel. The selection of people playing in our army is also controlled by the script, but the game development has not yet reached that step, so we should first prepare the players in advance and then script them later. In the lsousousmap constructor, add our troops and add a _ charalayer layer to display the troops on the battlefield. The following is the complete lsousousmap constructor.

Function lsousousmap () {var self = This; base (self, lsprite, []); lsousousouobject. SMAP = self; self. nodelength = 48; self. _ objectdata = NULL; self. _ loadbar = NULL; self. _ mapdata = NULL; self. mapw = 0; self. maph = 0; self. ourlist = []; self. enemylist = []; self. friendlist = []; self. _ maplayer = new lsprite (); self. _ charalayer = new lsprite (); self. _ viewmaplayer = new lsprite (); // temporarily added. lsousousouobject will be scripted later. memberlist. push (New lsousoumember (lglobal. chara ["peo1"]); lsousousouobject. perwarlist. push (lglobal. chara ["peo1"]. index); self. addchild (self. _ maplayer); self. addchild (self. _ charalayer); self. addchild (self. _ viewmaplayer); lglobal. script. scriptlayer. addchild (Self); lsousousmapscript. analysis ();}

To add troops, add the following three functions to the lsousousmap class to add our troops, enemy troops, and friends.

Lsousousmap. prototype. addourcharacter = function (PARAM) {var self = This; // judge whether the contestant is legal, lsousousouobject. perwarlist is the contestant selected by our army. If (parseint (Param [0])> = lsousousouobject. perwarlist. length) {lsousousmapscript. initialization (); return;} var I, MBR, _ characters; // determine the data of the contestant, lsousousouobject. memberlist is for (I = 0; I <lsousouobject. memberlist. length; I ++) {MBR = lsousousouobject. memberlist [I]; If (MBR. getindex () = lsousousouobject. perwarlist [parseint (Param [0])] Break;} // Add a contestant to the _ charalayer layer _ characters = new lsousousoucharacters (MBr, 0, parseint (Param [1]), 0); self. _ charalayer. addchild (_ characters); // determine the location of the contestant _ characters. X = parseint (Param [2]) * lsousousouobject. SMAP. nodelength; _ characters. y = parseint (Param [3]) * lsousousouobject. SMAP. nodelength; // movement target of the contestant. _ characters is used for character movement. tagercoordinate = new lpoint (_ characters. x, _ characters. y); // whether to hide the set if (parseint (Param [4]) = 0) _ characters. visible = false; // Add the contestant to our army personnel ourlist array lsousousouobject. SMAP. ourlist. push (_ characters); lsousousmapscript. initialization () ;}; lsousousmap. prototype. addenemycharacter = function (PARAM) {var self = This; var I, MBR, _ characters; // determine the data of the contestant var memberdata = lglobal. chara ["Peo" + Param [0]; MBR = new lsousoumember (memberdata); // Add a contestant to the battlefield _ charalayer layer _ characters = new lsousoucharacters (MBr, 1, parseint (Param [2]), parseint (Param [6]); self. _ charalayer. addchild (_ characters); // determine the location of the contestant _ characters. X = parseint (Param [3]) * lsousousouobject. SMAP. nodelength; _ characters. y = parseint (Param [4]) * lsousousouobject. SMAP. nodelength; // movement target of the contestant. _ characters is used for character movement. tagercoordinate = new lpoint (_ characters. x, _ characters. y); // whether to hide the set if (parseint (Param [5]) = 0) _ characters. visible = false; // Add a contestant to the array lsousousouobject of the enemy's enemylist. SMAP. enemylist. push (_ characters); lsousousmapscript. initialization () ;}; lsousousmap. prototype. addfriendcharacter = function (PARAM) {var self = This; var I, MBR, _ characters; // determine the contestant data var memberdata = lglobal. chara ["Peo" + Param [0]; MBR = new lsousoumember (memberdata); // Add a contestant to the battlefield _ charalayer layer _ characters = new lsousoucharacters (MBr, 1, parseint (Param [2]), parseint (Param [6]); self. _ charalayer. addchild (_ characters); // determine the location of the contestant _ characters. X = parseint (Param [3]) * lsousousouobject. SMAP. nodelength; _ characters. y = parseint (Param [4]) * lsousousouobject. SMAP. nodelength; // movement target of the contestant. _ characters is used for character movement. tagercoordinate = new lpoint (_ characters. x, _ characters. y); // whether to hide the set if (parseint (Param [5]) = 0) _ characters. visible = false; // Add the contestant to the friendly list array lsousousouobject. SMAP. friendlist. push (_ characters); lsousousmapscript. initialization ();};

The lsousoucharacters class is a classification class on the battlefield, including all the action control of the characters on the battlefield, as well as AI attributes, in lufylegend. in JS engines, animation for displaying characters is generally implemented using the lanimation class. However, when an animation is displayed using the lanimation class, the image must be an image, that is, the action that a character needs to play must be drawn to an image for cutting and playing. However, in Cao's biography, there are three character pictures on the battlefield:

Because the animations to be displayed are allocated to three images respectively, it is a little troublesome to use the lanimation class to implement them. Therefore, if an animation is displayed here, it is directly implemented using lbitmap, you only need to control the lbitmapdata object in lbitmap to display the animation. See the following code.

Function lsousoucharacters (Member, belong, direct, command) {var self = This; base (self, lsousoucharacter, []); self. _ member = member; self. _ belong = belong; self. _ direction = direct; self. _ command = command; self. animelist = []; self. action = 0; self. actionindex = 0; self. image ={}; self. image ["ATK"] = new lbitmapdata (lglobal. imglist ["s-View-ATK"]); self. image ["mov"] = new lbitmapdata (lglobal. imglist ["s-View-mov"]); self. image ["SPC"] = new lbitmapdata (lglobal. imglist ["s-View-SPC"]); self. image ["mov"]. setproperties (0, 0, 48); self. bitmap = new lbitmap (self. image ["mov"]); self. addchild (self. bitmap); self. setimage ();} lsousousoucharacters. prototype. setimage = function () {var self = This; var atklist = lglobal. dividecoordinate (self. image ["ATK"]. image. width, self. image ["ATK"]. image. height, 12, 1); var movlist = lglobal. dividecoordinate (self. image ["mov"]. image. width, self. image ["mov"]. image. height, 11,1); var spclist = lglobal. dividecoordinate (self. image ["SPC"]. image. width, self. image ["SPC"]. image. height, 5, 1); var list;/********** standing ********** // list = ["mov ", 48, 48, [[movlist [6] [0], 1]; self. animelist. push (list); // left list = ["mov", 48, 48, [[movlist [8] [0], 1]; self. animelist. push (list); // list = ["mov", 48, 48, [[movlist [7] [0], 1]; self. animelist. push (list); // right list = ["mov", 48, 48, [[movlist [8] [0],-1]; self. animelist. push (list);/******************* // list = ["mov", 48, 48, [[movlist [0] [0], 1], [movlist [1] [0], 1]; self. animelist. push (list); // left list = ["mov", 48, 48, [[movlist [4] [0], 1], [movlist [5] [0], 1]; self. animelist. push (list); // list = ["mov", 48, 48, [[movlist [2] [0], 1], [movlist [3] [0], 1]; self. animelist. push (list); // right list = ["mov", 48, 48, [[movlist [4] [0],-1], [movlist [5] [0],-1]; self. animelist. push (list); self. action = self. _ direction + 4 ;};

In a game of war games, the army on the battlefield can be said to be varied. The pictures used by each type of war are different, and special figures also have their own special images, the images used are different. In this way, more images are needed, and dozens or even hundreds of battlefield images may be used in a game. To quickly enter the battlefield, you need to read all images in advance. In a local game, reading these images does not take much time, but you need to pay attention to it on web games. Therefore, when I add a battlefield character lsousousoucharacters, I set the character image as a prepared image in advance. The following figure shows the effect after the operation.

The setimage function of the lsousousoucharacters class is used to break down the actions of three Character Images, including standing, moving, attack, and defense ]...... every action is pushed into the animelist array. This is a temporary breakdown of two actions: Standing and moving. As mentioned above, this animation directly uses the lbitmap object to play the animation by controlling the lbitmapdata object in the lbitmap object. In the setimage function, the elements in the animelist array are [lbitmapdata Object Name, image width, Image Height, animation coordinate group] in sequence. the animation coordinate group is a sequence composed of the image region coordinates and whether the image is flipped. For details, refer to [Region coordinates, whether the image is flipped], [Region coordinates, whether to flip the image]. The region coordinates here are not very well explained. For example, the following figure shows an example.

The red rectangle in the image is the display area. Because the size of each small motion image is 48x48, the coordinates of the area are ). In this way, if you want to play an animation, the animation will be implemented by replacing the lbitmapdata object of the current character with the corresponding action in animelist and the elements in the corresponding regional coordinate array, for specific implementation, add Levent first. the enter_frame event is as follows.

self.speed = 2;self.speedIndex = 0;self.addEventListener(LEvent.ENTER_FRAME,self.onframe);

The onframe Function Code is as follows:

LSouSouCharacterS.prototype.onframe = function(self){if(self.speedIndex++ < self.speed)return;self.speedIndex = 0;var anime = self.animeList[self.action]var list = anime[3];if(self.actionIndex >= list.length){self.actionIndex = 0;}var obj = list[self.actionIndex++];var coordinate = obj[0];self.bitmap.bitmapData = self.image[anime[0]];self.bitmap.bitmapData.setProperties(coordinate.x,coordinate.y,anime[1],anime[2]);self.bitmap.scaleX = obj[1];};

The above code implements animation playback, but the images used are still pre-set black action pictures. All characters are the same. If you want to diversify the battlefield characters, you only need to follow chara. in JSON data, read the appropriate battlefield image. After reading the image, replace the original image in the lsousousoucharacters class. The specific implementation is as follows.

LSouSouCharacterS.prototype.loadImage = function(){var self = this;atkLoader = new LLoader();atkLoader.parent = self;atkLoader.addEventListener(LEvent.COMPLETE,self.loadAtkOver);atkLoader.load("images/characters/s/" + self._member.getS() +"/atk.png" + (LGlobal.traceDebug?("?"+(new Date()).getTime()):""),"bitmapData");movLoader = new LLoader();movLoader.parent = self;movLoader.addEventListener(LEvent.COMPLETE,self.loadMovOver);movLoader.load("images/characters/s/" + self._member.getS() +"/mov.png" + (LGlobal.traceDebug?("?"+(new Date()).getTime()):""),"bitmapData");spcLoader = new LLoader();spcLoader.parent = self;spcLoader.addEventListener(LEvent.COMPLETE,self.loadSpcOver);spcLoader.load("images/characters/s/" + self._member.getS() +"/spc.png" + (LGlobal.traceDebug?("?"+(new Date()).getTime()):""),"bitmapData");};LSouSouCharacterS.prototype.loadAtkOver = function(event){var self = event.target.parent;self.image["atk"] = new LBitmapData(event.currentTarget);};LSouSouCharacterS.prototype.loadMovOver = function(event){var self = event.target.parent;self.image["mov"] = new LBitmapData(event.currentTarget);};LSouSouCharacterS.prototype.loadSpcOver = function(event){var self = event.target.parent;self.image["spc"] = new LBitmapData(event.currentTarget);};

After reading the character image, replace the image as follows:

The test connection is as follows:

Http://lufylegend.com/demo/test/lsharp/09/game/index.html

As mentioned above, this chapter will talk about so much. The next chapter will talk about the path-finding algorithms on the battlefield, allowing the army to move on the battlefield.

The source code as of this chapter is as follows, excluding the lufylegend. js engine source code. Please download it from the official website.

Http://lufylegend.com/demo/test/lsharp/09/09.rar

※Source code running instructions: server support is required. For details, see the preface and chapter 1 of this series.

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.