2014-05-10 2 views
0

나는 "서로를 알아"가기 위해 다른 것들을 시도하는 flappy bird 데모로 작업 중입니다. 데모를 통해 게임의 방향을 위쪽으로 이동하는 수직 스크롤로 변경했습니다. CGFloat를 음수 값으로 바꾸면 장애물이 위쪽으로 이동하지만 범위를 벗어나면 다시 산란하지 않습니다. 아래쪽 스크롤의 값을 변경하면 업데이트 방법에 따라 다시 생성됩니다. 누군가가 x에서 y로 잘못 변환 한 것을 설명 할 수 있습니까? 하단이 인식되고 화면 상단이 보이지 않는 이유는 무엇입니까? 미리 감사cocos2d flappy bird 데모

#import "MainScene.h" 

static const CGFloat scrollSpeed = -280.f; //upwards 
static const CGFloat firstObstaclePosition = -568.f; 
static const CGFloat distanceBetweenObstacles = 80; 

@implementation MainScene { 
CCSprite *_hero; 
CCPhysicsNode *_physicsNode; 
NSMutableArray *_obstacles; 
} 

- (void)spawnNewObstacle { 
CCNode *previousObstacle = [_obstacles lastObject]; 
CGFloat previousObstacleYPosition = previousObstacle.position.y; 
if (!previousObstacle) { 
    // this is the first obstacle 
    previousObstacleYPosition = firstObstaclePosition; 
} 
CCNode *obstacle = [CCBReader load:@"Obstacle"]; 
obstacle.position = ccp(0, previousObstacleYPosition + distanceBetweenObstacles); 
[_physicsNode addChild:obstacle]; 
[_obstacles addObject:obstacle]; 
} 
- (void)update:(CCTime)delta { 
_hero.position = ccp(_hero.position.x, _hero.position.y + delta * scrollSpeed);//move on Y axis 
_physicsNode.position = ccp(_physicsNode.position.x, _physicsNode.position.y - (scrollSpeed *delta));//scroll in Y axis 
//spawn more 
NSMutableArray *offScreenObstacles = nil; 
for (CCNode *obstacle in _obstacles) { 
    CGPoint obstacleWorldPosition = [_physicsNode convertToWorldSpace:obstacle.position]; 
    CGPoint obstacleScreenPosition = [self convertToNodeSpace:obstacleWorldPosition]; 
    if (obstacleScreenPosition.y < -obstacle.contentSize.height) { 
     if (!offScreenObstacles) { 
      offScreenObstacles = [NSMutableArray array]; 
     } 
     [offScreenObstacles addObject:obstacle]; 
    } 
} 
for (CCNode *obstacleToRemove in offScreenObstacles) { 
    [obstacleToRemove removeFromParent]; 
    [_obstacles removeObject:obstacleToRemove]; 
    // for each removed obstacle, add a new one 
    [self spawnNewObstacle]; 
} 
} 

- (void)didLoadFromCCB { 
self.userInteractionEnabled = TRUE; 
_obstacles = [NSMutableArray array]; 
[self spawnNewObstacle]; 
[self spawnNewObstacle]; 
[self spawnNewObstacle]; 
} 

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 

} 
@end 

에 나는 SB에서 _physicsNode 스크린 샷을 첨부했습니다.

enter image description here

답변

0

그것은 그들이 짧은 일정 높이있는 경우 장애물이 잘 산란되고, 그들 사이의 거리 값이 충분히 큰 것 같습니다. 거리 변수의 의미있는 가치를 얻으려면 장애물 높이를 통합하는 것이 좋습니다. 그냥 생각.

라인이 -

obstacle.position = ccp(0, previousObstacleYPosition + distanceBetweenObstacles); 

될 수 - 아래가 아닌 작업 수직 스크롤의 문제에 관해서는

obstacle.position = ccp(0, previousObstacleYPosition + distanceBetweenObstacles + previousObstacle.contentSize.height); 

위쪽으로 내가이 줄 예정이다 믿습니다

if (obstacleScreenPosition.y < -obstacle.contentSize.height) { 

이 줄은 장애물이 화면에서 벗어난 때를 결정하는 책임이 있기 때문에 ef 다음 장애물의 산란에 fect. 이 줄이 아래로 스크롤하는 데는 효과가 있지만 위쪽으로 스크롤하려면이 줄을 변경해야하는 이유가 있습니다.

시도 :

if (obstacleScreenPosition.y > (_physicsNode.contentSize.height + obstacle.contentSize.height)) { 

당신은 나가 고정되는 위치에 따라 장애물의 크기를 필요로하지 않을 수있다.

희망이 있습니다. 행운을 빈다.

+0

안녕하세요, Matt 님, 답장을 보내 주셔서 감사합니다. 나는 당신의 변화를 시험해 보았으나 이제는 스크롤을 통해 반쯤 장애물이 생겨났다. 정말 혼란 스럽네. – user2800989

+0

나는 또한 시도했다 if (obstacleScreenPosition.y> _physicsNode.contentSize.height) { 같은 결과. 흠 ... 내 물리 노드 치수가 틀립니다. 나는 혀를 따라 갔다. – user2800989

+0

이상한 점은 스크린 위치가 아래쪽에 있기 때문에 아래쪽 스크롤이 효과가 있다고 생각했습니다. 장애물 스크린의 위치가 장애물 음의 너비보다 작 으면 제거하고 다른 것을 스폰해야합니다. 그래서 장애물 위치가 화면의 높이보다 크면 제거해야한다고 생각했고 아래쪽 경우와 같이 새 것을 스폰합니다. 코드 조각이 작동해야하는 화면의 높이를 알 수 있다면. 나는 튜토리얼에서 물리 노드가 백분율을 기반으로한다고 생각했다. 그렇다면 contentSize.height를 사용하여 확인할 수 있었다. – mattk