码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 2D游戏开发框架 SpriteKit-->续(创建用户角色精灵--原创)

时间:2016-08-23 21:59:01      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

一、主要实现
   今天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 *)event
20 {
21     IsOrNoTachMyPlane=0;
22     
23 }
24 
25 -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
26 {
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

 


  

IOS 2D游戏开发框架 SpriteKit-->续(创建用户角色精灵--原创)

标签:

原文地址:http://www.cnblogs.com/xiaoliao/p/5800747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!