1

我是 sprite kit 的新手,为了提高我的技能,我正在创建一个类似于流行的 Flappy Bird 的游戏。我创建了一种方法,可以将管道添加到场景中作为障碍物。我使用 bodyWithRectangleOfSize 为这些障碍物创建了静态物理体,并将其设置为 barrier.size。然而,物理实体的大小与屏幕上显示的管道图像不同。屏幕上显示为空白的地方,鸟正在撞上。这只鸟也可以飞过管道的各个部分。这似乎是由物理体引起的,包括障碍物在尺寸中的位置。(例如,管道漂浮在屏幕底部上方 320 像素处,高度为 240 像素。物理体正在拉伸到屏幕的整个长度。)

更新:这不再是问题。我现在发现底部的物理体又短又胖,而顶部的物理体太长。间隙在那里,但它位于显示管道的中间。

正如我之前所说。我是 spritekit 的新手,这很可能是我的一个基本错误。任何帮助表示赞赏。谢谢你。

这是我生成每个障碍物的代码:

-(void)initialiseObstacles:(int)position
{

//create random height

int random = (arc4random()%250) + 150;



    //add bottom pipe and physics body

    SKSpriteNode *lowObstacle = [SKSpriteNode     spriteNodeWithImageNamed:@"pipe.png"];
    lowObstacle.size = CGSizeMake(50, random);
    lowObstacle.position = CGPointMake(position + 200, 0);
    lowObstacle.anchorPoint = CGPointMake(0,0);
    lowObstacle.name = @"lowObstacle";

    [self addChild:lowObstacle];

    lowObstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:lowObstacle.frame.size];
    lowObstacle.physicsBody.dynamic = NO;
    lowObstacle.physicsBody.categoryBitMask = obstacleCategory;

    //add top pipe & physics body

    SKSpriteNode *highObstacle = [SKSpriteNode spriteNodeWithImageNamed:@"pipetop.png"];
    highObstacle.anchorPoint = CGPointMake(0,0);
    highObstacle.size = CGSizeMake(50, 430-lowObstacle.size.height);
    highObstacle.position = CGPointMake(lowObstacle.position.x,lowObstacle.size.height + 130);
    highObstacle.name = @"highObstacle";

    [self addChild:highObstacle];

    highObstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:highObstacle.frame.size];
    highObstacle.physicsBody.dynamic = NO;
    highObstacle.physicsBody.categoryBitMask = obstacleCategory;

}

此代码每 100 帧生成一个管道,并由更新调用:

-(void)spawn{


frames = frames + 1;

if (frames == 100) {

    [self initialiseObstacles:200];
    frames = 0;
    }
}

此代码移动管道:

-(void)moveObstacles
{
[self enumerateChildNodesWithName:@"lowObstacle" usingBlock:^(SKNode *node, BOOL *stop) {
SKSpriteNode *ob = (SKSpriteNode *)node;
ob.position = CGPointMake(ob.position.x - 1.5, 0);


if (ob.position.x < -ob.size.width) {
    [ob removeFromParent];
}
}];

[self enumerateChildNodesWithName:@"highObstacle" usingBlock:^(SKNode     *node, BOOL *stop) {
    SKSpriteNode *highOb = (SKSpriteNode *)node;
    highOb.position = CGPointMake(highOb.position.x - 1.5, highOb.position.y);

    if (highOb.position.x < - highOb.size.width) {
        [highOb removeFromParent];

    }
}];
}
4

1 回答 1

0

使用图像初始化时,应单独保留 lowobstacle.size。此外,当您设置物理体时

bodywithrectangleofsize: lowobstacle.frame.size

应该是 bodywithrectangleofsize: lowobstacle.size

我设置锚点的经验并不好。它似乎摆脱了物理体。祝你好运!

于 2014-02-16T07:37:17.443 回答