2014-02-19 3 views
2

, 일정 거리 이후에 생성 파이프가 플래 피 버드와 같은 임의의 높이에서 CCSprites를 생성하고 내가 또한 만들려고 노력하고 임의의 높이어떻게 아이폰 OS 게임 플래 피 조류

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

에서 생성 flappy 새 파이프 (나는 파이프 대신 코드로 트리 분기라고 부름). https://docs.google.com/drawings/d/18bxsVsNOlScCvgi1mwuzD2At7R6xKM3QCh6BfAVMuMo/edit?usp=sharing (수평 라인이 있습니다 : 그것은 수직 스크롤 게임이기 때문에 파이프가 수직 대신 수평으로의 이동을 제외하고 이것은 내가 원하는 무엇 도면이다

(이 게임 doodle jump처럼 스크롤) 지점)

그래서 이것이 내가 수직 지점 (또는 파이프)를 만들기 위해 지금까지하려고 노력 무슨입니다 ... 내 .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)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]; 
} 

내가 정의 :

-(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 초를 호출 dt = 0.01 및 dv = 110.나는이 도움을 바랍니다 (http://www.youtube.com/watch?v=tTcYdpSIKJg)

:

당신은 유튜브 내 패러디를 볼 수 있습니다.

최고, Rafael Castro.

+0

'dy'는 무엇으로 정의되어 있습니까? –

+0

상단 파이프와 하단 파이프 사이의 공간입니다. 내 코드에 105를 정의했지만 원하는 값을 선택할 수 있습니다. – rcmcastro