从Sprite-Kit场景移动UIButton

时间:2014-02-22 05:33:48

标签: ios sprite-kit viewcontroller

与Game Center合作我决定创建一个UIButton,我称之为Leaderboards。问题是myScene在启动按钮(一个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中,我会使用下面的代码将Leaderboards按钮移出屏幕,但不能在myScene上使用它。

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

目前UIButton在屏幕上仍然可见,从而破坏了游戏。非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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];