3

iOS游戏flappy bird中,有一定距离后生成的管道,它们在随机高度生成

这是一张假图,但逻辑是一样的

我也在尝试制作飞扬的鸟管(我在我的代码中称它为树枝而不是管)。除了管道是垂直移动而不是水平移动,因为它是一个垂直滚动游戏(它像游戏涂鸦跳跃一样滚动)

这是我想要的图: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];
    }

}

现在,树枝出现了,但它们停留在左下角,根本不动,也不生成。我想让它们随着背景移动并在一定距离后生成,同时也在随机高度生成。我为你提供了我所有的代码,你知道我怎样才能做到这一点吗?提前致谢!

4

2 回答 2

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

我希望这对你有帮助。

最好的,拉斐尔·卡斯特罗。

于 2014-02-25T19:15:14.833 回答
1

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

于 2014-02-20T17:06:01.803 回答