(no.00004) iOS implementation brick Game (10): Bricks! More bricks!

Source: Internet
Author: User

Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please tell me, if you feel good please support a lot of praise. Thank you! Hopy;)

So far the game is basically playable, but the number of bricks is constant, and when the player hits all the bricks it will feel no meaning, so we must find ways to add more bricks to come in.

Create an instance variable that loops to hold the color

We want to add bricks by line, for the sake of beauty, each row of bricks should be of different colors. So let's start by creating an instance variable that keeps the color of the current brick:

NSInteger _currentBrickColor;

There is, of course, a way to update its values:

-(void)updateCurrentBrickColor:(NSInteger)incVal{    _currentBrickColor += incVal;    _currentBrickColor %= brkColorMax;    if (_currentBrickColor == brkColorUnknown) {        _currentBrickColor++;    }}

It's very simple, and it's constantly circulating in all the brick colors.

Creates a row of bricks at the specified y-axis position

Next we write a help method that creates a row of bricks at the specified y-coordinate height:

//在rowY位置创建新的一行砖块-(void)createOneBrickRow:(NSInteger)rowY{    [self updateCurrentBrickColor:1];    [self createOneBrickRow:rowY withColor:_currentBrickColor];}

Where another method is called, we implement it immediately:

//create a new row of bricks in rowy position with the specified color  -(void ) Createonebrickrow: (nsinteger ) RowY    Withcolor: (brickcolor) color{Brick *brick;    //if Rowy is 0, create a  from the height of the screen bricks to the highest possible heights. if  (Rowy = = 0 )    {rowy = Max_brick_hight; } for  (int  i = 0 ; I < 4 ;        i++) {brick = [brick brickwithcolor:color];        Brick.position  = CCP (i * (_brickwidth-brick_shadow_offset_x), rowy);        [_physicsworld Addchild:brick];        //put the newly added bricks to the head of the _bricks array     [_bricks insertobject:brick atindex:0 ]; }}

In general, we create a row of bricks at the specified rowy location, but there may be a situation where all the bricks are now out and there are no bricks on the screen. Then we set up a row of bricks from the highest position on the screen.

Sink a row of bricks

If the current bricks remain motionless, the new bricks cannot be added. So we have to find a way to move the bricks down so that we can add a new line to the vacated position.

We first realize the method of sinking a brick, and then realize the method of sinking a row of bricks.

The first is to sink a piece of brick:

//sinking a specified brick-(void) Sinkonebrick: (brick*) brick{ccphysicsbody    *physbody = Brick.physicsbody      Physbody.type  = Ccphysicsbodytypedynamic;     Cgpoint brickposition = Brick.position  ;  Brick.position  = CCP (brickposition.x , Brickposition.y -(_brickheight-brick_shadow_offset_y))  [Self scheduleblock:^ (Cctimer *timer) {Physbody.type  = Ccphysicsbodytypekinematic;
       } delay:0 ]; }  

In the above code, we first changed the brick type to dynamic and finally to the mixed state. This is due to the improper behavior of preventing static physical objects from bouncing when they encounter dynamic objects while moving.

With the method above, sinking a line of bricks is a good implementation:

//sink all bricks in level to step, then create new step-line bricks above.-(void) Sinkbricksandcreatenewrows: (Nsinteger) step{//Save the Y value of the highest row bricks in the current level    NsintegerHighestrowbricky =0; for(inti =0; I < step; i++) { for(Brick *brick in _bricks) {if(Highestrowbricky = =0) {Highestrowbricky = Brick. Position. Y; }                [ SelfSinkonebrick:brick]; }//Create a new row of bricks in the original highest row[ SelfCreateonebrickrow:highestrowbricky]; Highestrowbricky =0; }}

The above code completes the sinking of a row of bricks and then creates a new line of bricks in the vacated position.

Regular sinking Bricks

OK, finally write a timer method, call the above method after the specified interval:

-(void)doWithBricks{    [self sinkBricksAndCreateNewRows:1];}

The timer method is then called in the initialization method:

[selfschedule:@selectorinterval:3];

Now that we've compiled and run the app, we've implemented the effects we want to add bricks:

(no.00004) iOS implementation brick Game (10): Bricks! More bricks!

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.