Pregunta

Working with Game Center I decided to create a UIButton which I've called Leaderboards. The problem is that on myScene when the Start Button (a sprite-kit node), gets touched I need to move leaderboards off the screen but cannot do this since it occurs on myScene and not the 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.
    }
}

In the ViewController I would use the code below to move the Leaderboards button off the screen, but cannot use this on myScene.

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

Currently the UIButton remains visible on the screen distrupting the game. Any help is greatly appreciated.

¿Fue útil?

Solución

Give your Scene a reference to its parent view controller in MyScene.h:

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

@interface MyScene : SKScene

@property (nonatomic,weak) MyViewController *viewController;

@end

Then in MyViewController.m when you create the scene:

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

and create method:

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

also put it in MyViewController.h:

-(void)moveLeaderButton;

Then in MyScene.m you can call method:

[_viewController moveLeaderButton];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top