IOS 2D遊戲開發架構 SpriteKit-->續(建立使用者角色精靈--原創),iosspritekit--

來源:互聯網
上載者:User

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

 


  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.