gpt4 book ai didi

ios - 方法不能在 Switch 语句中使用?

转载 作者:行者123 更新时间:2023-11-28 19:00:36 24 4
gpt4 key购买 nike

为什么我不能在 switch 语句中使用 sceneWithSize:?如果我在 switch case 之外创建我的新游戏场景的对象,它工作正常吗?但是我根据用户选择的 spriteNode 切换游戏场景。为什么会发生这种情况,是否有替代方法来完成我想做的事情?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
NSLog(@"Level Selected: %@", node.name);
int x = [node.name intValue];

switch (x) {
case 1:
// if I move this code outside the switch statement it works?
LevelOne *newscene = [LevelOne sceneWithSize:self.size]; // here is where I get an expected expression error?
//[self.view presentScene:newscene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
break;

default:
break;
}
}

最佳答案

这不是因为方法调用,而是因为你不能用变量声明开始一个case。您必须将整个 block 用大括号括起来,如下所示:

switch (x) {
case 1: {
// if I move this code outside the switch statement it works?
LevelOne *newscene = [LevelOne sceneWithSize:self.size]; // here is where I get an expected expression error?
//[self.view presentScene:newscene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
break;
}

default:
break;
}

关于ios - 方法不能在 Switch 语句中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com