Unity3d Getting Started Tetris Summary (ii)

Source: Internet
Author: User

Russian implementation using Unity Grammar Summary ...

1.Instantiate

Cloning an object and his child nodes in instantiate can also include location information.

In the A.createblock () function, a random selection of seven blocks of the Gameobject, for dynamic block instantiation:

        Instantiate (Blocks[random]);//This clone is Gameobject,

B.setblock () to detect if block can be stationary, if yes, block static drawing,

    Instantiate (Cube,new0), quaternion.identity);//This clone is every mycube, the actual drawing

C.block.cs The Block initialization process, instantiating each sub-node.

            var New 0), quaternion.identity);

Instantiation is typically used for instance projectiles (bullets, grenades, fragments, flying iron balls, etc.), ai enemies, and particles exploding. This is often used for: multiple repetitive object.

This time for two types of instantiation, one is block, and the other is drawn mycube.

2.yield Interrupt

Reference: http://blog.csdn.net/huang9012/article/details/29595747

1. Specific time delay:

void Start () {     startcoroutine (Destroy ());//start and Destroy at the same time? }  IEnumerator Destroy () {    yieldreturn waitforseconds (3.0f);// The following operation is performed after 3s delay     Destroy (gameobject);}

2.yield return null, break, continue execution of the following code at the next frame

The function of 3.yield return is to return control to the outside immediately after executing to this line of code. the code after yield return executes when the external code calls MoveNext again , until the next yield return--or iteration ends.

a scenario in which you need to use yield in a game:
    • When the game settles scores, the score rises from 0 instead of displaying the final score directly

    • When people talk, the text appears one at a time rather than suddenly
    • 10, 9, 8 ... Countdown to 0

    • Some games (such as the King of the Fist) lose blood when the UI of the blood bar gradually decreases, rather than suddenly lowering to the current amount of blood

Conditions used in this Tetris:

In Block.cs:

Within the Fall function:

 while (true) {  //... Does it reach a standstill state?   ...   // No, then continue to fall  yield return null; // the next frame continues to run the fall function }

To translate the delay operation of a block: At the end of the translation function, delay a bit ...

yield return New Waitforseconds (. 1f);

In Manger.cs:

IEnumerator Checkrows (intYstart,intSize) {//detects if the block contains a few rows, whether it satisfies a line that is all true        yield return NULL;//After a new static draw block in Setblock (), wait for a frame to detect if there is a full line if(Ystart <1) Ystart =1; intCount =1;  for(inty = ystart;y < Ystart + size;y++){            intx;  for(x = maxblocksize;x < fieldwidth-maxblocksize;x++){                if(!fields[x, y]) {                     Break; }            }            if(x = = fieldwidth-maxblocksize) {//                yield returnStartcoroutine (Setrows (y));//To eliminate the line operation, and give control to the function setrows (), to reset the entire scene of the static cube, and do not rush to score, but first the elimination of the above data to move down, and is a smooth interpolation down, until all the next move to the end, before scoring ...
Why do we need yield here? Because the setrows need a smooth descent, the end ... Score + =Ten*count; Y--; Count++; }} createblock (Blockrandom); }

Yield: One is an iteration, while the loop requires successive return values, and one is a smooth descent cube operation

IEnumerator Checkrows (intYstart,intSize) {//detects if the block contains a few rows, whether it satisfies a line that is all true        yield return NULL; if(Ystart <1) Ystart =1; intCount =1;  for(inty = ystart;y < Ystart + size;y++){            intx;  for(x = maxblocksize;x < fieldwidth-maxblocksize;x++){                if(!fields[x, y]) {                     Break; }            }//If there is one or more rows in a row that are all true            if(x = = fieldwidth-maxblocksize) {//Debug.Log ("1111"); yield returnStartcoroutine (Setrows (y));//operation, and give control to the function setrows (),Debug.Log ("2222"); Score+=Ten*count; Y--; Count++;    }} createblock (Blockrandom); } IEnumerator Setrows (intYstart) {         for(inty = ystart;y < Fieldheight-1; y++) {//for the elimination line, the cube data above all moves down one line             for(intx = Maxblocksize;x < fieldwidth-maxblocksize;x++) {fields[x, y]= fields[x, y +1]; }        }                 for(intx = Maxblocksize;x < fieldwidth-maxblocksize;x++) {fields[x, fieldheight-1] =false; }                varcubes = Gameobject.findgameobjectswithtag ("Cube");//Search All Cubes        intCubetomove =0;  for(inti =0; i < cubes. length;i++) {gameobject cube=Cubes[i]; if(Cube.transform.position.y > Ystart) {//If this is the cube above this line, Cubetomove records the number of cubes that need to be movedCubeyposition[cubetomove] = (int) (CUBE.TRANSFORM.POSITION.Y);//Save these locations where you need to move the cubecubetransforms[cubetomove++] = Cube.transform;//??? Do not understand, cubetransforms directly represents the location of the cube?             }            Else if(CUBE.TRANSFORM.POSITION.Y = =Ystart) {Destroy (cube);//The deletion of this line            }        }                floatt =0;  while(T <=1f) {T+ = Time.deltatime *5f;  for(inti =0; i < cubetomove;i++) {//smooth interpolation Move DownCubetransforms[i].position =NewVector3 (cubetransforms[i].position.x, Mathf.lerp (Cubeyposition[i], Cubeyposition[i]-1, T), cubetransforms[i].position.z); } Debug.Log ("33333"); yield return NULL; }                if(++cleartimes = =timetoaddspeed) {Blocknormalfallspeed+=Addspeed; Cleartimes=0; }            }

The printing results after the elimination of the line are:

11111

33333

33333

33333...//performed a 1s time after the elimination of the line down operation, with two yield ...

22222

3.FindGameObjectsWithTag

4.IEnumerator

5.StartCoroutine

6.Time

Unity3d Getting Started Tetris Summary (ii)

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.