【问题标题】:Moving A UIButton from Sprite-Kit scene从 Sprite-Kit 场景中移动一个 UIButton
【发布时间】:2014-04-23 00:13:28
【问题描述】:

与 Game Center 合作,我决定创建一个 UIButton,我称之为排行榜。问题是,在 myScene 上,当 Start Button(一个 sprite-kit 节点)被触摸时,我需要将排行榜移出屏幕但不能这样做,因为它发生在 myScene 而不是 ViewController 上。

-(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 在屏幕上仍然可见,从而扰乱了游戏。非常感谢任何帮助。

【问题讨论】:

    标签: ios sprite-kit viewcontroller


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多