如何生成彼此之间有一定距离的平台

时间:2014-04-23 18:15:13

标签: objective-c ios7 sprite-kit

对不起我的英语;)我正在使用 SpriteKit 进行一场无限级别的比赛(比如涂鸦跳)我有一个问题:我无法理解生成一个平台,以便在距离前一个平台一定距离处生成新平台。

以下是生成平台的示例代码:

- (void)addPlatforms
{
    int random = arc4random() % (int)(self.frame.size.width-80)+100;

    // right end platform
    SKSpriteNode *rightEndPlatform = [SKSpriteNode spriteNodeWithImageNamed:@"leftPlatform"];
    rightEndPlatform.size = CGSizeMake(16, 16);
    rightEndPlatform.physicsBody =
    [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(rightEndPlatform.size.width, rightEndPlatform.size.height - 5)];
    rightEndPlatform.position = CGPointMake(random, 0);

    // left end platform
    SKSpriteNode *leftEndPlatform = [SKSpriteNode spriteNodeWithImageNamed:@"rightPlatform"];
    leftEndPlatform.size = CGSizeMake(16, 16);
    leftEndPlatform.physicsBody =
    [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(leftEndPlatform.size.width, leftEndPlatform.size.height - 5)];
    leftEndPlatform.position = CGPointMake(rightEndPlatform.position.x - PLATFORM_RANGE, rightEndPlatform.position.y);

    // left platform
    SKSpriteNode *leftPlatform = [SKSpriteNode spriteNodeWithImageNamed:@"centerPlatform"];
    leftPlatform.size = CGSizeMake(leftEndPlatform.position.x, 16);
    leftPlatform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:leftPlatform.frame.size];
    leftPlatform.position =
    CGPointMake(leftEndPlatform.position.x - leftPlatform.size.width/2,
                leftEndPlatform.position.y);

    // right platform
    SKSpriteNode *rightPlatform = [SKSpriteNode spriteNodeWithImageNamed:@"centerPlatform"];
    rightPlatform.size = CGSizeMake(self.frame.size.width+rightEndPlatform.position.x, 16);
    rightPlatform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rightPlatform.frame.size];
    rightPlatform.position =
    CGPointMake(rightEndPlatform.position.x + rightPlatform.size.width/2,
                rightEndPlatform.position.y);

    // add to the scene
    [self addChild:rightEndPlatform];
    [self addChild:leftEndPlatform];
    [self addChild:leftPlatform];
    [self addChild:rightPlatform];

    // add platforms to array
    [platformArray addObject:rightEndPlatform];
    [platformArray addObject:leftEndPlatform];
    [platformArray addObject:leftPlatform];
    [platformArray addObject:rightPlatform];

    for (int i = 0; i < [platformArray count]; i++)
    {
        SKSpriteNode *sprite = [platformArray objectAtIndex:i];
        sprite.physicsBody.usesPreciseCollisionDetection = YES;
        sprite.physicsBody.dynamic = NO;
        sprite.name = [NSString stringWithFormat:@"beforePlayer"];
    }
}

然后移动它:

-(void)update:(CFTimeInterval)currentTime
{
    for (int i = 0; i < [platformArray count]; i++)
    {
        SKSpriteNode *platform = [platformArray objectAtIndex:i];
        platform.position = CGPointMake(platform.position.x, platform.position.y + PLATFORM_SPEED);
        if (platform.position.y > self.frame.size.height+100)
        {
            [platform removeFromParent];
        }
    }
}

按时间间隔生成的平台:

timer = [NSTimer scheduledTimerWithTimeInterval:TIME_INTERVAL target:self selector:@selector(addPlatforms) userInfo:nil repeats:YES];

我需要生成彼此之间有一定距离的平台(当第一个平台生成并向上移动100px时 - 然后生成第二个平台......等等)

1 个答案:

答案 0 :(得分:1)

保留一个变量,其中包含有关&#34;远远超过&#34;用户已经移动了。由于您的平台不断向下移动并且具有固定的速度,因此只需要变量_screenAltitude + = PLATFORM_SPEED; - 然后当if(_screenAltitude%distance_between_platforms == 0)addPlatForm();

所以回顾一下:跟踪当前的屏幕高度。当该模数平台之间的距离为零时,则添加新平台。只需确保平台速度是平台距离的干净除数:) - 或模数永远不会为零。

我不会使用计时器生成平台:)