Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite Kit iOS7 - Add Slider

I thought you could add UIKit slider/or button to your sprite kit app.

But cannot figure out how to do this, the code to create a slider programatically is

if (self.view) {
    CGRect frame = CGRectMake(0.0, 0.0, 200.0, 300);
    UISlider *slider = [[UISlider alloc] initWithFrame:frame];
    //[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [slider setBackgroundColor:[UIColor clearColor]];
    slider.minimumValue = 0.0;
    slider.maximumValue = 50.0;
    slider.continuous = YES;
    [self.view addSubview:slider];
    NSLog(@"View is alive - look for slider...");
} 
else {
    NSLog(@"No View!!");
}

The above does not work, the number of subviews of the view remains the same

I assume I have to add it as a child to my layer (SKNode), however the addChild method will not work with a UISlider. It needs to be an SKNode itself.

I am calling this within the scene class here

-(id)initWithSize:(CGSize)size {    

    if (self = [super initWithSize:size]) {
         // thought putting here would work 
         // but self.view is nil
    }

    return self;
}

Thanks to comment, I can get it to show - but I have to add in within the viewController class, like this

- (void)viewDidLoad
{
[super viewDidLoad];

// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [XBLMyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];

if (self.view) {
    CGRect frame = CGRectMake(0.0, 0.0, 200.0, 300);
    UISlider *slider = [[UISlider alloc] initWithFrame:frame];
    //[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [slider setBackgroundColor:[UIColor clearColor]];
    slider.minimumValue = 0.0;
    slider.maximumValue = 50.0;
    slider.continuous = YES;
    NSLog(@"View is alive - look for slider...");
    [self.view addSubview:slider];
} else {
    NSLog(@"No View!!");
}
}

Is there not away to do it within the actual scene class ....

Thanks

like image 902
DogCoffee Avatar asked Sep 27 '13 08:09

DogCoffee


1 Answers

After some tinkering

My solution, thanks to @LearnCocos2D hint

My Scene Class needed this

- (void) didMoveToView:(SKView *)view
{
    [self addSlider];
}

Then call your method to add the slider.... etc

like image 114
DogCoffee Avatar answered Sep 22 '22 07:09

DogCoffee