0

我正在使用 flappy bird 演示尝试不同的事情,只是为了“了解彼此”。通过演示,我设法将游戏的方向更改为垂直向上移动。将 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 屏幕截图。

在此处输入图像描述

4

1 回答 1

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)) {

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

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

于 2014-05-10T23:36:26.883 回答