2014-02-19 30 views
2

在的iOS遊戲飛揚的鳥,也有一定的距離後產生的管道和它們所產生的隨機高度如何產卵隨機高度像笨鳥先飛CCSprites

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

你可能想看到這個:[Apple拒絕Flappy Bird knockoffs](http://www.tuaw.com/2014/02/17/apple-rejecting-flappy-bird-knockoffs-and-other-news- for-feb-1 /) – rmaddy

+9

我是一個14歲的孩子,想成爲一個更好的Objective-C編碼器。不是爲了讓飛鳥成功而賺錢。 –

回答

1

您可能想嘗試基於三角曲線(如正弦或餘弦)來放置管道(https://en.wikipedia.org/wiki/Trigonometric_functions)。看起來你正在將管道放置在相當定義的隨機範圍內,儘管如果將此範圍更改爲三角曲線圖的偏移量,它將考慮到玩家更好地在開放間隙之間轉換的能力。至少這是我的感覺。我認爲代碼會更容易遵循,我也有點困惑。您還可以通過更改參數(如增加幅度或頻率)輕鬆改變曲線的難度。

+0

投票?你不喜歡我的想法?噓 - 甕。 –

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的模仿:(​​)

我希望這可以幫助您。

Best,Rafael Castro。

+0

什麼是'dy'定義爲? –

+0

這是頂部管道和底部管道之間的空間。我在我的代碼中定義了dy 105,但是你可以選擇你想要的任何值 – rcmcastro