2014-12-25 53 views
2

我的精灵位置是(screenWidth,screenHeigh/2)。我尝试了很多使用CGPathAddArc将精灵从右向左移动但不成功的代码。苹果文档很难理解。所以我在这里发布希望有人可以帮助我。 这是我用过的代码之一。如何使用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)]; 

提前

+0

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

回答

1

如果您创建一个空白SpriteKit项目很多的感谢,对GameScene.m此略作修改默认代码会使飞船从右侧飞过向左下了半圈路径。

请注明,如果您需要进一步澄清!

与您的代码段的主要区别在于[SKAction followPath...]asOffset参数为NO,而orientToPath为YES。

#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 

希望这会有所帮助!

相关问题