简体   繁体   English

触摸事件中精灵的旋转

[英]Rotation of Sprite in touch event

How am i able to rotate a SpriteNode in the touch event? 如何在触摸事件中旋转SpriteNode?

Here the code that've got so far: 这里是到目前为止的代码:

 // Detect what side of the screen the user has touched.
 if(touchLocation.x < 160) { 
   // They've touched the left side (the car turns  left)
   [car runAction:[SKAction rotateByAngle:-50 duration:0]];
   [car.physicsBody applyImpulse:CGVectorMake(-20,0)];
   car.physicsBody.velocity = CGVectorMake(-20, 0);

 }else{
   // They've touched the right side (the car turns right)
   [car runAction:[SKAction rotateByAngle:50 duration:0]];
   [car.physicsBody applyImpulse:CGVectorMake(20,0)];
   _bird.physicsBody.velocity = CGVectorMake(20, 0);
 }

The rotation for -50 doesn't work, even though other Stackoverflow questions have given that as an accepted answer. 即使其他Stackoverflow问题已将其作为可接受的答案,-50的旋转也不起作用。 Also the working normal 50 degree turn seems to work for a split second then revert back to its original place. 正常的50度转弯似乎也可以工作一秒钟,然后恢复到原始位置。

How would I get the rotation to stick and rotate smoothly? 我如何使旋转保持平稳并旋转?

when you use rotateByAngle it's in radians. 当您使用rotateByAngle它以弧度为单位。 theres about ~6 radians in a circle. 一圈约有6个弧度。 so you're rotating this sprite many times over. 因此,您需要多次旋转此精灵。 It isn't a 50 degree turn the way you'd expect. 这并不是您所期望的50度转弯。 You need to be using variations of CGFloat(M_PI) . 您需要使用CGFloat(M_PI)变体。 If you want to convert between degrees and radians the conversion is 如果要在度和弧度之间转换,则转换为

let radians = CGFloat(M_PI) * degrees / 180.0

start there and then see if the sprite behaves in a more predictable way 从那里开始,然后查看精灵是否以更可预测的方式运行

since your duration for your animation is 0 there is really no reason to use an action there.. can you just set 由于动画的持续时间为0,因此实际上没有理由在其中使用动作。

car.zRotation = radians

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM