如何以随机高度生成CCSprites,如flappy bird

时间:2014-02-19 01:01:35

标签: ios objective-c arrays cocos2d-iphone flappy-bird-clone

在iOS游戏飞扬的鸟类中,有一些管道在一定距离后产生并且随机产生高度

This is a fake image, but the logic is the same

我也在尝试制作飘逸的鸟管(我把它称为我的代码中的树枝而不是管道)。除了管道垂直移动而不是水平移动,因为它是一个垂直滚动游戏(它像游戏doodle jump一样滚动)

这是我想要的图片:https://docs.google.com/drawings/d/18bxsVsNOlScCvgi1mwuzD2At7R6xKM3QCh6BfAVMuMo/edit?usp=sharing (水平线是分支)

所以这就是我到目前为止做的垂直分支(或管道)......

.h

CCSprite *branch;
NSMutableArray *_branches;
CCSprite *obstacle;
CCNode *previousBranch;
CGFloat previousBranchYPosition;

.m

@implementation HelloWorldLayer 

static const CGFloat firstBranchPosition = 426.f;
static const CGFloat distanceBetweenBranches = 140.f;
#define ARC4RANDOM_MAX      0x100000000
static const CGFloat minimumXPositionRightBranch = 280.f;
static const CGFloat maximumXPositionLeftBranch = 50.f;
static const CGFloat pipeDistance = 100.f;
static const CGFloat maximumXPositionRightBranch = maximumXPositionLeftBranch - pipeDistance;

setBranchInitialPosition方法

    /* This is where I am setting the initial position of the branches. 
So I am specifying the position of the first branch and the other branches after it so it gets placed every time a certain distance is passed. I have a left branch and a right branch*/
     -(void) setBranchInitialPosition {
        CGFloat random = ((double)arc4random() / ARC4RANDOM_MAX);
            CGFloat range = maximumXPositionRightBranch - minimumXPositionRightBranch;

            _rightBranch.position = ccp(minimumXPositionRightBranch + (random * range), _rightBranch.position.y);
            _leftBranch.position = ccp(_rightBranch.position.x + pipeDistance, _leftBranch.position.y);
        }

spawnNewBranches方法

// This is how I want the branches to spawn and I want to add them to an array full of branches
   - (void)spawnNewBranches {
        previousBranch = [_branches lastObject];
        previousBranchYPosition = previousBranch.position.y;

        if (!previousBranch) {
            // this is the first obstacle
            previousBranchYPosition = firstBranchPosition;
        }

        _rightBranch = [CCSprite spriteWithFile:@"branch.png"];
        _leftBranch = [CCSprite spriteWithFile:@"branch.png"];
        [_leftBranch addChild:_rightBranch];
        [self setBranchInitialPosition];

        obstacle = [CCSprite node];
        [obstacle addChild:_leftBranch];


        obstacle.position = ccp(160, previousBranchYPosition + distanceBetweenBranches);
        [self addChild:obstacle];
        [_branches addObject:obstacle];
    }

scroll方法

-(void) scroll:(ccTime)dt
{
    // moves the bg
    background.position = ccp(screenCenter.x, background.position.y + [[NSUserDefaults standardUserDefaults] integerForKey:@"scrollSpeed"]*dt);
    bg2.position = ccp(screenCenter.x, background.position.y-background.contentSize.height);

    // it adds the new bg's to the screen before the old bg's move off the screen
    if (background.position.y >= screenSize.height*1.5)
    {
        background.position = ccp(screenCenter.x, (screenCenter.y)-(background.size.height/2));
    } else if (bg2.position.y >= screenSize.height*1.5) {
        bg2.position = ccp(screenCenter.x, (screenCenter.y)-(bg2.size.height/2));
    }

        // This is where I want them to appear every certain distance and also move with the brackground
    obstacle.position = ccp(obstacle.position.x, obstacle.position.y*[[NSUserDefaults standardUserDefaults] integerForKey:@"scrollSpeed"]*dt);

    NSMutableArray *offScreenObstacles = nil;
    if (obstacle.position.y >= screenSize.height*1.5) {
        [offScreenObstacles addObject:obstacle];
    }

    for (CCNode *obstacleToRemove in offScreenObstacles) {
        [obstacleToRemove removeFromParent];
        [_branches removeObject:obstacleToRemove];
        // for each removed obstacle, add a new one
        [self spawnNewBranches];
    }

}

现在,树枝正在出现,但是它们留在左下角,它们根本不会移动或产生。我想让它们随背景移动并在一定距离后产生,同时也在随机高度生成。我向你提供了我的所有代码,你知道我怎么做这个工作吗?提前谢谢!

2 个答案:

答案 0 :(得分:1)

您可能希望尝试根据正弦或余弦(https://en.wikipedia.org/wiki/Trigonometric_functions)等三角曲线放置管道。看起来您将管道放置在一个相当定义的随机范围内,但如果您将此范围更改为与三角曲线图的偏移量,则会考虑到玩家更好地在开放间隙之间转换的能力。至少那是我的感觉。我认为代码会更容易理解,而且我有点困惑。您还可以通过更改参数(如增加幅度或频率)轻松改变曲线的难度。

答案 1 :(得分:1)

我为了好玩而创建了Flappy Bird的副本。我用这段代码创建了管道:

-(void)createPipes{

    //Create Random
    int from = 65;
    int max = [[UIScreen mainScreen] bounds].size.height - 124;
    int delta = max - from - dy;
    int y = from + arc4random() % (delta - from);

    //Pipe Bottom
    UIImageView *pipeBottom = [[UIImageView alloc] init];
    [pipeBottom setContentMode:UIViewContentModeTop];
    [pipeBottom setImage:[UIImage imageNamed:@"pipeBottom"]];
    [pipeBottom setFrame:CGRectMake(320, y+dy, 60, max - y - dy)];
    [pipeBottom setClipsToBounds:YES];

    //Pipe Top
    UIImageView *pipeTop = [[UIImageView alloc] init];
    [pipeTop setFrame:CGRectMake(320, 0, 60, y)];
    [pipeTop setContentMode:UIViewContentModeBottom];
    [pipeTop setImage:[UIImage imageNamed:@"pipeTop"]];

    [self.view insertSubview:pipeTop atIndex:1];
    [self.view insertSubview:pipeBottom atIndex:1];

    if (!self.pipes)
        self.pipes = [[NSMutableArray alloc] init];

    [self.pipes addObject:pipeBottom];
    [self.pipes addObject:pipeTop];

}

并移动它们:

-(void)moveArray:(NSMutableArray *)array{
    float ds = dv * dt;
    NSMutableArray *trash = [NSMutableArray array];
    for (UIImageView *obj in array) {
        CGRect frame = obj.frame;
        frame.origin.x -= ds;
        if (frame.origin.x < -frame.size.width) {
            [obj removeFromSuperview];
            [trash addObject:obj];
        }else{
            obj.frame = frame;
        }
    }
    [array removeObjectsInArray:trash];
}
-(void)movePipes{
    [self moveArray:self.pipes];
}

我每0.01秒调用一次这个函数来运行游戏:

-(void)runGame{
    _time += dt;
    if (_time >= 180.0/dv) {
        _time = 0;
        [self createPipes];
    }
    [self movePipes];
    [self moveEnemies];
    [self moveYoshi];
    [self moveBar];
    [self verifyScore];
    [self verifyCollision];
    [self verifyState];
}

我定义了dt = 0.01和dv = 110.

你可以在youtube中看到我的模仿:(http://www.youtube.com/watch?v=tTcYdpSIKJg

我希望这对你有所帮助。

Best,Rafael Castro。