Fork me on GitHub

(问题)求cocos2d上的bezier曲线的细节

http://www.cocoachina.com/bbs/read.php?tid=84995

需求:一个打飞机游戏,要求飞机在屏幕外由左至右或者由右至左飞过,但其飞行的路线应该是多样的、随机的,我采用了bezier曲线,方法是在左右屏幕的x 轴上随机的插入n个点,然后依次用这些点作为一个个bezier曲线的终点,生成n+1条bezier曲线(用ccbezierto动作实现),然后把这些动作放在一个动作序列中,再运行这个动作序列。但是出现明显的问题是:各个动作动作完后(一段bezier曲线)有明显的停顿现象,不只是因为曲线不圆滑,就算是圆滑,各个动作之间估计也有停顿现象。现在想就些请教各位两个问题:
1、cocos2d中的,有没有办法一次性地设置n次bezier曲线,好像只能设置3个点,这样的话,曲线变化不多,路线不生动;
2、如果采取变种的办法,人为的分成n个曲线动作,放在动作序列中,再运行动作序列,怎样才能让各个动作之间没有停顿的现象。

 

//startPos----varStartAndEnd-----.-----.----.--.------.----varStartAndEnd----destPos
-(CCSequence*)makeBeizPoses:(CGPoint)startPos destPos:(CGPoint)destPos routeCount:(int)routeCount varStartAndEnd:(int)varStartAndEnd{
    float beiZposesX_int[FLY_ROUTE_MAX] = {0};
    float beiZposesY_int[FLY_ROUTE_MAX] = {0};
//NSArray* beiZposesX,*beiZposesY;
    NSArray* beiZposesX = [[NSArray alloc]init];
    NSArray* beiZposesY = [[NSArray alloc]init];
    int plugPoint = routeCount - 1;
    float startP,endP;
    startP = startPos.x + varStartAndEnd;
    endP = destPos.x - varStartAndEnd;
    if(plugPoint >= 1)
    {
        beiZposesX = [self pickUniqueNumbers:plugPoint min:startP max:endP];
        beiZposesY = [self pickUniqueNumbers:plugPoint min:FLY_SCAPE_LOW max:FLY_SCAPE_TOP];
    }

    float changeTemp = 0;

//SET beiZposesX_int
    beiZposesX_int[0] = startPos.x;
    beiZposesX_int[routeCount] = destPos.x;
    beiZposesY_int[0] = startPos.y;
    beiZposesY_int[routeCount] = destPos.y;

    for (int i = 0; i < [beiZposesX count]; i ++) {
        beiZposesX_int[i+1] = [[beiZposesX objectAtIndex:i]floatValue];
    }
//SET beiZposesY_int
    int coutY = [beiZposesY count];
NSLog(@"coutY is:%d",coutY);
    for (int i = 0; i < [beiZposesY count]; i ++) {
        beiZposesY_int[i+1] = [[beiZposesY objectAtIndex:i]floatValue];
    }
    if(startPos.x < destPos.x)
    {
        for (int i = 0; i < routeCount; i ++) 
        {
            for (int j = 0; (j+1) <= (routeCount - i); j ++) 
                if (beiZposesX_int[j + 1] < beiZposesX_int[j] )
                {
                    changeTemp = beiZposesX_int[j + 1];
                    beiZposesX_int[j + 1] = beiZposesX_int[j];
                    beiZposesX_int[j] = changeTemp;
                }
        }
    }
    else
    {
        for (int i = 0; i < routeCount; i ++) 
        {
            for (int j = 0; (j+1) <= (routeCount - i); j ++) 
                if (beiZposesX_int[j + 1] > beiZposesX_int[j] )
                {
                    changeTemp = beiZposesX_int[j + 1];
                    beiZposesX_int[j + 1] = beiZposesX_int[j];
                    beiZposesX_int[j] = changeTemp;
                }

        }
    }

    CGPoint beizPoses[routeCount + 1];
    for(int index = 0;index <= routeCount;index++)
        beizPoses[index] = CGPointMake(beiZposesX_int[index], beiZposesY_int[index]);

    float useTime;
    id actionCallFunc = [CCCallFunc actionWithTarget:self selector:@selector(disAppear)];
    CCSequence* seqFly;
NSMutableArray* actionArray = [NSMutableArray arrayWithCapacity:routeCount];
ccBezierConfig bezier;
    CCBezierTo *flymove;
    for(int i = 0;i < routeCount;i ++)
    {
        bezier.controlPoint_1 = beizPoses;//start point
        bezier.endPosition = beizPoses[i + 1];//end ponit
        bezier.controlPoint_2 = [self makeBezCtrlPos:bezier.controlPoint_1 endPos:bezier.endPosition]; //control ponit

        useTime = [self makeFlyTime:bezier.controlPoint_1   endPos:bezier.endPosition]; 
        flymove = [CCBezierTo actionWithDuration:useTime bezier:bezier];
        [actionArray addObject:flymove];

    }
    [actionArray addObject:actionCallFunc];
    seqFly = [CCSequence actionsWithArray:actionArray];
//[beiZposesX release];
//[beiZposesY release];
    return seqFly;
}





代码如下:

posted on 2012-04-09 10:06  pengyingh  阅读(1096)  评论(1)    收藏  举报

导航