如何使用sprite工具包创建路径

时间:2014-02-27 23:13:09

标签: ios xcode sprite-kit cgpath

我刚开始使用的应用程序非常棒;我用cocos2d构建它,有很多资源可以帮助我。我决定把它带到精灵套件,因为未来苹果集成,但我无法解决问题。我试图让游戏几乎完全按程序生成,而我正在努力的主要事情之一是获得动态生成的路径。我设置代码来制作随机点并按行连接它们,但我想知道如何制作平滑曲线并在运行中生成它们。 iDevices处于纵向模式,曲线正在向上移动。

以下是代码:

- (void) generatePath {

 //I make the points in advance which works perfect  




float minDY = 160;
float minDX = 60;
int rangeDY = 80;
int rangeDX = screenWidth;

float x =  (screenWidth * .75 );
float y = - screenHeight;

float dx, nx;
float sign = 1; // +1 - going up, -1 - going  down
float paddingLeft = screenWidth/4;
float paddingRight = 0;


for (int i=0; i<kMaxPathKeyPoints; i++) {
    _pathKeyPoints[i] = CGPointMake(x, y);
    if (i == 0) {
        x = screenWidth * .75;
        y = screenHeight * .75;
    } else {
        y += rand()%rangeDY+minDY;
        while(true) {
            dx = rand()%rangeDX+minDX;
            nx = x + dx*sign;
            if(nx < screenWidth-paddingRight && nx > paddingLeft) {
                break;
            }
        }
        x = nx;
    }
    sign *= -1;
}
}




- (void) draw {


//this is where my trouble is, I don't know how to draw the points as the are needed and make them smooth curves; I also need to identical lines for each side of the path, but I can figure that out later.

    yourline = [SKShapeNode node];
    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, screenWidth * .75, screenHeight * .75);





    for(int i = 0; i <= kMaxPathKeyPoints-2; ++i) {
        CGPathAddLineToPoint(pathToDraw, NULL, _pathKeyPoints[i].x, - _pathKeyPoints[i+1].y + screenHeight);
           }
    yourline.path = pathToDraw;
    [yourline setStrokeColor:[UIColor redColor]];
    [self addChild:yourline];
 //[self addChild:yourline];

}

如果有人可以帮助我,这对我来说意味着世界!

0 个答案:

没有答案