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;)
The essence of the level game is concentrated in the game's level, which contains all the elements of the game, as to how the level management of these elements depends on the effect of the different games to decide. We started by building bricks in the level, small ball and bounce bar 3 elements.
Create a Level class
Create a level class in Xcode that inherits from the Ccnode class. Modify the header file as follows:
#import "CCNode.h"@interface Level : CCNode-(void)removeFromBricks:(id)brick;-(CGPoint)ballPosition;@end
Level to maintain a separate list to hold the brick instances in it, we need to use the Removefrombricks method to ensure that the brick is removed correctly when removing bricks outside the level. The latter method to see the meaning of the name, is to obtain the position of the ball, this will be used in the back.
Implementation of the level initialization method
Open the LEVEL.M file and add an initialization method:
-(void)didLoadFromCCB{ _bricks = [NSMutableArray array]; Brick *brick = [Brick brickWithColor:brkColorBlack]; _brickHeight = brick.contentSize.height; _brickWidth = brick.contentSize.width; _viewSize = [CCDirector sharedDirector].viewSize; [self initBricks]; [self initStick];}
We first created an array of _bricks that saved bricks, got some of the variables that would be used frequently later in the level, and then the way to initialize the bricks and bounce rods. You might wonder how less of a method to initialize a small ball? Here to illustrate, because in the method of initializing the ball to adjust the moment of the ball, So it was too early here, when the physical object of the ball was not ready, we put it in the OnEnter method:
-(void)onEnter{ [super onEnter]; [self initBall];}
Initialization of Bricks
Let's start by looking at the initialization of the bricks:
-(void ) initbricks{Brick *brick = [Brick Brickwithcolor:brkcolorblack]; for (int row = 0 ; row < 8 ; row++) {[self updatecurrentbrickcolor:1 ]; for (int col = 0 ; col < 4 ; col++) {brick = [brick brickwithcolor:_currentbrickcolor]; Brick.position = CCP (col * (_brickwidth-brick_shadow_offset_x), Max_brick_hight-row * (_brickheight-brick_shadow_offset_y)); [_physicsworld Addchild:brick]; [_bricks Addobject:brick]; } }}
The above code draws 8 lines from the top of the screen, 4 bricks per line, and adds each block to the physical world and _bricks array.
The initialization of the rebound stick
-(void)initStick{ _stick = [Stick stickNormal]; _stick.position = ccp(_viewSize.width/2, STICK_Y); [_physicsWorld addChild:_stick];}
The code is simple, build a bounce stick and add it to the physical world.
Initialization of small balls
Finally, the initialization of the ball is done:
Initialize the ball in level and give it a random impulse. (Do not want the ball to fall directly on the rebound stick)-(void) initball{_ball = (ccsprite*) [Ccbreader load:@"Elements/ball"];Nsassert (_ball, @"Ball must not nil");_ball. Name= @"Ball";_ball. Position= CCP (_viewsize. Width/2, ball_y);[_physicsworld Addchild:_ball];Ccphysicsbody *physball = _ball. Physicsbody;Cgpoint velocity = Physball. Velocity;Nsintegerneg= Arc4random_uniform (2);if (neg==0) {neg= -1;} Nsinteger rndx = Arc4random_uniform ( -)+ -;Velocity. x=neg* RNDX;[Physball Applyangularimpulse:Ten];[Physball applyimpulse:velocity];}
In the above method to build a small ball, we do not want its vertical drop down, so randomly give it a momentum, so that its slightly lively point of falling down;)
Now to compile and run the game, now the game has just entered the effect as shown below:
A basic level framework is basically set up, the next part of our game to achieve collision processing. See;)
(no.00004) iOS implementation brick Game (vii): implementation of Level Class