方法不能正确生成精灵

时间:2014-11-20 05:22:55

标签: ios sprite-kit skspritenode skphysicsbody skaction

现在我在屏幕顶部有五个砖块,它们位于相同的y位置并且间隔相等。我想在场景顶部产生五个以上的休息时间,并且每5秒向下滑动一次。当新砖块向下滑动时,我希望以前的砖块向下滑动以便为新砖块腾出空间。我希望这个序列永远继续下去。我希望新砖块能够获得旧砖块的相同属性,例如当球击中它们的反应时。

使用我输入的代码完成此操作,我得到一个砖生成然后向下滑动。此外,新砖的行为与旧砖不同,它不像物理世界的一部分。如何获得5个砖块,然后向下滑动并应用与其他砖块相同的属性。

这段代码是我为生成更多砖块而添加的代码:

-(void) spawnMoreBricks:(CGSize)size {
    for (int i = 0; i < 5; i++) {
        SKSpriteNode *brick = [SKSpriteNode spriteNodeWithImageNamed:@"brick"];
        //resize bricks
        brick.size = CGSizeMake(60, 30);
        //position it
        brick.position = CGPointMake(self.size.width/2, self.size.height);

        SKAction *wait = [SKAction waitForDuration:5];
        SKAction *spawn = [SKAction runBlock:^{[self addChild: brick];
            SKAction *move = [SKAction moveByX:0 y:-80 duration:3];
            [brick runAction: move];
        }];

        SKAction *spawnSequence = [SKAction sequence:@[wait, spawn]];
        SKAction *repeatSequence = [SKAction repeatActionForever:spawnSequence];

        [self runAction:repeatSequence];
    }
}

此代码在初始化时将砖块添加到场景中,它们会对球与它们碰撞做出反应以及所有内容:

- (void) addBricks:(CGSize)size {
    for (int i = 0; i < 5; i++) {
        //create brick sprite from image
        SKSpriteNode *brick = [SKSpriteNode spriteNodeWithImageNamed:@"brick"];
        //resize bricks
        brick.size = CGSizeMake(60, 30);
        //psoition bricks
        int xPos = size.width/5 * (i+.5);
        int yPos = size.height - 25;
        brick.position = CGPointMake(xPos, yPos);
        brick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:brick.frame.size];
        brick.physicsBody.dynamic = NO;
        brick.physicsBody.categoryBitMask = brickCategory;

        //add bricks to scene
        [self addChild:brick];
    }
}

0 个答案:

没有答案