문제

I'm building a game using Sprite Kit that needs quick precise movements following the user's touch. I need to detect if the user has touched on the "Player" in the view, and if they have, when they move, the player sprite needs to move with the touch accordingly.

I have this working now, however, it's not very precise... the more movement input (without lifting your finger), the more offset the sprite gets from the touch location.

Here's the code I'm using right now.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{


    if (self.isFingerOnPlayer) {
        UITouch* touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInNode:self];
        CGPoint previousLocation = [touch previousLocationInNode:self];


        // Calculate new position for player
        int playerX = player.position.x + (touchLocation.x - previousLocation.x);
        int playerY = player.position.y + (touchLocation.y - previousLocation.y);

        // Limit x and y so that the player will not leave the screen any
        playerX = MAX(playerX, player.size.width/2);
        playerX = MIN(playerX, self.size.width - player.size.width/2);
        playerY = MAX(playerY, (player.size.width/2)-3+inBoundsOffset);
        playerY = MIN(playerY, (self.size.height - ((player.size.width/2)-3) - inBoundsOffset));


        // Update position of player
        player.position = CGPointMake(playerX, playerY);


    }


}

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

    UITouch* touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];

    SKPhysicsBody* body = [self.physicsWorld bodyAtPoint:touchLocation];
    if (body && [body.node.name isEqualToString: @"player"]) {
        NSLog(@"Began touch on player");
        self.isFingerOnPlayer = YES;

    }
}

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    self.isFingerOnPlayer = NO;
}

It detects the touch location, checks to make sure that you're touching the player sprite, if you are, then when you move, so does the sprite... but it can get off quite quickly if you're slinging your finger around (as playing this game will cause the player to do).

Can anybody suggest a more accurate way of accomplishing this, that will keep the sprite under the user's finger even when moving around a lot without lifting your finger?

도움이 되었습니까?

해결책

You have to keep in mind that -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event ONLY gets called when the moving finger writes (sorry couldn't help the Inspector Clouseau reference).

In effect what happens is that the user can very quickly move his/her finger from one location to the next and as soon the the finger is lifted, your position updates stops.

What I suggest you do is create a CGPoint property and have -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event store the location in the CGPoint property.

Then in your -(void)update:(CFTimeInterval)currentTime you add the code that actually moves the player to the finger coordinates. This should make things a lot smoother.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top