簡體   English   中英

Objective-C switch語句的奇怪行為

[英]Odd behavior with Objective-C switch statement

我有一個使用全局整數position在選項菜單中導航的游戲。 當為每個屏幕按下返回按鈕時,它將調用函數[self options] ,以重新加載原始的“ options”屏幕。 我感覺這是我弄得很傻的東西。

現在,調用確定后退按鈕是否為以下函數的代碼:

if ([self touchIsInNode:[self childNodeWithName:@"back"] touchPoint:touchPoint]) {
                    // stuff should happen here
                }

使用touchIsInNode:是我自己的自定義方法,用於處理返回BOOL值的水龍頭(如果YES則為YES )。

為每個屏幕backabackbbackc等命名后退按鈕是一種可行的解決方法,但是無論position的值如何,它仍會在每種開關情況下調用代碼。

這是我在switch語句中所做的事情:

switch (position) {
            // case 0 thru 3 are unrelated to the question... 0 is for the main menu, 1 is for the original logic for the "options" screen, 2 is for the end of the game, and 3 is to skip to the end of the intro screen.
            case 4:
            {
                NSLog(@"position: %i", position);
                if ([self touchIsInNode:[self childNodeWithName:@"backa"] touchPoint:touchPoint]) {
                    NSLog(@"store back button was pressed and [self options] is being called...");
                    [self options];
                }
                // store UI
            }
            case 5:
            {
                NSLog(@"position: %i", position);
                if ([self touchIsInNode:[self childNodeWithName:@"backb"] touchPoint:touchPoint]) {
                    NSLog(@"stats back button was pressed and [self options] is being called...");
                    [self options];
                }
                // stats UI
            }
            case 6:
            {
                NSLog(@"position: %i", position);
                if ([self touchIsInNode:[self childNodeWithName:@"backc"] touchPoint:touchPoint]) {
                    NSLog(@"about back button was pressed and [self options] is being called...");
                    [self options];
                }
                // about UI
            }
            case 7:
            {
                NSLog(@"position: %i", position);
                if ([self touchIsInNode:[self childNodeWithName:@"backe"] touchPoint:touchPoint]) {
                    NSLog(@"dev options back button was pressed and [self options] is being called...");
                    [self options];
                }
                // dev options UI
            }
            case 8:
            {
                NSLog(@"position: %i", position);
                if ([self touchIsInNode:[self childNodeWithName:@"backd"] touchPoint:touchPoint]) {
                    NSLog(@"purchased options back button was pressed and [self options] is being pressed...");
                    [self options];
                }
                // purchased options UI
            }
            default:
            {
                NSLog(@"INVALID POSITION: %i", position);
                break;
            }
        }
}

我的options方法如下所示:

for (SKNode* node in [self children]) {
    // fade out and remove each node if it is not "optionsButton"
    if (![node.name isEqual:@"optionsButton"]) {
        [node runAction:[SKAction fadeAlphaTo:0 duration:1] completion:^{
            [node removeFromParent];
        }];
    } else {
        // if it is "optionsButton", perform an animation.
        [node runAction:[SKAction scaleTo:1.5 duration:1]];
        [node runAction:[SKAction moveTo:CGPointMake(self.size.width / 2, self.size.height - ((node.frame.size.height * (4.0 / 3.0)) / node.yScale)) duration:1] completion:^{
            // afterwards, call a method that logs each node's name.
            [self logEveryNode];
        }];
    }
}
position = 1;
NSLog(@"position: %i", position);

// processing labels

// add in each child

NSLog(@"options called, with %i nodes in [self children]", [self children].count);

// fade in all of the labels

然后,對於選項菜單中的每個子菜單,它們看起來都像這樣(使用“存儲”作為模型):

position = 4;
for (SKNode* node in [self children]) {
    if ([node.name isEqual:@"optionsButton"]) {
        [node runAction:[SKAction scaleTo:(2.0 / 3.0) duration:1]];
        [node runAction:[SKAction moveTo:CGPointMake(self.size.width / 2, self.size.height - (node.frame.size.height * (1.0 / 3.0))) duration:1]];
    } else if ([node.name isEqual:@"store"]) {
        [node runAction:[SKAction scaleTo:1.5 duration:1]];
        [node runAction:[SKAction moveTo:CGPointMake(self.size.width / 2, self.size.height - node.frame.size.height * 3) duration:1]];
    } else {
        [node runAction:[SKAction fadeAlphaTo:0 duration:1] completion:^{
            [node removeFromParent];
        }];
    }
}

// draw UI for the store and move the back button accordingly

SKLabelNode *back = [SKLabelNode labelNodeWithFontNamed:@"Cochin"];
back.alpha = 0;
back.fontColor = [UIColor blackColor];
back.text = @"back";
back.name = @"backa";
back.fontSize = 44;
back.position = CGPointMake(self.size.width / 2, self.size.height * 0.5);

[self addChild:back];

[back runAction:[SKAction fadeAlphaTo:1 duration:1]];

即使position的值已更改為1表示返回到options菜單,該代碼的結果也會在每種情況下執行。 同樣, didBeginTouches被奇怪地調用了5次,盡管我在模擬器中只“輕按”了一次屏幕。 以下是與日志相關的部分:

2015-05-23 01:10:40.581 game[25258:1981445] position: 4
2015-05-23 01:10:40.581 game[25258:1981445] store back button was pressed and [self options] is being called...
2015-05-23 01:10:40.582 game[25258:1981445] position: 1
2015-05-23 01:10:40.588 game[25258:1981445] options called, with 10 nodes in [self children]
2015-05-23 01:10:40.588 game[25258:1981445] position: 1
2015-05-23 01:10:40.588 game[25258:1981445] position: 1
2015-05-23 01:10:40.588 game[25258:1981445] position: 1
2015-05-23 01:10:40.589 game[25258:1981445] position: 1
2015-05-23 01:10:40.589 game[25258:1981445] INVALID POSITION: 1

為什么每個case運行,甚至是default案例? 似乎只有position == 4才能運行case 4 ,而position == 500類的東西可以運行default情況。 如果有幫助,則選項菜單中有5個子菜單。

您需要在每個case塊的末尾添加一個break語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM