简体   繁体   English

虚拟操纵杆外部的iOS SpriteKit触摸会干扰操纵杆

[英]iOS SpriteKit touch outside of virtual joystick interferes with joystick

I am new to objective c so still very much learning the ropes. 我对目标c还是陌生的,因此仍然非常学习。 I have a game which uses code provided by TheSneakyNarwhal to make a virtual joystick on screen. 我有一个使用TheSneakyNarwhal提供的代码在屏幕上制作虚拟游戏杆的游戏。

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

I have imported the files provided at the git and the following is the code relevant to the joystick implementation: 我已经导入了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;
 }

This joystick moves the character around the screen and works fine. 该操纵杆可在屏幕上移动角色并正常工作。 I have followed the instructions provided at the above git. 我已经按照上面git提供的说明进行操作。 The joystick is placed in the lower left corner of the screen. 游戏杆放置在屏幕的左下角。 Problem is on some occasions if I am moving the character with the joystick and touch anywhere else on the screen this can interfere with the joystick and pull it to the right. 问题是在某些情况下,如果我用操纵杆移动角色并触摸屏幕上的其他任何地方,可能会干扰操纵杆并将其向右拉。

Has anybody used this implementation for a the spriteKit joystick and had this problem? 是否有人将这种实现用于s​​priteKit游戏杆,并遇到了这个问题? Or does anybody have any idea why this would be happening? 还是有人知道为什么会这样?

I had encountered the same problem earlier. 我之前遇到过同样的问题。 I fixed it for my game by modifying the -touchesMoved method in the Joystick.m file. 我通过修改Joystick.m文件中的-touchesMoved方法为游戏修复了该问题。

Just copy and replace the following code with the -touchesMoved method in Joystick.m 只需将以下代码复制并替换为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];
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM