cocos2d flappy bird demo

时间:2014-05-10 22:01:54

标签: cocos2d-iphone spritebuilder flappy-bird-clone

我正在使用飞扬的小鸟演示尝试不同的事情只是为了“彼此了解”。 通过演示,我设法将游戏的方向改为垂直滚动向上移动。 将CGFloat反转为负值会使我的障碍向上移动,但一旦它们超出界限,它们就不会重新产生。 如果我更改向下滚动的值,则会根据更新方法重新生成。 有人可以向我解释我在x到y转换时遇到了什么问题吗?为什么底部被识别并且屏幕顶部没有? 提前致谢

#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

1 个答案:

答案 0 :(得分:0)

如果它们是一个短而恒定的高度,并且它们之间的距离值足够大,看起来你的障碍将会产生很好的效果。结合障碍物的高度以获得更有意义的距离变量值可能更好。只是一个想法。

行 -

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

可能是 -

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

至于垂直滚动向下工作而不向上工作的问题我相信这是由于这条线:

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

由于该线负责确定障碍物何时离开屏幕,因此它对下一个障碍物的产生有影响。这条线适用于向下滚动但需要更改以进行向上滚动是有道理的。

尝试:

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

您可能需要或不需要障碍物的大小,具体取决于它的锚定位置。

我希望这有效,祝你好运。