一、说明
今天给角色精灵增加了子弹发射功能,增加了子弹与敌对精灵的碰撞检测,当角色精灵子弹与敌对精灵碰撞后,它们都会从屏幕上消失。
二、场景层SKScene的修改
1、 在初始化场景层的方法中增加下面代码
self.physicsWorld.contactDelegate = self;
self.physicsWorld.gravity = CGVectorMake(0,0);
2、初始化场景层增加场景动力检测代理
1 - (instancetype)initWithSize:(CGSize)size 2 { 3 if (self = [super initWithSize:size]) { 4 5 NearbyArray=[[NSMutableArray alloc]init]; 6 /*第一个场景背景节点*/ 7 UIImage *farTextureImage=[UIImage imageNamed:@"planeBack"]; 8 SKTexture *farTexture = [SKTexture textureWithImage:farTextureImage]; 9 10 SKSpriteNode *farTextureSpriteOne = [SKSpriteNode spriteNodeWithTexture:farTexture size:self.size]; 11 // farTextureSpriteOne.anchorPoint=CGPointMake(DEVICE_Width/2, DEVICE_Height/2); 12 farTextureSpriteOne.zPosition=0; 13 farTextureSpriteOne.position=CGPointMake(self.frame.size.width/2, self.frame.size.height/2 ); 14 15 16 17 18 /*第二个场景背景节点*/ 19 UIImage *farTextureImageTwo=[UIImage imageNamed:@"planeBack"]; 20 SKTexture *farTextureTwo = [SKTexture textureWithImage:farTextureImageTwo]; 21 SKSpriteNode *farTextureSpriteTwo = [SKSpriteNode spriteNodeWithTexture:farTextureTwo size:self.size]; 22 //farTextureSpriteTwo.anchorPoint=CGPointMake(0, 0); 23 farTextureSpriteTwo.zPosition=0; 24 farTextureSpriteTwo.position=CGPointMake(farTextureSpriteOne.position.x, -(self.frame.size.height/2-10)); 25 26 27 28 /*第三个场景背景节点*/ 29 UIImage *farTextureImageThree=[UIImage imageNamed:@"planeBack"]; 30 SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree]; 31 32 SKSpriteNode *farTextureSpriteThree =[SKSpriteNode spriteNodeWithTexture:farTextureThree size:self.size]; 33 34 farTextureSpriteThree.zPosition=0; 35 farTextureSpriteThree.position=CGPointMake(farTextureSpriteOne.position.x, -(self.frame.size.height/2+self.frame.size.height-20)); 36 37 38 39 40 [self addChild:farTextureSpriteOne]; 41 [self addChild:farTextureSpriteTwo]; 42 [self addChild:farTextureSpriteThree]; 43 44 /*把三个场景背景节点加到一个数组中去,等会滚动之后,才好快速获取每个节点,重置postion*/ 45 [NearbyArray addObject:farTextureSpriteOne]; 46 [NearbyArray addObject:farTextureSpriteTwo]; 47 [NearbyArray addObject:farTextureSpriteThree]; 48 49 50 51 52 FirendPlane= [SKFoePlane createPlayerPlane]; 53 FirendPlane.zPosition=1; 54 FirendPlane.position=CGPointMake(self.frame.size.width/2, 50 ); 55 [self addChild:FirendPlane]; 56 /*增加两个精灵产生动力之后的代理*/ 57 self.physicsWorld.contactDelegate = self; 58 self.physicsWorld.gravity = CGVectorMake(0,0); 59 } 60 return self; 61 62 }
三、初始化角色精灵的子弹
1 -(void)PlayerBulletFire 2 { 3 4 bulletTime++; 5 if (bulletTime==10) { 6 UIImage *RolePlaneImage=[UIImage imageNamed:@"加号"]; 7 8 SKTexture *RolePlaneImageTextture = [SKTexture textureWithImage:RolePlaneImage]; 9 FirendPlane.PlayerBulletSprite=[SKSpriteNode spriteNodeWithTexture:RolePlaneImageTextture size:CGSizeMake(DEVICE_Width*0.06, DEVICE_Width*0.06)]; 10 FirendPlane.PlayerBulletSprite.zPosition=1; 11 FirendPlane.PlayerBulletSprite.position=CGPointMake(FirendPlane.position.x, FirendPlane.position.y+10 ); 12 [self addChild:FirendPlane.PlayerBulletSprite]; 13 FirendPlane.PlayerBulletSprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:FirendPlane.PlayerBulletSprite.size]; 14 15 FirendPlane.PlayerBulletSprite.physicsBody.collisionBitMask = 1; 16 17 [FirendPlane.PlayerBulletSprite runAction:[SKAction sequence:@[[SKAction moveToY:DEVICE_Height+50 duration:1.5],[SKAction removeFromParent]]]]; 18 bulletTime=0; 19 } 20 21 22 }
四、update中增加bullet初始化的方法
2 -(void)update:(CFTimeInterval)currentTime { 3 7 [self PlayerBulletFire]; 8 }
五、实现场景动力检测代理
下面我说明下:因为在前面的代码中增加了对子弹和敌对精灵的重力碰撞检测的代码,所以当为此类型的两个单位碰撞到一起之后,场景就会自动执行
下面的代理,下面的代理就是将两个相互碰撞的精灵移出场景层。
1 -(void)didBeginContact:(SKPhysicsContact *)contact 2 { 3 [contact.bodyA.node removeFromParent]; 4 [contact.bodyB.node removeFromParent]; 5 6 }
六、案例地址截图
案例 http://download.csdn.net/detail/liaohang1987x/9612989
时间: 2024-10-24 15:39:22