IOS 2D游戏开发框架 SpriteKit-->续(完善角色功能)

时间:2021-07-19 17:14:51

本人开发的开发者技术变现资源聚集地,大家支持下,下面是网址

https://www.baiydu.com

一、说明
       今天给角色精灵增加了子弹发射功能,增加了子弹与敌对精灵的碰撞检测,当角色精灵子弹与敌对精灵碰撞后,它们都会从屏幕上消失。

二、场景层SKScene的修改

1、 在初始化场景层的方法中增加下面代码
self.physicsWorld.contactDelegate = self;

self.physicsWorld.gravity = CGVectorMake(0,0);

2、初始化场景层增加场景动力检测代理

 - (instancetype)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) { NearbyArray=[[NSMutableArray alloc]init];
/*第一个场景背景节点*/
UIImage *farTextureImage=[UIImage imageNamed:@"planeBack"];
SKTexture *farTexture = [SKTexture textureWithImage:farTextureImage]; SKSpriteNode *farTextureSpriteOne = [SKSpriteNode spriteNodeWithTexture:farTexture size:self.size];
// farTextureSpriteOne.anchorPoint=CGPointMake(DEVICE_Width/2, DEVICE_Height/2);
farTextureSpriteOne.zPosition=;
farTextureSpriteOne.position=CGPointMake(self.frame.size.width/, self.frame.size.height/ ); /*第二个场景背景节点*/
UIImage *farTextureImageTwo=[UIImage imageNamed:@"planeBack"];
SKTexture *farTextureTwo = [SKTexture textureWithImage:farTextureImageTwo];
SKSpriteNode *farTextureSpriteTwo = [SKSpriteNode spriteNodeWithTexture:farTextureTwo size:self.size];
//farTextureSpriteTwo.anchorPoint=CGPointMake(0, 0);
farTextureSpriteTwo.zPosition=;
farTextureSpriteTwo.position=CGPointMake(farTextureSpriteOne.position.x, -(self.frame.size.height/-)); /*第三个场景背景节点*/
UIImage *farTextureImageThree=[UIImage imageNamed:@"planeBack"];
SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree]; SKSpriteNode *farTextureSpriteThree =[SKSpriteNode spriteNodeWithTexture:farTextureThree size:self.size]; farTextureSpriteThree.zPosition=;
farTextureSpriteThree.position=CGPointMake(farTextureSpriteOne.position.x, -(self.frame.size.height/+self.frame.size.height-)); [self addChild:farTextureSpriteOne];
[self addChild:farTextureSpriteTwo];
[self addChild:farTextureSpriteThree]; /*把三个场景背景节点加到一个数组中去,等会滚动之后,才好快速获取每个节点,重置postion*/
[NearbyArray addObject:farTextureSpriteOne];
[NearbyArray addObject:farTextureSpriteTwo];
[NearbyArray addObject:farTextureSpriteThree]; FirendPlane= [SKFoePlane createPlayerPlane];
FirendPlane.zPosition=;
FirendPlane.position=CGPointMake(self.frame.size.width/, );
[self addChild:FirendPlane];
/*增加两个精灵产生动力之后的代理*/
self.physicsWorld.contactDelegate = self;
self.physicsWorld.gravity = CGVectorMake(,);
}
return self; }

三、初始化角色精灵的子弹

 -(void)PlayerBulletFire
{ bulletTime++;
if (bulletTime==) {
UIImage *RolePlaneImage=[UIImage imageNamed:@"加号"]; SKTexture *RolePlaneImageTextture = [SKTexture textureWithImage:RolePlaneImage];
FirendPlane.PlayerBulletSprite=[SKSpriteNode spriteNodeWithTexture:RolePlaneImageTextture size:CGSizeMake(DEVICE_Width*0.06, DEVICE_Width*0.06)];
FirendPlane.PlayerBulletSprite.zPosition=;
FirendPlane.PlayerBulletSprite.position=CGPointMake(FirendPlane.position.x, FirendPlane.position.y+ );
[self addChild:FirendPlane.PlayerBulletSprite];
FirendPlane.PlayerBulletSprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:FirendPlane.PlayerBulletSprite.size]; FirendPlane.PlayerBulletSprite.physicsBody.collisionBitMask = ; [FirendPlane.PlayerBulletSprite runAction:[SKAction sequence:@[[SKAction moveToY:DEVICE_Height+ duration:1.5],[SKAction removeFromParent]]]];
bulletTime=;
} }

四、update中增加bullet初始化的方法

 -(void)update:(CFTimeInterval)currentTime {

     [self PlayerBulletFire];
}

五、实现场景动力检测代理

下面我说明下:因为在前面的代码中增加了对子弹和敌对精灵的重力碰撞检测的代码,所以当为此类型的两个单位碰撞到一起之后,场景就会自动执行

下面的代理,下面的代理就是将两个相互碰撞的精灵移出场景层。

  -(void)didBeginContact:(SKPhysicsContact *)contact
{
[contact.bodyA.node removeFromParent];
[contact.bodyB.node removeFromParent]; }

本人创业做的一款androidApp, 下载量已经有2000多万,各种当前热门的网络手机奖励红包全部集成,另外还有热门电影和淘宝高额优惠券!很适合各类型的用户。

 IOS 2D游戏开发框架 SpriteKit-->续(完善角色功能)