IOS 2D遊戲開發架構 SpriteKit-->續(建立使用者角色精靈--原創),iosspritekit--
一、主要實現
今天spritekit實現建立玩家角色精靈(SKSpriteNode *), 增加角色精靈的手勢操作,這裡增加的手勢計算方法與objective-c中是不一樣的,因為objective-c使用的座標系與spritekit使用的座標系不是一樣的,後面還增加了精靈的碰撞檢查代碼。
二、 SKSpriteNode手勢
SKSpriteNode類內建5個手勢監測的方法,
// 手指按下的時候調用
1、 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
// 手指移動的時候調用
2、 -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
// 手指抬起的時候調用
3、- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
取消(非正常離開螢幕,意外中斷事件)
4、 -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
3D Touch相關方法,當前觸摸對象估計的觸摸特性,傳回值是UITouchPropertyie、
5、 -(void)touchesEstimatedPropertiesUpdated:(NSSet *)touches
6、上面5個方法中用得最多的是 1、2、3,下面我們要操作的角色精靈也是用到這三個手勢,實現的思路→當使用者點擊螢幕時進入到1手勢,判斷點擊的座標點是不是在角色精靈精靈上,如果是才能執行2手勢,代碼中用了一個int變數紀錄,如果點擊到了角色精靈int=1,int=1時2手勢才能執行,當使用者手抬起時,將會執行手勢3,說明手勢結束,結束後我們將int=1設定為=0。
二、 代碼
1.情境層初始化中增加建立角色精靈的代碼:SKSpriteNode * FirendPlane
1 UIImage *RolePlaneImage=[UIImage imageNamed:@"AttackPlane"];2 SKTexture *RolePlaneImageTextture = [SKTexture textureWithImage:RolePlaneImage];3 FirendPlane=[SKSpriteNode spriteNodeWithTexture:RolePlaneImageTextture size:CGSizeMake(DEVICE_Width*0.25, DEVICE_Width*0.25)];4 FirendPlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:FirendPlane.size];5 /*增加碰撞監測代碼*/6 FirendPlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane;7 FirendPlane.zPosition=1;8 FirendPlane.position=CGPointMake(self.frame.size.width/2, 50 );9 [self addChild:FirendPlane];
2.增加手勢
1 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 2 { 3 UITouch *touctObj = [touches anyObject]; 4 5 CGPoint location=[touctObj locationInNode:self]; 6 7 8 CGFloat CurrentTagAreaX=FirendPlane.position.x-FirendPlane.size.width/2; 9 10 CGFloat CurrentTagAreaY=FirendPlane.position.y-FirendPlane.size.height/2;11 if (location.x>=CurrentTagAreaX &&location.x<=FirendPlane.position.x+(FirendPlane.size.width/2) &&12 location.y>=CurrentTagAreaY &&location.y<=FirendPlane.position.y+(FirendPlane.size.height/2)) {13 14 IsOrNoTachMyPlane=1;15 }16 17 }18 19 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event20 {21 IsOrNoTachMyPlane=0;22 23 }24 25 -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event26 {27 if (IsOrNoTachMyPlane>0) {28 UITouch *touctObj = [touches anyObject];29 CGPoint location=[touctObj locationInNode:self];30 31 32 if (location.x >= self.size.width - (FirendPlane.size.width / 2)) {33 34 location.x = self.size.width - (FirendPlane.size.width / 2);35 36 }else if (location.x <= (FirendPlane.size.width / 2)) {37 38 location.x = FirendPlane.position.x;39 40 }41 42 43 if (location.y >= self.size.height - (FirendPlane.size.height / 2)) {44 45 location.y = self.size.height - (FirendPlane.size.height / 2);46 47 48 49 }else if (location.y <= (FirendPlane.size.height / 2)) {50 51 location.y = FirendPlane.position.y;52 53 }54 55 SKAction *action = [SKAction moveTo:CGPointMake(location.x, location.y) duration:0];56 57 [FirendPlane runAction:action];58 }59
二、
http://download.csdn.net/detail/liaohang1987x/9610880