我有一個遊戲使用全局整數position
來瀏覽選項菜單。當每個屏幕都按下後退按鈕時,它會調用功能[self options]
重新加載原始「選項」屏幕。我有一種感覺,這是非常愚蠢的東西我搞砸了。Objective-C switch語句的奇怪行爲
現在,調用確定背部按鈕是否是這個函數的代碼:
if ([self touchIsInNode:[self childNodeWithName:@"back"] touchPoint:touchPoint]) {
// stuff should happen here
}
隨着touchIsInNode:
是處理水龍頭返回BOOL
值自己的自定義方法(YES
如果是) 。
命名爲每個屏幕backa
,backb
,backc
等的後退按鈕的按鈕是一個可行的解決辦法,但它仍然呼叫在每個開關情況下,代碼不管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'結尾,否則它們將在下一個case中繼續。標準C行爲。 –
我覺得這樣一個白癡沒有'休息',那裏。謝謝! – DDPWNAGE