你的位置:首页 > 操作系统

[操作系统]IOS 2D游戏开发框架 SpriteKit

  这次包括之后讲的spritekit 我都会围绕一个案例来说,这个案例就是一个简单的2d飞机大战游戏,今天这里我讲创建敌对精灵,就是敌对飞机,敌对飞机不停的被刷新到屏幕上.....当然这里涉及到的类其实还是,精灵,和材质两个类,这两个类前两篇的案例中已经出现过,使用方法都一样,主要看逻辑,我这里主要是实现每间隔一段时间屏幕就刷新一个敌对飞机。飞机从屏幕高度为起点往下移动,当potion移动到0时将敌对飞机移出。

 1 /*这个方法是spritekit 的场景自带的,每过一秒就会被调用*/ 2 -(void)update:(CFTimeInterval)currentTime { 3   [self BackMove:1]; 4    5   [self initEnemySprite];//本次增加的创建敌对飞机的方法 6    7    8 } 9 /*创建敌对飞机*/ 10 -(void)initEnemySprite 11 { 12   /*此方法是放在update里面的所以是每秒执行一次,下面三个变量就是控制飞机刷新速度的,如果不控制,屏幕就会每秒都刷新一个飞机出来,那么不一会屏幕就会爆满, 这里每35秒刷新一架小型地址每400秒刷新一架中型飞机, 每700秒刷行一架大飞机*/ 13   _smallPlaneTime++; 14   _mediumPlaneTime++; 15   _bigPlaneTime++; 16    17    18   //int RadomNumber=  (arc4random() % 100) + 0; 19   int SpriteX=DEVICE_Width; 20   /*随机精灵在x轴的位置*/ 21    int x = (arc4random() % (SpriteX-90)) + 45; 22    23   int speed = 0; 24   25    26   if (_smallPlaneTime>35) { 27     UIImage *farTextureImageThree=[UIImage imageNamed:@"MemberTwo"]; 28     SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree]; 29      30      31     SKSpriteNode *foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size:CGSizeMake(farTextureThree.size.width/2.5, farTextureThree.size.height/2.5)]; 32     //增加敌对飞机受动力感应的范围 33     foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size]; 34      /*飞机向下移动速度*/ 35     speed= (arc4random() % 5) + 2; 36      37     foePlane.position = CGPointMake(x, self.size.height); 38     foePlane.zPosition=1; 39     /*下面三个属性是设置敌对飞机的重力检测属性,之后它们都会用到,比如之后用户操作的飞机发射子弹打在它们上面,下面这属性就起作用了*/ 40     foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane; 41     foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet; 42     foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet; 43     [self addChild:foePlane]; 44     /*当精灵的y坐标为0时将精灵从父节点移出*/ 45     [foePlane runAction:[SKAction sequence:@[[SKAction moveToY:0 duration:speed],[SKAction removeFromParent]]] completion:^{ 46       [foePlane removeFromParent]; 47     }]; 48    _smallPlaneTime=0; 49     50   } 51    52   if (_mediumPlaneTime>400) { 53    54    55      56     UIImage *farTextureImageThree=[UIImage imageNamed:@"Teamer"]; 57     SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree]; 58     SKSpriteNode *foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size:CGSizeMake(farTextureThree.size.width/2.5, farTextureThree.size.height/2.5)]; 59     //增加敌对飞机受动力感应的范围 60     foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size]; 61      /*飞机向下移动速度*/ 62      speed = (arc4random() % 2) + 2; 63  64     foePlane.position = CGPointMake(x, self.size.height); 65      66     foePlane.zPosition=1; 67     /*下面三个属性是设置敌对飞机的重力检测属性,之后它们都会用到,比如之后用户操作的飞机发射子弹打在它们上面,下面这属性就起作用了*/ 68     foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane; 69     foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet; 70     foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet; 71     [self addChild:foePlane]; 72     /*当精灵的y坐标为0时将精灵从父节点移出*/ 73     [foePlane runAction:[SKAction sequence:@[[SKAction moveToY:0 duration:speed],[SKAction removeFromParent]]] completion:^{ 74       [foePlane removeFromParent]; 75     }]; 76        _mediumPlaneTime=0; 77     78    } 79   80    81    if (_bigPlaneTime>700) { 82       83       84      85       86       87       88      UIImage *farTextureImageThree=[UIImage imageNamed:@"Unknown"]; 89      SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree]; 90       91       92      SKSpriteNode *foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size:CGSizeMake(farTextureThree.size.width/2.5, farTextureThree.size.height/2.5)]; 93       94       95      //增加敌对飞机受动力感应的范围 96      foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size]; 97       98      /*飞机向下移动速度*/ 99       speed = (arc4random() % 5) + 3;100      foePlane.position = CGPointMake(x, self.size.height);101      foePlane.zPosition=1;102      /*下面三个属性是设置敌对飞机的重力检测属性,之后它们都会用到,比如之后用户操作的飞机发射子弹打在它们上面,下面这属性就起作用了*/103      foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane;104      foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet;105      foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet;106       [self addChild:foePlane];107      /*当精灵的y坐标为0时将精灵从父节点移出*/108      [foePlane runAction:[SKAction sequence:@[[SKAction moveToY:5 duration:speed],[SKAction removeFromParent]]] completion:^{109         [foePlane removeFromParent];110       }];111      _bigPlaneTime=0;112      113     }114  115   116  117 118   119 }

 

其实这里的精灵和材质类我都是封装了类的,这里为了显示代码,所以直接把封装类的代码写一起了。开发时不建议这样做。

 

下面是下载地址:http://download.csdn.net/detail/qq_35826634/9599204  有兴趣的朋友可以去下来看看