2

In my iOS game, I am trying to move an object along a path (see below):

enter image description here

I want to move the triangle above around to the bottom of the box.

But I don't know how to use CGPathRefs very well. Here is what I am trying:

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, 20, 0, 20, 0, M_PI/2, NO);
SKAction *moveTriangle = [SKAction followPath:path asOffset:YES orientToPath:YES duration:1.0];
[self.triangle runAction:moveTriangle];

And I keep getting odd results. Can anybody help me adjust my path to match the image above? Thanks!

4

1 回答 1

2

使用原型的想法

     // will arc around this sprite for demo
    SKSpriteNode *boxOrange = [SKSpriteNode spriteNodeWithColor:[UIColor orangeColor] size:CGSizeMake(30, 30)];
    boxOrange.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    [self addChild:boxOrange];

    SKSpriteNode *heroSprite = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(70, 70)];
    CGFloat offeset = 150; // change as required, ie bring hero closer to rotn point
    heroSprite.position = CGPointMake(-offeset, heroSprite.position.y);
    SKSpriteNode *guideContainer = [SKSpriteNode spriteNodeWithColor:[SKColor purpleColor] size:CGSizeMake(3, 3)];
    // for full effect - change guide container to clearColor
    // otherwise good for seeing where rotation point will occur
    [guideContainer addChild:heroSprite];

    [self addChild:guideContainer];
    guideContainer.position = boxOrange.position;

    [guideContainer runAction:[SKAction rotateToAngle:1.57 duration:3.0 shortestUnitArc:YES]];
    // just found shortestUnitArc action .. this will be handy for at times
于 2013-10-23T22:44:47.000 回答