I am currently integrating UI elements inside a sprite-kit scene, and i have to say that is a bit tricky. If you could give me more information about the other elements of the class, if is it a UIView
or a SKScene
(SKView), or a SKNode
. When mixing both approaches this really is a pain.
But with the information you gave I can tell you that I have the same problem and I solved changing the method where a I added the UI elements on the SKScene. Before it was on the viewDidLoad but I should do it on the viewDidAppear.
EXAMPLE (Objective-C):
- The iCarousel is a UI element (not SKView or SKNode).
`
-(void)didMoveToView:(SKView *)view {
//Your code but in Objective-C
UIButton * button = UIButton
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage * image = [UIImage imageNamed:@"bluePlayButton"];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake(self.size.width/2, self.size.height * 0.15, 300, 200)];
button.layer.zPosition=1;
[self.view addSubView:button];
}
`
Also, double check the zPosition of the button and the other elements.
Make really sure that the image is being loaded properly with the debugger. Even with the extension (.png) something might be happening to prevent so. ( as suggested by @matt on the comments)
I STRONGLY recommend to avoid mixing UIKit elements with Sprite-Kit elements.
- Please give some more information if this does not solve your problem ;)
EDIT:
Another example of how to do the the corresponding to UIButton in Sprite-Kit ( the most simple way).
(Just taken from this question - Setting up buttons in SKScene )
You could use a SKSpriteNode as your button, and then when the user touches, check if that was the node touched. Use the SKSpriteNode's name property to identify the node:
//fire button
- (SKSpriteNode *)fireButtonNode
{
SKSpriteNode *fireNode = [SKSpriteNode spriteNodeWithImageNamed:@"fireButton.png"];
fireNode.position = CGPointMake(fireButtonX,fireButtonY);
fireNode.name = @"fireButtonNode";//how the node is identified later
fireNode.zPosition = 1.0;
return fireNode;
}
Add node to your scene:
[self addChild: [self fireButtonNode]];
Handle touches:
//handle touch events
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
//if fire button touched, bring the rain
if ([node.name isEqualToString:@"fireButtonNode"]) {
//do whatever...
}
}