本人开发的开发者技术变现资源聚集地,大家支持下,下面是网址
https://www.baiydu.com
这次包括之后讲的spritekit 我都会围绕一个案例来说,这个案例就是一个简单的2d飞机大战游戏,今天这里我讲创建敌对精灵,就是敌对飞机,敌对飞机不停的被刷新到屏幕上.....当然这里涉及到的类其实还是,精灵,和材质两个类,这两个类前两篇的案例中已经出现过,使用方法都一样,主要看逻辑,我这里主要是实现每间隔一段时间屏幕就刷新一个敌对飞机。飞机从屏幕高度为起点往下移动,当potion移动到0时将敌对飞机移出。
/*这个方法是spritekit 的场景自带的,每过一秒就会被调用*/
-(void)update:(CFTimeInterval)currentTime {
[self BackMove:]; [self initEnemySprite];//本次增加的创建敌对飞机的方法 }
/*创建敌对飞机*/
-(void)initEnemySprite
{
/*此方法是放在update里面的所以是每秒执行一次,下面三个变量就是控制飞机刷新速度的,如果不控制,屏幕就会每秒都刷新一个飞机出来,那么不一会屏幕就会爆满, 这里每35秒刷新一架小型地址每400秒刷新一架中型飞机, 每700秒刷行一架大飞机*/
_smallPlaneTime++;
_mediumPlaneTime++;
_bigPlaneTime++; //int RadomNumber= (arc4random() % 100) + 0;
int SpriteX=DEVICE_Width;
/*随机精灵在x轴的位置*/
int x = (arc4random() % (SpriteX-)) + ; int speed = ; if (_smallPlaneTime>) {
UIImage *farTextureImageThree=[UIImage imageNamed:@"MemberTwo"];
SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree]; SKSpriteNode *foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size:CGSizeMake(farTextureThree.size.width/2.5, farTextureThree.size.height/2.5)];
//增加敌对飞机受动力感应的范围
foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size];
/*飞机向下移动速度*/
speed= (arc4random() % ) + ; foePlane.position = CGPointMake(x, self.size.height);
foePlane.zPosition=;
/*下面三个属性是设置敌对飞机的重力检测属性,之后它们都会用到,比如之后用户操作的飞机发射子弹打在它们上面,下面这属性就起作用了*/
foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane;
foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet;
foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet;
[self addChild:foePlane];
/*当精灵的y坐标为0时将精灵从父节点移出*/
[foePlane runAction:[SKAction sequence:@[[SKAction moveToY: duration:speed],[SKAction removeFromParent]]] completion:^{
[foePlane removeFromParent];
}];
_smallPlaneTime=; } if (_mediumPlaneTime>) { UIImage *farTextureImageThree=[UIImage imageNamed:@"Teamer"];
SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree];
SKSpriteNode *foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size:CGSizeMake(farTextureThree.size.width/2.5, farTextureThree.size.height/2.5)];
//增加敌对飞机受动力感应的范围
foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size];
/*飞机向下移动速度*/
speed = (arc4random() % ) + ; foePlane.position = CGPointMake(x, self.size.height); foePlane.zPosition=;
/*下面三个属性是设置敌对飞机的重力检测属性,之后它们都会用到,比如之后用户操作的飞机发射子弹打在它们上面,下面这属性就起作用了*/
foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane;
foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet;
foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet;
[self addChild:foePlane];
/*当精灵的y坐标为0时将精灵从父节点移出*/
[foePlane runAction:[SKAction sequence:@[[SKAction moveToY: duration:speed],[SKAction removeFromParent]]] completion:^{
[foePlane removeFromParent];
}];
_mediumPlaneTime=; } if (_bigPlaneTime>) { UIImage *farTextureImageThree=[UIImage imageNamed:@"Unknown"];
SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree]; SKSpriteNode *foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size:CGSizeMake(farTextureThree.size.width/2.5, farTextureThree.size.height/2.5)]; //增加敌对飞机受动力感应的范围
foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size]; /*飞机向下移动速度*/
speed = (arc4random() % ) + ;
foePlane.position = CGPointMake(x, self.size.height);
foePlane.zPosition=;
/*下面三个属性是设置敌对飞机的重力检测属性,之后它们都会用到,比如之后用户操作的飞机发射子弹打在它们上面,下面这属性就起作用了*/
foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane;
foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet;
foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet;
[self addChild:foePlane];
/*当精灵的y坐标为0时将精灵从父节点移出*/
[foePlane runAction:[SKAction sequence:@[[SKAction moveToY: duration:speed],[SKAction removeFromParent]]] completion:^{
[foePlane removeFromParent];
}];
_bigPlaneTime=; } }
其实这里的精灵和材质类我都是封装了类的,这里为了显示代码,所以直接把封装类的代码写一起了。开发时不建议这样做。
下面是下载地址:http://download.csdn.net/detail/qq_35826634/9599204 有兴趣的朋友可以去下来看看
本人创业做的一款androidApp, 下载量已经有2000多万,各种当前热门的网络手机奖励红包全部集成,另外还有热门电影和淘宝高额优惠券!很适合各类型的用户。