Objective c 如何通过触摸spriteKit旋转图像/精灵?
我试着用一个手指触摸一个精灵旋转。老实说,我不知道怎么做,对spriteKit来说是个新手。Objective c 如何通过触摸spriteKit旋转图像/精灵?,objective-c,rotation,touch,sprite-kit,image-rotation,Objective C,Rotation,Touch,Sprite Kit,Image Rotation,我试着用一个手指触摸一个精灵旋转。老实说,我不知道怎么做,对spriteKit来说是个新手。 有什么想法吗?这将使用动作将精灵旋转到您触摸的位置。如果您希望它在拖动手指时旋转。您应该删除该操作并在触摸移动:上进行计算 -(void)didMoveToView:(SKView *)view { sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"]; sprite.xScale = 0.5; sprite
有什么想法吗?这将使用动作将精灵旋转到您触摸的位置。如果您希望它在拖动手指时旋转。您应该删除该操作并在
触摸移动:
上进行计算
-(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;
}
}
或者:(请记住从触摸开始:
- (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:方法,这是相当多的次数。如果您每次都添加动作,它可能看起来很像奇怪而且效率很低。所以你必须手动更新轮换。我更新了替代解决方案的答案。