[Self. gameTimer addToRunLoop: [nsunloop mainRunLoop] forMode: NSDefaultRunLoopMode];
-(Void) steps {_ steps ++; if (_ steps % 60 = 0) {[self updateGameClock];} // background processing [self. model moveBackground]; [self. bgView setBackgroundFrame1: self. model. bgFrame1 andFrame2: self. model. bgFrame2]; // refresh self. heroView. center = self. model. hero. position; // if (_ steps % 10 = 0) {[self. model. hero fire];} // handle [self checkBullets]; // initialize the enemy machine [self initialEnemies]; // move the enemy machine [self updateEnemies]; // Collision Detection [self collisionDetector];}
_ Step is a static long integer that records the number of refresh times.
- (void)moveBackground{ self.bgFrame1 = CGRectOffset(self.bgFrame1, 0, RMoveOffset); self.bgFrame2 = CGRectOffset(self.bgFrame2, 0, RMoveOffset); CGRect topFrame = CGRectOffset(self.gameArea, 0, -self.gameArea.size.height); if (self.bgFrame1.origin.y >= self.gameArea.size.height) { self.bgFrame1 = topFrame; } else if (self.bgFrame2.origin.y >= self.gameArea.size.height) { self.bgFrame2 = topFrame; }}
Each time you refresh the background view, move one point down.
-(Void) collisionDetector {/******* bullet collision with enemy aircraft ******/NSMutableSet * removeBulletSet = [NSMutableSet set]; for (BulletView * bulletView in self. bulletViewSet) {Bullet * bullet = bulletView. bullet; for (EnemyView * enemyView in self. enemyViewSet) {Enemy * enemy = enemyView. enemy; if (CGRectIntersectsRect (bulletView. frame, enemyView. frame )&&! Enemy. toBlowup) {enemy. hp-= bullet. damage; if (enemy. hp <= 0) {enemy. toBlowup = YES;} else {if (enemy. type = REnemyTypeBig) {[enemyView stopAnimating];} enemyView. image = enemyView. hitImage;} [removeBulletSet addObject: bulletView] ;}}for (BulletView * bulletView in removeBulletSet) {[bulletView removeFromSuperview]; [self. bulletViewSet removeObject: bulletView];}/******** explosive processing ******/if (_ steps % 4 = 0) {NSMutableSet * toRemovedSet = [NSMutableSet]; for (EnemyView * enemyView in self. enemyViewSet) {Enemy * enemy = enemyView. enemy; if (enemy. toBlowup) {enemy. speed = 0; enemyView. image = enemyView. blowupImages [enemy. blowupFrames ++];} if (enemy. blowupFrames = enemyView. blowupImages. count) {[toRemovedSet addObject: enemyView] ;}}for (EnemyView * enemyView in toRemovedSet) {self. totalScore + = enemyView. enemy. prize; [self updateScoreLabel]; [self. enemyViewSet removeObject: enemyView]; [enemyView removeFromSuperview];} [toRemovedSet removeAllObjects];} /****** enemy plane collision hero plane ******/for (EnemyView * enemyView in self. enemyViewSet) {if (CGRectIntersectsRect (enemyView. frame, self. model. hero. collisionRect) {[self. heroView stopAnimating]; NSArray * images = [ImageResources nvidimages]. heroBlowupImages; self. heroView. image = images [3]; self. heroView. animationImages = images; self. heroView. animationDuration = 1.0f; self. heroView. animationRepeatCount = 1; [self. heroView startAnimating]; [self pauseGameTimer]; MyLog (@ "Hero death"); [[SoundsTool shareSoundsTool] playSoundWithType: RSoundTypeHeroOver]; [self endGame]; }}}
The above is the collision detection part. The two cases are processed respectively, and the animation is displayed frame by frame.
- (void)setToBlowup:(BOOL)toBlowup{ if (toBlowup == NO) { _toBlowup = NO; } else { _toBlowup = YES; RSoundType soundType; switch (self.type) { case REnemyTypeSmall: soundType = RSoundTypeSmallBlowup; break; case REnemyTypeMiddle: soundType = RSoundTypeMiddleBlowup; break; case REnemyTypeBig: soundType = RSoundTypeBigBlowup; break; } [[SoundsTool shareSoundsTool] playSoundWithType:soundType]; }}
- (void)playSoundWithType:(RSoundType)soundType{ NSString *soundName = nil; switch (soundType) { case RSoundTypeBullet: soundName = @"bullet"; break; case RSoundTypeSmallBlowup: soundName = @"enemy1_down"; break; case RSoundTypeMiddleBlowup: soundName = @"enemy3_down"; break; case RSoundTypeBigFly: soundName = @"enemy2_out"; break; case RSoundTypeBigBlowup: soundName = @"enemy2_down"; break; case RSoundTypeHeroOver: soundName = @"game_over"; break; } [self playSoundWithSoundName:soundName];}- (void)playSoundWithSoundName:(NSString *)soundName{ SystemSoundID soundId = [self.soundDict[soundName] unsignedLongValue]; AudioServicesPlaySystemSound(soundId);}
Initialize an enemy's machine
- (void)initialEnemies{ NSInteger level = self.model.gameLevel; NSInteger flag = RMaxLevel/level; NSInteger timeS = arc4random_uniform(10*flag)+10/level+flag*1; NSInteger timeM = arc4random_uniform(50*flag)+200/level+flag*3; NSInteger timeB = arc4random_uniform(100*flag)+1000/level+flag*10; Enemy *enemy = nil; if (_steps % timeS == 0) { enemy = [self.model createEnemyWithType:REnemyTypeSmall andSize:[ImageResources sharedImages].smallFlyImage.size]; [self initEnemyViewWithEnemy:enemy]; } else if (_steps % timeM == 0) { enemy = [self.model createEnemyWithType:REnemyTypeMiddle andSize:[ImageResources sharedImages].middleFlyImage.size]; [self initEnemyViewWithEnemy:enemy]; } else if (_steps % timeB == 0) { enemy = [self.model createEnemyWithType:REnemyTypeBig andSize:[[ImageResources sharedImages].bigFlyImages[0] size]]; [self initEnemyViewWithEnemy:enemy]; [[SoundsTool shareSoundsTool] playSoundWithType:RSoundTypeBigFly]; }}
The initialization time of the enemy's machine is easy to understand. Most of the methods in the game are encapsulated and not completely pasted. You can download it on your own. It takes a short time from the architecture to the preparation, and the functions of the item are not implemented. You may wish to have suggestions for improvement or examples for communication.