UIButton not displayed in SKScene

advertisements

I'm trying to write some methods which can be used to generate labels, images, buttons etc.. in my spritekit project. I call my setUPScene method in the initWithSize method and in the setUpScene method I call three methods to set label, image and button. My label and image appears on the main scene but I can't succeed to appear UIButton type button. Actually I seek for some error but I think everything seems right. Here are my methods;

-(void) setUpMainScene {

    self.backgroundColor = [SKColor colorWithRed:1.0 green:0.15 blue:0.3 alpha:1.0];

    [self addChild:[self createLabel:@"Vecihi"
                   labelFontName:@"Chalkduster"
                   labelFontSize:20
                  labelPositionX:CGRectGetMidX(self.frame)
                  labelPositionY:CGRectGetMidY(self.frame)]];

   [self addChild:[self createImage:@"Spaceship"
                  imagePositionX:CGRectGetMidX(self.frame)
                  imagePositionY:CGRectGetMidY(self.frame)+60
                      imageWidth:40.0
                     imageHeight:60.0]];

   [self.view addSubview:[self createButton:@"ButtonSample.png"
                           setTitleColor:[UIColor whiteColor]
                         buttonPositionX:(CGRectGetMidX(self.frame))
                         buttonPositionY:(CGRectGetMidY(self.frame)-200.0)
                             buttonWidth:200.0
                            buttonHeight:70.0]];

}

 //Create any label with given parameters
 -(SKLabelNode *) createLabel:(NSString *) labelTitle labelFontName:(NSString *)     fontName labelFontSize:(int)fontSize labelPositionX:(CGFloat) x labelPositionY:(CGFloat) y {

    SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:fontName];

    myLabel.text = labelTitle;
    myLabel.fontSize = fontSize;
    myLabel.position = CGPointMake(x,y);

   return myLabel;
}

 //Create any image with given parameters
-(SKSpriteNode *) createImage:(NSString *) imageName imagePositionX:(CGFloat) x   imagePositionY:(CGFloat) y imageWidth:(CGFloat) width imageHeight:(CGFloat) height {

SKSpriteNode *myImage = [SKSpriteNode spriteNodeWithImageNamed:imageName];

myImage.position = CGPointMake(x, y);
myImage.size = CGSizeMake(width, height);

return myImage;

}

//Create any button wtih given parameters
-(UIButton *) createButton:(NSString *) buttonImage setTitleColor:(UIColor *) titleColor buttonPositionX:(CGFloat) x buttonPositionY:(CGFloat) y buttonWidth:(CGFloat) width buttonHeight:(CGFloat) height {

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

NSLog(@"%@", buttonImage);

myButton.frame = CGRectMake(x, y, width, height);
myButton.backgroundColor = [UIColor clearColor];
[myButton setTitleColor:titleColor forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:buttonImage] forState:UIControlStateNormal];

return myButton;

}


You have to setup the button in the viewcontroller owning the scene, NOT the scene itself.

So, move

[self.view addSubview:[self createButton:@"ButtonSample.png" setTitleColor:[UIColor whiteColor] buttonPositionX:(CGRectGetMidX(self.frame)) buttonPositionY:(CGRectGetMidY(self.frame)-200.0) buttonWidth:200.0 buttonHeight:70.0]];

in SampleScene.m

to SampleViewController.m like this

- (void) viewDidLoad {

    [self.view addSubview:[self.scene createButton:@"ButtonSample.png"
                       setTitleColor:[UIColor whiteColor]
                     buttonPositionX:(CGRectGetMidX(self.view.frame))
                     buttonPositionY:(CGRectGetMidY(self.view.frame)-200.0)
                         buttonWidth:200.0
                        buttonHeight:70.0]];

    //More commands not shown...

}

Please note the changes in the button positioning, and button creation

Replacing SamplesScene & SampleViewController to their corresponding names of course