0

How come I can't use sceneWithSize: inside a switch statement? If I create an object of my new game scene outside the switch case it works fine? But I\m switching game scenes depending on which spriteNode the user selected. Why is this happening and is there an alternative method to do what I'm trying to do?

-(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;
        }
    }
Cyrille
  • 25,014
  • 12
  • 67
  • 90

1 Answers1

4

It's not because of the method call, it's because you can't start a case with a variable declaration. You have to wrap the whole block in curly braces, like this:

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;
    }
Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • Technical explanation here: http://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement – Cyrille Oct 09 '14 at 18:05
  • whoops thanks I blame Xcode's default code snippet for a switch case statement :P –  Oct 09 '14 at 18:09