【问题标题】:How to rotate something using spritekit relative to user touch coordinates如何使用 spritekit 相对于用户触摸坐标旋转某些东西
【发布时间】:2014-05-26 00:10:54
【问题描述】:

我想根据用户在屏幕上拖动手指来旋转枪。我想我需要极坐标中的笛卡尔点,并且我需要一个长按手势,这两者都是我所拥有的。我只是想知道我将如何进行编程?抱歉,我对精灵套件真的很陌生,我已经阅读了所有苹果的文档,但我很难找到这个。我的锚点是 0,0。

-(void)shootBullets:(UILongPressGestureRecognizer *) gestureRecognizer
{
    double radius;
    double angle;
      SKSpriteNode *gun = [self newGun];
 }

我想出了如何获得角度,但现在我的 zrotation 不起作用。就像我的 nslog 和角度是正确的但是当我点击 gun.zrotation 并且我点击什么都没有发生?请帮帮我,我快疯了。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
SKSpriteNode *gun = [self newGun];
self.gunRotation = atanf(location.y/location.x)/0.0174532925;
gun.zRotation = self.gunRotation;
NSLog(@"%f",gun.zRotation);
}

【问题讨论】:

    标签: ios iphone objective-c


    【解决方案1】:

    这是一个非常古老的问题,但这是我所做的 - 希望它会帮助那里的人:

    #import "MyScene.h"
    #define SK_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180
    

    然后添加这个:

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        for (UITouch *touch in touches) {
            CGPoint positionInScene = [touch locationInNode:self];
    
            float deltaX = positionInScene.x - gun.position.x;
            float deltaY = positionInScene.y - gun.position.y;
    
            float angle = atan2f(deltaY, deltaX);
    
            gun.zRotation = angle - SK_DEGREES_TO_RADIANS(90.0f);  
        }
    }
    

    【讨论】:

    • 我不懂这种语言,这个答案出现在“迟到的答案”中。这里面应该有f吗? * 0.01745329252f
    • SK_DEGREES_TO_RADIANS(90.0f) 实际上是M_PI_2
    • 有一个内置的角度和弧度函数:GLKMathDegreesToRadians().
    【解决方案2】:

    查看answers I got to this question about how to implement a rotary knob,人们已经弄清楚了所有的几何形状,你需要做的就是实现一个透明的旋钮,获取它的角度并将它传递给你的精灵套件对象,使用动作旋转

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多