Sprite kit 使用路径旋转精灵
嗨,我有一个精灵沿着一条贝塞尔曲线的路径,我希望精灵旋转到它当前移动的方向,我可以通过在中将Sprite kit 使用路径旋转精灵,sprite-kit,bezier,2d-games,Sprite Kit,Bezier,2d Games,嗨,我有一个精灵沿着一条贝塞尔曲线的路径,我希望精灵旋转到它当前移动的方向,我可以通过在中将orientToPath设置为YES来告诉它沿着路径 SKaction *enemyCurve = [SKAction followpath:cgpath asOffset:NO orientToPath:YES duration:5]; 但这只会导致精灵面对运动的最后一点。有什么建议可以让它旋转到当前的方向吗? 我已经包含了我用于下面路径的代码。谢谢你的帮助和建议 -(void)AddEnemy:(M
orientToPath
设置为YES
来告诉它沿着路径
SKaction *enemyCurve = [SKAction followpath:cgpath asOffset:NO orientToPath:YES duration:5];
但这只会导致精灵面对运动的最后一点。有什么建议可以让它旋转到当前的方向吗?
我已经包含了我用于下面路径的代码。谢谢你的帮助和建议
-(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];
}
更新中的方法仅链接到顶部发布的代码,该代码确定了敌人将采取的路径。我还更新了该方法,以包括整个方法。使节点朝向特定的点。这个链接解释了我使用的精灵是如何用相同的代码自动旋转的。你在对敌方节点做其他事情吗?我建议你发布创建敌方节点的代码。嗨,很抱歉回复太晚,我已经更新了它,包括创建敌方节点的代码以及创建和应用路径的完整方法。你需要计算旋转角度,在物理更新方法中更新精灵的zRotation,至少我是这么做的。
[self AddEnemy:BezierCurve :NULL :NULL:One];