2014-02-22 80 views
1

使用Game Center我決定創建一個我稱之爲排行榜的UIButton。問題是,在myScene上,當啓動按鈕(一個精靈套件節點)被觸動時,我需要將排行榜從屏幕上移開,但不能這樣做,因爲它出現在myScene而不是ViewController上。從Sprite-Kit場景中移動一個UIButton

-(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]; 

    if ([node.name isEqualToString:@"Start"]) { 
     self.Start.position = CGPointMake(-100, -100); 
     self.Instructions.position = CGPointMake(-100, -100); 
     //Leaderboards need to be moved off screen now. 
    } 
} 

在ViewController中,我將使用下面的代碼將排行榜按鈕移出屏幕,但不能在myScene上使用它。

Leaderboards.center = CGPointMake(-100, -100); 

目前UIButton在屏幕上仍然可見,破壞遊戲。任何幫助是極大的讚賞。

回答

0

給您的場景中MyScene.h其父視圖控制器的引用:

#import <SpriteKit/SpriteKit.h> 
#import "MyViewController.h" 

@interface MyScene : SKScene 

@property (nonatomic,weak) MyViewController *viewController; 

@end 

然後在MyViewController.m當您創建的情景:

// Create and configure the scene. 
MyScene * scene = [MyScene sceneWithSize:skView.bounds.size]; 

//assign self to the view controller property 
scene.viewController = self; 

// Present the scene. 
[skView presentScene:scene]; 

,並創建方法:

-(void)moveLeaderButton{ 
    _leaderButton.center = CGPointMake(-100, -100); 
} 

也把它放在MyViewController.h中:

-(void)moveLeaderButton; 

然後在MyScene.m可以調用方法:

[_viewController moveLeaderButton];