【问题标题】:iOS SpriteKit touch outside of virtual joystick interferes with joystickiOS SpriteKit 触摸虚拟操纵杆外会干扰操纵杆
【发布时间】:2014-04-03 13:30:54
【问题描述】:

我是目标 c 的新手,所以仍然非常了解其中的技巧。我有一个游戏,它使用 TheSneakyNarwhal 提供的代码在屏幕上制作虚拟操纵杆。​​p>

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 游戏杆上使用过这个实现并且遇到过这个问题?或者有人知道为什么会这样吗?

【问题讨论】:

    标签: ios objective-c sprite-kit joystick


    【解决方案1】:

    我之前也遇到过同样的问题。我通过修改 Joystick.m 文件中的 -touchesMoved 方法为我的游戏修复了它。

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

    -(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];
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多