一、說明 今天給角色精靈增加了子彈發射功能,增加了子彈與敵對精靈的碰撞檢測,當角色精靈子彈與敵對精靈碰撞後,它們都會從屏幕上消失。 二、場景層SKScene的修改 1、 在初始化場景層的方法中增加下麵代碼 self.physicsWorld.contactDelegate = self; self. ...
一、說明
今天給角色精靈增加了子彈發射功能,增加了子彈與敵對精靈的碰撞檢測,當角色精靈子彈與敵對精靈碰撞後,它們都會從屏幕上消失。
二、場景層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