objective-c - For循环卡住Obj-C

标签 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 中工作正常,但在这个 skscene 中却没有,据我所知,一切都已初始化。

最佳答案

中央调度 (GCD)

来自 Apple 的文档:

Grand Central Dispatch (GCD) comprises language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in iOS and 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 代码都必须在主线程上执行。

关于objective-c - For循环卡住Obj-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29440688/

相关文章:

ios - 删除 NSMutableArray 中的对象会导致崩溃

ios - 如何通过代码从 SKScene 转到 UIViewController?

ios - 返回到相同条件下的上一个场景

ios - 文本字段作为搜索栏

objective-c - 如果当前版本可供销售但尚未公开,我可以向 App Store 提交新版本的应用程序吗?

objective-c - 简单的进度条不更新

ios - 如何在 Objective-C 中将视频保存在 iPhone Gallery 的特定文件夹中

ios - SpriteKit 内存泄漏?

ios - 使用 UserDefaults 在 SKScene 之间传递字符串

ios - 场景的子元素发布速度不够快?