对于Loop Freeze Obj-C

时间:2015-04-03 22:47:28

标签: objective-c skscene

所以这个循环在skscene中运行。它在我的多人游戏中完美运行,但是..在单人游戏模式下,它的工作效果不是很好......

问题是,它意味着迭代执行快速动画的循环。但是,整个过程只是在循环结束时冻结并动画化。

for (int i = 0; i < 7; i++) {
    //to loop between players
    for (int j = 0; j < 4; j++) {

        NSString *imageName = [NSString stringWithFormat:@"back"];
        NSString *bundle = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
        SKTexture *texture = [SKTexture textureWithImage:image];
        cardDisplay = [CardSpriteNode spriteNodeWithTexture:texture];
        cardDisplay.size = CGSizeMake(104, 144);
        cardDisplay.position = _dealPosition;
        cardDisplay.zPosition = zPosCount;
        zPosCount++;
        cardDisplay.userInteractionEnabled = YES;
        cardDisplay.name = @"cardBack";
        [self addChild:cardDisplay];


        CGPoint moveToPosition;
        //change position to deal to
        if (j == 0) {
            moveToPosition = position1;
        }
        else if (j == 1) {
            moveToPosition = position2;
        }
        else if (j == 2) {
            moveToPosition = position3;
        }
        else if (j == 3) {
            moveToPosition = position4;
        }

        [cardDisplay runAction:[SKAction moveTo:moveToPosition duration:0.3]];
        [cardDisplay runAction:[SKAction fadeOutWithDuration:0.4]];
        [NSThread sleepForTimeInterval:0.1];

        if (i == 6 && j == 4 - 1) {
            SKNode *node = [self childNodeWithName:@"cardBack"];
            [node removeAllChildren];

            for (int p = 0; p < [playerCards count]; p++) {

                addBegin = p * (cardDisplay.size.width / 3.0);
                NSString *imageName = [NSString stringWithFormat:@"%@", playerCards[p]];
                NSString *bundle = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
                UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
                SKTexture *texture = [SKTexture textureWithImage:image];
                cardDisplay = [CardSpriteNode spriteNodeWithTexture:texture];
                cardDisplay.size = CGSizeMake(104, 144);
                cardDisplay.anchorPoint = CGPointMake(0.5, 0.5);
                cardDisplay.position = CGPointMake(-self.frame.size.width/2.8 + addBegin, -300);
                cardDisplay.zPosition = 1;
                cardDisplay.userInteractionEnabled = NO;
                cardDisplay.name = [NSString stringWithFormat:@"%@", playerCards[p]];
                [self addChild:cardDisplay];

                CGPoint moveToNewPosition = CGPointMake(-self.frame.size.width/2.8 + addBegin, -218);

                [cardDisplay runAction:[SKAction moveTo:moveToNewPosition duration:1]];
                [NSThread sleepForTimeInterval:0.2];
            }
        }
    }
}

就像我说的那样,这在我的其他skscene中运行良好,但是在这个中它并没有,并且据我所知,一切都已初始化。

2 个答案:

答案 0 :(得分:3)

Grand Central Dispatch(GCD)

来自Apple的文档:

  

Grand Central Dispatch(GCD)包括语言功能,运行时库和系统增强功能,可为iOS和OS X中多核硬件上的并发代码执行提供系统,全面的改进。

使用GCD非常简单。以下是使用GCD在单独的线程上执行代码的示例。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    for (int i = 0; i < 7; i++) {
        //to loop between players
        for (int j = 0; j < 4; j++) {

            NSString *imageName = [NSString stringWithFormat:@"back"];
            NSString *bundle = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
            UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
            SKTexture *texture = [SKTexture textureWithImage:image];
            cardDisplay = [CardSpriteNode spriteNodeWithTexture:texture];
            cardDisplay.size = CGSizeMake(104, 144);
            cardDisplay.position = _dealPosition;
            cardDisplay.zPosition = zPosCount;
            zPosCount++;
            cardDisplay.userInteractionEnabled = YES;
            cardDisplay.name = @"cardBack";
            dispatch_async(dispatch_get_main_queue(), ^(void){
                    [self addChild:cardDisplay];
            });

            CGPoint moveToPosition;
            //change position to deal to
            if (j == 0) {
                moveToPosition = position1;
            }
            else if (j == 1) {
                moveToPosition = position2;
            }
            else if (j == 2) {
                moveToPosition = position3;
            }
            else if (j == 3) {
                moveToPosition = position4;
            }

            [cardDisplay runAction:[SKAction moveTo:moveToPosition duration:0.3]];
            [cardDisplay runAction:[SKAction fadeOutWithDuration:0.4]];
            [NSThread sleepForTimeInterval:0.1];

            if (i == 6 && j == 4 - 1) {
                SKNode *node = [self childNodeWithName:@"cardBack"];
                [node removeAllChildren];

                for (int p = 0; p < [playerCards count]; p++) {

                    addBegin = p * (cardDisplay.size.width / 3.0);
                    NSString *imageName = [NSString stringWithFormat:@"%@", playerCards[p]];
                    NSString *bundle = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
                    UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
                    SKTexture *texture = [SKTexture textureWithImage:image];
                    cardDisplay = [CardSpriteNode spriteNodeWithTexture:texture];
                    cardDisplay.size = CGSizeMake(104, 144);
                    cardDisplay.anchorPoint = CGPointMake(0.5, 0.5);
                    cardDisplay.position = CGPointMake(-self.frame.size.width/2.8 + addBegin, -300);
                    cardDisplay.zPosition = 1;
                    cardDisplay.userInteractionEnabled = NO;
                    cardDisplay.name = [NSString stringWithFormat:@"%@", playerCards[p]];
                    dispatch_async(dispatch_get_main_queue(), ^(void){
                        [self addChild:cardDisplay];
                    });

                    CGPoint moveToNewPosition = CGPointMake(-self.frame.size.width/2.8 + addBegin, -218);

                    [cardDisplay runAction:[SKAction moveTo:moveToNewPosition duration:1]];
                    [NSThread sleepForTimeInterval:0.2];
                }
            }
        }
    }
});

你会注意到我使用的一些地方:

dispatch_async(dispatch_get_main_queue(), ^(void){
    [self addChild:cardDisplay];
});

这将始终包含将影响UI的代码;所有UI代码必须在主线程上执行。

答案 1 :(得分:2)

    [self performSelectorInBackground:@selector(dealCards) withObject:self];

不确定原因,但确实有效。

请勿使用此答案。