【问题标题】:How can I rotate image/sprite with touch on spriteKit?如何通过触摸 spriteKit 旋转图像/精灵?
【发布时间】:2014-10-11 19:12:04
【问题描述】:

我正在尝试用一根手指触摸使精灵旋转。老实说,我不知道该怎么做,spriteKit 的新手。 有什么想法吗?

【问题讨论】:

  • 只要你触摸它就一直旋转吗?或一段固定的时间?还是固定金额?
  • 您是想根据用户触摸的位置将精灵旋转到特定角度,还是只是无限期地旋转精灵?
  • 谢谢大家(:这家伙解决了它(:

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


【解决方案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;
    }
}

【讨论】:

  • 正如你所说,我希望它在拖动手指时旋转它。但是你删除动作是什么意思,没有动作精灵将如何旋转?代码可能会有所帮助...再次感谢!
  • 每次触摸移动时都会调用 touchesMoved: 方法,这种情况很多次。如果你每次都添加一个动作,它可能看起来很奇怪并且效率很低。因此,您必须手动更新轮换。我更新了替代解决方案的答案。
猜你喜欢
  • 1970-01-01
  • 2015-06-02
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多