Merick XD
Merick XD

Reputation: 21

Passing the object from SKScene to UIViewController

I'm trying to pass an object from an SKScene to the current UIViewController where the scene is being shown, it's like I created a label that will only be triggered once the object from the scene reached a specific location, I understand that I can just easily create a SKLabel and have it added to the scene once the object reaches the location I want it to, but I'd rather do it the ViewController style way since I'll be adding a lot of objects that will do the same thing as my app Progress, that reason step aside, I did actually tried adding an sk label to see if it will work that way, Yes I was able to see the SKLabel appear upon the object reaching let's say location.x = 50 and I set the node to be removed when the object reaches location.x = 270, But the problem is it's only doing it once, after the object being added again, it seems that the scene is not removing the node even though I'm pointing my object to hit 270..

By the way, since I mentioned 2 problems, here's the code that executes the said operation for the SKlabel node which is only happening once, I want it to execute the statement one time, everytime I hit that location

if (newLocation.x==270.00 )) {
    [self addingTheLabel];
}

if (newLocation.x == 50.00) {
    SKAction *removingTheNode = [SKAction removeFromParent];
    [self.label runAction:removingTheNode];
}

Upvotes: 1

Views: 119

Answers (2)

Merick XD
Merick XD

Reputation: 21

Nevermind, was able to resolve the issue now..

For those people who might encounter this,Creating a protocol on your scene will fix the issue:

@protocol gameSceneDelegate <NSObject>
-(void)testProtocol;
-(void)testProtocol2;

@end;
@interface MyScene : SKScene
@property (weak,nonatomic) id<gameSceneDelegate> delegate;

Implement it on your scene's view controller:

@interface ViewController : UIViewController<gameSceneDelegate>

in ViewController.m you need to set first your scene as your delegate:

MyScene *scenePointer = (MyScene*) scene;

[scenePointer setDelegate:self];

and finally, implement the methods on your ViewController:

-(void)testProtocol{
    NSString *sampleString2 = [[NSString alloc]initWithFormat:@"This will show when testProtocol is selected"];
    self.sampleLabel.text = sampleString2;

}

-(void)testProtocol2{
    NSString *sampleString3 = [[NSString alloc]initWithFormat:@"This will show when test 2 protocol is selected"];
    self.sampleLabel.text = sampleString2;
}

Make an if statement inside your ViewDidLoad that if your scenePoint is the delegate do the following:

if([scenePointer delegate]){
    [self testProtocol];
    [self testProtocol2];

}

Now, Going to your Scene, since what I want is for the label to change whenever the SpriteNode hits a specific location, what I did is:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    for(UITouch *touch in touches){
        CGPoint location = [touch locationInNode:self];
        CGPoint newLocation = CGPointMake(location.x, 450);

         if (newLocation.x == 270) {
            //This will trigger the method everytime the spritenode hit's this location
            if([_delegate respondsToSelector:@selector(testProtocol2)]){
                [_delegate performSelector:@selector(testProtocol2)];
                }

        }


        if(newLocation.x <= 220){
            newLocation.x = 220;
            //This will trigger the method everytime the spritenode hit's this location
            if([_delegate respondsToSelector:@selector(testProtocol)]){
                [_delegate performSelector:@selector(testProtocol)];

            }

        }
        self.spriteNode.position = newLocation;
    }

}

It will now change the label every time your SpriteNode hits the location you want it to. I hope this will help other's who are in the same concept as this.

Upvotes: 1

lennartk
lennartk

Reputation: 619

import your appdelegate and then use

ViewController *vc = (ViewController*)[AppDelegate*)[[UIApplication sharedApplication] delegate] viewController];

to parse the object to your presented UIViewController

Upvotes: 0

Related Questions