如何通过触摸spriteKit旋转图像/精灵?

时间:2014-10-11 19:12:04

标签: objective-c rotation touch sprite-kit image-rotation

我试图用手指触摸旋转精灵。老实说,我不知道怎么做,spriteKit的新手。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这会将精灵旋转到您使用动作触摸的位置。如果您想在拖动手指时旋转它。您应该删除操作并在touchesMoved:上进行计算。

-(void)didMoveToView:(SKView *)view {
    sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];

    sprite.xScale = 0.5;
    sprite.yScale = 0.5;
    sprite.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height/2);

    [self addChild:sprite];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];


        float dY = sprite.position.y - location.y;
        float dX = sprite.position.x - location.x;
        float angle = atan2f(dY, dX) + 1.571f;
        [sprite runAction:[SKAction rotateToAngle:angle duration:0.5 shortestUnitArc:YES]];
        return;
    }
}

或者:(请记住从touchesBegan:

中删除代码
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        float dY = sprite.position.y - location.y;
        float dX = sprite.position.x - location.x;
        float angle = (atan2f(dY, dX)) + 1.571f;
        sprite.zRotation = angle;
    }
}