如何使用CGPathAddArc从屏幕的右向左移动精灵

如何使用CGPathAddArc从屏幕的右向左移动精灵,path,automatic-ref-counting,sprite,Path,Automatic Ref Counting,Sprite,我的精灵位置是(屏幕宽度,屏幕高度/2)。我已经尝试了很多使用CGPathAddArc将精灵从右向左移动的代码,但没有成功。苹果的文档很难理解。所以我在这里发帖子,希望有人能帮助我。 这是我使用过的代码之一 if (iPad) { CGPathAddArc(thePath, NULL, 384.0f , 768, 384.0f , 0.f, -M_PI*3/2, NO); } SKAction *moveAction = [SKAction followPath:thePath

我的精灵位置是(屏幕宽度,屏幕高度/2)。我已经尝试了很多使用CGPathAddArc将精灵从右向左移动的代码,但没有成功。苹果的文档很难理解。所以我在这里发帖子,希望有人能帮助我。 这是我使用过的代码之一

if (iPad) {
        CGPathAddArc(thePath, NULL, 384.0f , 768, 384.0f , 0.f, -M_PI*3/2, NO);
}
SKAction *moveAction = [SKAction followPath:thePath asOffset:YES orientToPath:NO duration:(5.0f/200.0f)*(iPad ? 384.0f :200.f)];

许多人提前感谢

如果你创建一个空白的SpriteKit项目,这个稍微修改一下的GameSecene.m默认代码将使宇宙飞船沿着半圆的路径从右向左飞行

如果您需要进一步澄清,请发表评论

与代码片段的关键区别在于,对于
[SKAction followPath…]
而言,
asOffset
参数为否,而
orientToPath
为是

#import "GameScene.h"

@implementation GameScene

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];

    myLabel.text = @"Hello, World!";
    myLabel.fontSize = 65;
    myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));

    [self addChild:myLabel];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];

        sprite.xScale = 0.5;
        sprite.yScale = 0.5;
        sprite.position = location;

        // HERE COMES THE MODIFICATION
        CGMutablePathRef thePath = CGPathCreateMutable();
        CGPathAddArc(thePath, NULL, self.frame.size.width/2 , 200, self.frame.size.width/2 , 0.f, -M_PI, NO);
        SKAction *moveAction = [SKAction followPath:thePath asOffset:NO orientToPath:YES duration:5.0];
        [sprite runAction:[SKAction repeatActionForever:moveAction]];
        // HERE ENDS THE MODIFICATION

        [self addChild:sprite];
    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

希望这有帮助

请检查我的答案-或者我也可以上传到github。。。