How to call leaderboard from SKNode Button in MenuScene - ios

I got the following Warning:
Incompatible pointer types sending 'MenuScene' to parameter of type 'uiviewcontroller'
I want to call the leaderboard from Game Center by klick on this button in my menu scene.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode: self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"playButton"]) {
SKTransition *transition = [SKTransition fadeWithDuration:0.5];
MyScene *gameScene = [[MyScene alloc]initWithSize:CGSizeMake(self.size.width, self.size.height)];
[self.scene.view presentScene:gameScene transition:transition];
} else if ([node.name isEqualToString:#"leaderboardButton"]) {
[[GameCenterHelper defaultHelper] showLeaderboardOnViewController:self];
}
at this last lane, i got the warning

you should present a ViewController from root view controller, so you should do:
if ([node.name isEqualToString:#"leaderboardButton"]) {
UIViewController *vc = self.view.window.rootViewController;
[[GameCenterHelper defaultHelper] showLeaderboardOnViewController:vc];

Related

how to make a button in Sprite kit for touchesBegan

basically for now I got TitleScene, where are present name of the my game and when tap anywhere on screen it jumps to GameScene, it looks something like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKScene *scene = [GameScene sceneWithSize:self.size];
SKTransition *transition = [SKTransition pushWithDirection:SKTransitionDirectionUp duration:1.0f];
[self.view presentScene:scene transition:transition];
}
My question how can i change this method to button?
maybe give your sprite a name of "button" so you know which one youre touching
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *touchedNode = [self nodeAtPoint:location];
if (touchedNode && [touchedNode.name isEqual:#"button"]) {
// your code here
}
}
}

Returning To Previous Scene In Same Condition

I have GamePlayScene that when ended, removes all nodes and displays the Score and an option to restart. I also have a button on that scene that takes you to another scene to display upgradable items. I would like to make a function that adds a back button on the "upgrade items" scene that returns me to the ended GamePlay scene. The code that I have right now returns me back to the GamePlay scene but restarts the game instead of displaying the Game Over text. I've tried searching online but I can't manage to find a good solution. My Code:
GamePlayScene.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
...
else if (self.restart) {
[[GameState sharedInstance] saveState];
if ([node.name isEqualToString:#"UpgradesButton"]){
UpgradeShipScene *upgradeScene = [UpgradeShipScene sceneWithSize:(self.frame.size)];
SKTransition *transition = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:upgradeScene transition:transition];
}
...
}
UpgradesScene.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"BackButton"]){
for (SKNode *node in [self children]){
[node removeFromParent];
}
GamePlayScene *scene = [GamePlayScene sceneWithSize:self.view.bounds.size];
SKTransition *transition = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:scene transition:transition];
}
}
You can use a SKNode as a "fake" SKScene, and then use a SKAction to animate it (to present or hide).

How to cause a screen transition when an SKSpriteNode is tapped

I am building an iOS application using the spritekit framework, and I am trying to cause the screen to transition over to the next scene when the user taps the "start" button. I am able to get the screen to transition, and load the next scene, but it happens when the user taps ANYWHERE on the screen.I only want this to occur when the user taps the "start" spritenode. I created the start button using SKSpriteNode, and I named it startButton, using the name property.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKAction *doNothing = [SKAction moveByX:0 y:0 duration:0];
if (startButton != nil)
{
[startButton runAction:doNothing completion:^{
SKScene *gamescene = [[GameScene alloc] initWithSize:self.size];
SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:0.0];
[self.view presentScene:gamescene transition:transition];
}];
}
}
Thanks!
Instead of
if(startButton != nil){ }
try using this in the touchesBegan method
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if([node.name isEqualTo:#"startButton"]){
//start scene
}
Make sure that you set the name of the start button (label or sprite) like this
start.name = #"startButton";
EDIT:
Dont use
[startButton runAction:doNothing completion:^{
....
}
Instead, just transition right away
if([node.name isEqualTo:#"startButton"]){
SKScene *gamescene = [[GameScene alloc] initWithSize:self.size];
SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:0.0];
[self.view presentScene:gamescene transition:transition];
}

Button pressed animation in Sprite-Kit

In a Sprite-Kit game I want to animate Buttons when they were pressed. Right now the code reacts directly and to not wait till the animation is executed. Furthermore I want the button to animate back when the user wipes his finger out of the button.
Here my code:
-(void)addStartButton:(CGSize)size {
self.start = [SKSpriteNode spriteNodeWithImageNamed:#"startButton1"];
self.start.position = CGPointMake(size.width/2, size.height/2);
self.start.name = #"start";
[self addChild:self.start];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
self.start.texture = [SKTexture textureWithImageNamed:#"startButton2"];
MyScene *myScene = [MyScene sceneWithSize:self.size];
[self.view presentScene:myScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}
}
This will give you 2 second delay before switching to another scene:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
SKAction *changeTexture = [SKAction runBlock:^{
self.start.texture = [SKTexture textureWithImageNamed:#"startButton2"];
}];
SKAction *wait = [SKAction waitForDuration:2.f];
SKAction *presentAnotherScene = [SKAction runBlock:^{
MyScene *myScene = [MyScene sceneWithSize:self.size];
[self.view presentScene:myScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}];
[self runAction:[SKAction sequence:#[changeTexture,wait,presentAnotherScene]]];
}
}
Furthermore I want the button to animate back when the user wipes his finger out of the button.
This seems pointless, since you are transitioning to another scene when user presses the button.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
startButton.texture = [SKTexture textureWithImageNamed:#"startButton2"];
}else{startButton.texture = [SKTexture textureWithImageNamed:#"startButton1"];}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
startButton.texture = [SKTexture textureWithImageNamed:#"startButton2"];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
//Start Button Actions
}

Error for SKScene alloc

In the SKScene gamescene I alloc the scene but it has an error message with No visible #interface with 'SKScene' declares the selector alloc.
I have think this means im not declaring it in my interface?? Corrections plz.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
// if play button touched, start transition to next scene
if ([node.name isEqualToString:#"play"]) {
NSLog(#"play pressed");
SKScene *GameScene = [[GameScene alloc] initWithSize:self.size];
SKTransition *flip = [SKTransition flipVerticalWithDuration:1.0];
[self.view presentScene:GameScene transition:flip];
}
}
It looks like you are trying to create a variable that is the same name as the class!
SKScene *GameScene = [[GameScene alloc] initWithSize:self.size];
This is probably confusing the compiler. Change the variable name of *GameScene to something else and see if that works.

Resources