Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 具有Objective-C开关语句的奇数行为_Ios_Objective C_Sprite Kit_Switch Statement - Fatal编程技术网

Ios 具有Objective-C开关语句的奇数行为

Ios 具有Objective-C开关语句的奇数行为,ios,objective-c,sprite-kit,switch-statement,Ios,Objective C,Sprite Kit,Switch Statement,我有一个使用全局整数位置在选项菜单中导航的游戏。当按下每个屏幕上的后退按钮时,它会调用一个函数,[self options],以重新加载原始的“options”屏幕。我有一种感觉,这是我搞砸的一件非常愚蠢的事情 现在,调用确定是否返回按钮的函数的代码如下: if ([self touchIsInNode:[self childNodeWithName:@"back"] touchPoint:touchPoint]) { // stuff should ha

我有一个使用全局整数
位置
在选项菜单中导航的游戏。当按下每个屏幕上的后退按钮时,它会调用一个函数,
[self options]
,以重新加载原始的“options”屏幕。我有一种感觉,这是我搞砸的一件非常愚蠢的事情

现在,调用确定是否返回按钮的函数的代码如下:

if ([self touchIsInNode:[self childNodeWithName:@"back"] touchPoint:touchPoint]) {
                    // stuff should happen here
                }
使用
touchinnode:
是我自己的自定义方法,用于处理返回
BOOL
值的点击(
YES
,如果是)

为每个屏幕的后退按钮命名
backa
backb
backc
等是一个可行的解决办法,但无论
位置的值如何,它仍然在每个开关情况下调用代码

以下是我在
开关
语句中的内容:

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
然后,对于选项菜单中的每个子菜单,它们看起来如下所示(使用“存储”菜单作为模型):

此代码的结果是,即使在
位置
的值更改为
1
表示返回到
选项
菜单后,也会执行每种情况。另外,
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

为什么每个
案例
都会运行,甚至是
默认案例?似乎只有
position==4
应该运行
case4
,而类似
position==500
的东西将运行
默认的
案例。如果有帮助,选项菜单中有5个子菜单。

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

case语句需要以
break结尾否则,它们将在下一种情况下继续。标准的C行为。我觉得自己像个白痴,因为我没有
break那里。非常感谢。当堆栈溢出允许时,我会接受这个答案。感谢您和Gerd K在评论中发现这一点!
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