iOS 7 SpriteKit - Creating game menus/buttons (Scenes vs. more view controllers)

前端 未结 3 694
暗喜
暗喜 2021-02-03 10:06

So, I\'ve begun working on a project in SpriteKit, and I\'m hoping to be able to implement a \"main menu\" scene with a few buttons. One would go to a level select screen, one w

3条回答
  •  春和景丽
    2021-02-03 10:44

    Although @Albert's answer is a good one, i have always just used SKLabelNode. It is a class that is already inside of the SpriteKit framework, and doesn't require the use of gestures.

    This makes you wonder, how does it react to actions? Well, that is actually quite simple.

    I just use touchesBegan:withEvent: method, and I check if the label node is being pressed upon. Here is an example.

    // in .m file
    @interface ClassName () {
       SKLabelNode *menuItem1;
       SKLabelNode *menuItem2;
    }
    
    @implementation
    
    - (id) initWithSize:(CGSize)size // can't fully remember this one...
    {
         if(self = [super initWithSize:size]) {
            // here you create your labels
            menuItem1 = [SKLabelNode labelNodeWithFontNamed:@"Your font."];
    
            // this will position this at the center, top 1/3 of the screen
            menuItem1.position = CGPointMake(self.frame.size.width/2, self.frame.size.height * .3); 
            menuItem1.text = @"Menu 1";
            [self addChild:menuItem1]; // takes care of the first
    
            menuItem2 = [SKLabelNode labelNodeWithFontNamed:@"Your font."];
    
            // place at center, lower 1/3 of screen
            menuItem2.position = CGPointMake(self.frame.size.width/2, self.frame.size.height * .6); 
            menuItem2.text = @"Menu 2";
            [self addChild:menuItem2];
        }
        return self;
    }
    
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
         UITouch *t = [touches anyObject];
         CGPoint touchLocation = [t locationInNode:self.scene];
    
          if(CGRectContainsPoint(menuItem1.frame, touchLocation)) {
              // do whatever for first menu
          }
    
          if(CGRectContainsPoint(menuItem2.frame, touchLocation)) {
              // do whatever for second menu
          }
    }
    

    hope this helps!

提交回复
热议问题