HTML5 Egret Game Development Idiom Big Challenge (vii) game logic and data processing

Source: Internet
Author: User
Tags addchild

This article on the basis of the above, will be the logic of coding development so that the game can play formally, there is no attention to the details of the experience, but directly implement the rules of the game logic, will be divided into two parts description: Data processing and game logic.

Initializing game data

In the previous fifth chapter, we have read all the level data through the construction of the data in the level selection interface, Leveldatamanager is responsible for the management of all the level data, in the Scenelevels class, when the Onclick_level trigger, Will switch to the game interface, so the transformation code is as follows:

PrivateOnclick_level (E:egret. TouchEvent) {varicon = <LevelIcon>E.currenttarget; if( This. sel_level! =icon. Level) { This. img_arrow.x =icon.x;  This. Img_arrow.y =ICON.Y;  This. Sel_level =icon.    level; }Else{        //Enter and start the game         This. Parent.addchild (scenegame.shared ()); Scenegame.shared (). Initlevel (icon.        level);  This. Parent.removechild ( This); }}

Do you remember the single case? Unknown can go to see the previous fifth, at this time Initlevel method has not been implemented, implement the Scenegame class, and add Initlevel method:

classScenegameextendseui.component {//Single Case    Private StaticShared:scenegame;  Public StaticShared () {if(scenegame.shared = =NULL) {scenegame.shared=NewScenegame (); }        returnscenegame.shared; }     PublicConstructor () {Super();  This. Skinname ="Src/game/scenegameskin.exml";  This. Btn_back.addeventlistener (Egret. Touchevent.touch_tap, This. Onclick_back, This); }    //Object Variables    PrivateGroup_answer:eui.    Group; PrivateGroup_words:eui.    Group; PrivateImg_question:eui.    Image; PrivateBtn_back:eui.    Group; PrivateLevelindex: Number; //Initializing Levels     PublicInitlevel (Level: Number){         This. Levelindex =Level ; varLeveldata =leveldatamanager.shared ().    Getlevel (level); }    PrivateOnclick_back () { This. Parent.addchild (scenelevels.shared ());  This. Parent.removechild ( This); }}

Parameter passed in is the level value, because we already have the data management class, does not need to go from the outside, now has the corresponding level data, can build the game, for the later design operation, we will now the ongoing level to save in a custom variable is This.levelindex, in the future, it will help us to complete the change of levels and other operations.

But at this point we found a problem, is the level of the choice "word" is 20, and the level of data is 10 (4+6), so that not enough we put what to do? There is a very simple way, random another topic will be the problem of the answer and the question combination, and then disrupt the character sequence, you can, of course, if in order to reduce the difficulty, you can also design the problem as 10 "problem word", where the use of 20 "problem word" to ensure the consistency of the difficulty, The following transformation Initlevel method:

//Initializing Levels PublicInitlevel (Level: Number){     This. Levelindex =Level ; varLeveldata =leveldatamanager.shared ().    Getlevel (level); //to connect fields together    varWords = Leveldata.answer +Leveldata.word; //random a field of another topic mixed into this topic     while(words.length==10){        vari = Math.floor (Math.random () * 400); if(i!=Level ) {            vartemp =leveldatamanager.shared ().            Getlevel (i); Words+ = Temp.word +Temp.answer; }    }    //Rearrange The fields    varwordlist:string[]=[];  for(varI =0; i<words.length;i++) {Wordlist.push (Words.charat (i)); } wordlist= This. Randomlist (wordlist); //Assign Value     for(vari = 0;i< This. group_words.numchildren;i++){        varWordrect = <Word> This. Group_words.getchildat (i);        Wordrect.setwordtext (Wordlist[i]); Wordrect.visible=true; }    //Reset Some states     for(vari = 0;i< This. group_answer.numchildren;i++){        varAnswerrect = <AnswerWord> This. Group_answer.getchildat (i); Answerrect. Setselectword (NULL); Answerrect.visible=true; Answerrect. Selectword=NULL; }    //Display Image     This. Img_question.source ="resource/assets/"+leveldata.img;}//random a sequence of numbersPrivaterandomlist (arr:any[]): any[] {varArray = [];  while(Arr.length > 0) {        vari = Math.floor (Math.random () *arr.length);        Array.push (Arr[i]); Arr.splice (i,1); }    returnArray;}

At the end of the addition of a self-written randomlist method, is to break an array sequence, the above comments have basically finished the code, it should be noted that the Answerword Setselectword method, in the previous code, no null is processed, So we have to modify Answerword.setselectword (Word:word):

//when a question word is selected to be added to the answer, the settings are not visible and are saved to this object later in use PublicSetselectword (word:word) {if(Word! =NULL){         This. Setwordtext (Word.getwordtext ());  This. Selectword =Word; Word.visible=false; }    Else{         This. Setwordtext ("");  This. Selectword =NULL; }}

The function of this method is that when the following problem word operation, it is saved in the answer word, in the future when the operation will be restored display.

Word Block manipulation processing logic

Open Word class, the game of the block is independent, almost does not change, so we will click the event into the object itself processing, by accessing the game class's singleton to make the code look better read some, so, directly modify the Onclick_tap method as follows:

protected Onclick_tap () {        scenegame.shared (). Onclick_word (this);    }

Then in Scenegame implement the Onclick_word method, the incoming parameter is word:

//when a word is clicked, it is thrown by the word class PublicOnclick_word (word:word) {//find a suitable location to add the answer content    varSel:answerword =NULL;  for(vari = 0;i< This. group_answer.numchildren;i++){        varAnswer = <AnswerWord> This. Group_answer.getchildat (i); if(Answer. Selectword = =NULL) {sel=answer;  Break; }    }    //when there is a suitable position, it fills the word and determines whether the victory    if(Sel! =NULL) {sel.        Setselectword (word); //Judging whether Victory        varCheck_str:string ="";  for(vari = 0;i < This. group_answer.numchildren;i++) {            varAnswer = <AnswerWord> This. Group_answer.getchildat (i); Check_str+=Answer.getwordtext (); }        if(Check_str = = leveldatamanager.shared (). Getlevel ( This. Levelindex). Answer) {//VictoryConsole.log ("Win"); }    }}

If you do not find a suitable add location, there is no action, this logic has been basically completed, you can run to see the effect, add the Start interface to main:

void {    //This.addchild (scenebegin.shared ());     This . AddChild (scenelevels.shared ());}

Then go all the way to the game interface and you'll get the results you want.

This article has ended, using click events and calls to each other to achieve the basic rule processing, due to the length of the problem, there are many things are not implemented, so after this add a two-level page explanation.

This project source:Chengyutiaozhan4.zip(due to the file size limit of the blog park, resource resources, please go to the back of the second chapter to download )

HTML5 Egret Game Development Idiom Big Challenge (vii) game logic and data processing

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.