用路径旋转精灵

时间:2015-02-05 14:09:27

标签: sprite-kit bezier 2d-games

嗨我有一个精灵跟随一条bezier曲线的路径,并且id像精灵一样旋转到它当前正在移动的方向,我可以通过将orientToPath设置为{{1来告诉它遵循路径在

YES

但这只会导致精灵面对最后的移动点。有关如何使其旋转到当前方向的任何建议吗? 我已经包含了我用于下面路径的代码。感谢您提供任何帮助和建议。

SKaction *enemyCurve = [SKAction followpath:cgpath asOffset:NO orientToPath:YES duration:5];

下面可以看到创建敌人节点的代码,我不对该节点应用任何旋转。

在initWithSize中:

-(void)AddEnemy:(MotionType *)motion :(CGPoint*)Start :(CGPoint*)Finish :(EnemyType)enemyType{
double curTime = CACurrentMediaTime();

if (enemyType == One) {
    if(curTime > _nextAsteroidspawn)
    {
        float randSecs = [self randomValueBetween:0.20 andValue:1.0];
        _nextAsteroidspawn = randSecs + curTime;

        float randY = [self randomValueBetween:0.0 andValue:self.frame.size.height];
        float randDuration = [self randomValueBetween:2.0 andValue:10.0];


        SKSpriteNode *asteroid = [_Asteroids objectAtIndex:_nextAsteroid];
        _nextAsteroid++;

        if(_nextAsteroid >= _Asteroids.count){
            _nextAsteroid = 0;
        }



        [asteroid removeAllActions];
        asteroid.position = CGPointMake(self.frame.size.width+asteroid.size.width/2, 50);
        asteroid.hidden = NO;

        CGMutablePathRef cgpath = CGPathCreateMutable();

        CGPoint startingPoint = CGPointMake(self.frame.size.width+asteroid.size.width/2 , 50);

        CGPoint controlPoint1 = CGPointMake(160, 300);
        CGPoint controlPoint2 = CGPointMake(200, 600);

        CGPoint endingPoint = CGPointMake(0, self.frame.size.width - self.frame.size.width - asteroid.size.width / 2);

        CGPathMoveToPoint(cgpath, NULL, startingPoint.x, startingPoint.y);
        CGPathAddCurveToPoint(cgpath, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x
                              , controlPoint2.y, endingPoint.x
                              , endingPoint.y);


        SKAction *enemyCurve = [SKAction followPath:cgpath asOffset:NO orientToPath:YES duration:5];



        CGPoint location = CGPointMake(-self.frame.size.width-asteroid.size.width, randY);

        SKAction *moveAction = [SKAction moveTo:location duration:randDuration];
        SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^(){

            asteroid.hidden = YES;
        }];


        SKAction *moveAsteroidActionWithDone = [SKAction sequence:@[enemyCurve,moveAction, doneAction]];

        [asteroid runAction:moveAsteroidActionWithDone withKey:@"asteroidMoving"];

        CGPathRelease(cgpath);

        //[asteroid runAction: [SKAction repeatActionForever:[SKAction animateWithTextures:_Asteroids timePerFrame:0.1f]]];
    }

更新:

_Asteroids = [[NSMutableArray alloc] initWithCapacity:knumAsteroids];
        for(int i = 0; i < knumAsteroids; i++)
        {
            SKSpriteNode *asteroid = [SKSpriteNode spriteNodeWithImageNamed:@"enemyBasic1"];
            asteroid.hidden = YES;
            [_Asteroids addObject:asteroid];
            [self addChild:asteroid];
        }

更新中的方法只链接到顶部发布的代码,该代码确定敌人将采取的路径我还更新了它以包括整个方法。

0 个答案:

没有答案