【问题标题】:How to rotate and point to cursor location in SpriteKit?如何在 SpriteKit 中旋转并指向光标位置?
【发布时间】:2014-11-01 07:54:07
【问题描述】:

我正在使用 SpriteKit 的 Spaceship 演示,我希望它旋转到我在屏幕上单击的位置,然后朝它移动。

我当前的代码只让它从位置 50,50 开始向上移动屏幕:

sprite.position = CGPointMake(50,50);
SKAction *fly = [SKAction moveByX:0.0F y:50.0F duration:1];
[sprite runAction:[SKAction repeatActionForever:fly];

我该如何让它发挥作用?

【问题讨论】:

    标签: iphone ios7 sprite-kit


    【解决方案1】:

    这么长的回答,这会对你有所帮助。(我受益于raywenderlich的书)

    //Math Utilities
    static inline CGPoint CGPointSubtract(const CGPoint a,
                                          const CGPoint b)
    {
        return CGPointMake(a.x - b.x, a.y - b.y);
    }
    
    static inline CGPoint CGPointMultiplyScalar(const CGPoint a,
                                                const CGFloat b)
    {
        return CGPointMake(a.x * b, a.y * b);
    }
    
    static inline CGFloat CGPointLength(const CGPoint a)
    {
        return sqrtf(a.x * a.x + a.y * a.y);
    }
    
    static inline CGPoint CGPointNormalize(const CGPoint a)
    {
        CGFloat length = CGPointLength(a);
        return CGPointMake(a.x / length, a.y / length);
    }
    
    static inline CGPoint CGPointAdd(const CGPoint a,
                                     const CGPoint b)
    {
        return CGPointMake(a.x + b.x, a.y + b.y);
    }
    
    static const float SHIP_SPEED = 60.0;
    
    @implementation yourScene
    {
        SKSpriteNode *ship;
        NSTimeInterval _lastUpdateTime;
        NSTimeInterval _dt;
        CGPoint _velocity;
        CGPoint _lastTouchLocation;
    }
    
    -(void)didMoveToView:(SKView *)view 
    {
        ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
        ship.position = CGPointMake(50, 50);
        [self addChild:ship];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInNode:self.scene];
        [self moveShipToward:touchLocation];//Move Toward
    }
    
    -(void)update:(CFTimeInterval)currentTime {
        /* Called before each frame is rendered */
        {
            if (_lastUpdateTime) {
                _dt = currentTime - _lastUpdateTime;
            } else {
                _dt = 0;
            }
            _lastUpdateTime = currentTime;
    
            CGPoint offset = CGPointSubtract(_lastTouchLocation, ship.position);
            float distance = CGPointLength(offset);
            if (distance < SHIP_SPEED * _dt) {
                ship.position = _lastTouchLocation;
                _velocity = CGPointZero;
            } else {
                [self moveSprite:ship velocity:_velocity];
                [self rotateSprite:ship toFace:_velocity];
            }
        }
    }
    
    - (void)moveShipToward:(CGPoint)location
    {
        _lastTouchLocation = location;
        CGPoint offset = CGPointSubtract(location, ship.position);
        CGPoint direction = CGPointNormalize(offset);
        _velocity = CGPointMultiplyScalar(direction, SHIP_SPEED);
    }
    
    - (void)moveSprite:(SKSpriteNode *)sprite
              velocity:(CGPoint)velocity
    {
        CGPoint amountToMove = CGPointMultiplyScalar(velocity, _dt);
        sprite.position = CGPointAdd(sprite.position, amountToMove);
    }
    
    - (void)rotateSprite:(SKSpriteNode *)sprite
                  toFace:(CGPoint)direction
    {
        sprite.zRotation = atan2f(direction.y, direction.x);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 2013-04-14
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      相关资源
      最近更新 更多