zoukankan      html  css  js  c++  java
  • IOS 2D游戏开发框架 SpriteKit-->续(创建用户角色精灵--原创)

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

    https://www.baiydu.com

    一、主要实现
       今天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

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

     

      

  • 相关阅读:
    Go语言TCP/UDP Socket编程
    Go目录
    Go语言获取项目当前路径
    Mysql写入记录出现 Incorrect string value: 'xB4xE7xB1xCAxBCxC7‘错误?(写入中文)
    Erlang的Web库和框架
    erlang 资源
    Erlang基础2
    Erlang语言基础总结
    angular修改端口号port
    npm ERR! Cannot read property 'resolve' of undefined
  • 原文地址:https://www.cnblogs.com/xiaoliao/p/5800747.html
Copyright © 2011-2022 走看看