문제

i am moving a sprite using the touches moved method. currently the sprite jumps to the point on which the screen is touched but I want the sprite only to move when it is touched directly.

my code:

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

    for (UITouch *touch in touches) {

        CGPoint location = [touch locationInNode:self];
        CGPoint newPosition = CGPointMake(location.x, self.size.height/2);

        self.sprite.position = newPosition;
     }
 }
도움이 되었습니까?

해결책

check if the touch location is inside the sprite, like this:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    if(CGRectContainsPoint(self.sprite.boundingBox,positionInScene)) {

        CGPoint newPosition = CGPointMake(positionInScene.x, self.size.height/2);

        self.sprite.position = newPosition;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top