使用Objective-C switch语句的奇怪行为

时间:2015-05-23 05:37:33

标签: ios objective-c sprite-kit switch-statement

我有一个使用全局整数position来浏览选项菜单的游戏。当为每个屏幕按下后退按钮时,它会调用一个函数[self options]来重新加载原始"选项"屏幕。我有一种感觉,这是我非常愚蠢的事情。

现在,调用函数的代码确定后退按钮是否为:

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

touchIsInNode:是我自己的自定义方法,用于处理点击返回BOOL值(如果是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个子菜单。

1 个答案:

答案 0 :(得分:1)

您需要在每个案例块的末尾添加一个break语句。