代码之家  ›  专栏  ›  技术社区  ›  kev3kev3

iOS SpriteKit触摸虚拟游戏杆外部会干扰游戏杆

  •  1
  • kev3kev3  · 技术社区  · 10 年前

    我对目标c是个新手,所以我仍在不断学习。我有一款游戏,它使用TheSneakyNarwhal提供的代码在屏幕上制作虚拟游戏杆。

    https://github.com/TheSneakyNarwhal/SpriteKit-Joystick

    我已经导入了git提供的文件,以下是与操纵杆实现相关的代码:

     -(Joystick *)newJoystickNode {
         SKSpriteNode *jsThumb = [SKSpriteNode spriteNodeWithImageNamed:@"joystick"];
         SKSpriteNode *jsBackdrop = [SKSpriteNode spriteNodeWithImageNamed:@"dpad"];
         Joystick *joystick = [Joystick joystickWithThumb:jsThumb andBackdrop:jsBackdrop];
         joystick.position = CGPointMake(jsThumb.size.width, jsThumb.size.width);
         joystick.name = @"playerJoystick";
         //[joystick setScale:0.8];
         return joystick;
     }
    
     -(void)joystickMovement
     {
         Joystick *joystick = (Joystick*)[self childNodeWithName:@"playerJoystick"];
         SKSpriteNode *player = (SKSpriteNode*)[self childNodeWithName:@"hero"];
         if ((joystick.velocity.x != 0 || joystick.velocity.y != 0) && (self.speed == 1))
          {
             player.position = CGPointMake(player.position.x + .1 *joystick.velocity.x, player.position.y + .1 * joystick.velocity.y);
          }
     }
    
    
     -(id)initWithSize:(CGSize)size {
         if (self = [super initWithSize:size]) {
             CADisplayLink *velocityTick = [CADisplayLink displayLinkWithTarget:self selector:@selector(joystickMovement)];
             [velocityTick addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
             [self addChild:[self newJoystickNode]];
             [self addChild:[self newFireButtonNode]];
         }
         return self;
     }
    

    这个操纵杆可以在屏幕上移动角色,并且工作正常。我已按照上述git提供的说明进行操作。操纵手柄位于屏幕左下角。问题是在某些情况下,如果我用操纵杆移动角色并触摸屏幕上的任何其他位置,这可能会干扰操纵杆并将其拉向右侧。

    是否有人将此实现用于spriteKit操纵杆并遇到此问题?或者有人知道为什么会发生这种情况吗?

    1 回复  |  直到 10 年前
        1
  •  0
  •   ZeMoon    10 年前

    我以前也遇到过同样的问题。我为我的游戏修正了这个问题,修改了journey.m文件中的-touchesMoved方法。

    只需复制以下代码并将其替换为 -touchesMoved 中的方法 Joystick.m

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if (isTracking == YES)
        {
            for (UITouch *touch in touches)
            {
                CGPoint touchPoint = [touch locationInNode:self];
                if (sqrtf(powf((touchPoint.x - self.anchorPointInPoints.x), 2) + powf((touchPoint.y - self.anchorPointInPoints.y), 2)) <= thumbNode.size.width)
                {
                    CGPoint moveDifference = CGPointMake(touchPoint.x - self.anchorPointInPoints.x, touchPoint.y - self.anchorPointInPoints.y);
    
                    thumbNode.position = CGPointMake(self.anchorPointInPoints.x + moveDifference.x, self.anchorPointInPoints.y + moveDifference.y);
                }
                else
                {
                    double vX = touchPoint.x - self.anchorPointInPoints.x;
                    double vY = touchPoint.y - self.anchorPointInPoints.y;
    
                    if (!(vX > 256 || vY > 256)) //Touchpoint should be within limits. Change the values to suit your situation.
                    {
                        double magV = sqrt(vX*vX + vY*vY);
                        double aX = self.anchorPointInPoints.x + vX / magV * thumbNode.size.width;
                        double aY = self.anchorPointInPoints.y + vY / magV * thumbNode.size.width;
    
                        thumbNode.position = CGPointMake(aX, aY);
                    }
    
                }
            }
            velocity = CGPointMake(((thumbNode.position.x - self.anchorPointInPoints.x)), ((thumbNode.position.y - self.anchorPointInPoints.y)));
    
            angularVelocity = -atan2(thumbNode.position.x - self.anchorPointInPoints.x, thumbNode.position.y - self.anchorPointInPoints.y);
    
            [_delegate joystickMovement];
        }
    
    }