首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过触摸spriteKit来旋转图像/精灵?

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

Stack Overflow用户
提问于 2014-10-11 19:12:04
回答 1查看 982关注 0票数 1

我试着用一根手指来旋转一个精灵。老实说,我不知道该怎么做,这是spriteKit的新鲜事。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-12 11:48:13

这将使雪碧旋转到你使用动作触摸的地方。如果你想让它在拖动手指的时候旋转。您应该删除该操作并在touchesMoved:上进行计算。

代码语言:javascript
复制
-(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:中删除代码)

代码语言:javascript
复制
- (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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26318283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档