Question

I am new to objective c so still very much learning the ropes. I have a game which uses code provided by TheSneakyNarwhal to make a virtual joystick on screen.

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:

 -(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. 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? Or does anybody have any idea why this would be happening?

Was it helpful?

Solution

I had encountered the same problem earlier. I fixed it for my game by modifying the -touchesMoved method in the Joystick.m file.

Just copy and replace the following code with the -touchesMoved method in 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];
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top