2014-04-03 69 views
1

我是新來的目標c,所以還是非常瞭解繩索。我有一款遊戲,使用TheSneakyNwawhal提供的代碼在屏幕上製作虛擬遊戲杆。iOS SpriteKit虛擬操縱桿之外的觸摸會干擾操縱桿

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

我已經進口的混帳提供的文件,並將以下是相關的操縱桿實現的代碼:

-(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操縱桿,並有這個問題?或者有人知道爲什麼會發生這種情況嗎?

回答

0

我以前遇到過同樣的問題。我通過修改Joystick.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]; 
    } 

}