SirDeveloper
SirDeveloper

Reputation: 276

SKScene: bounds of scene

How can I get the bound of scene?

I'm testing with demo and I initialize scene in this way:

GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
scene.scaleMode = SKSceneScaleModeAspectFill;

So, when I tap on screen I log:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:   self];
        NSLog(@"%f, %f", location.x, location.y);
    }
}

When I press left-bottom I get circa 300-0 while I'm expecting 0-0.

How can I get the top-bottom-left-right bounds? I can't find a method like:

[scene getBounds]

Scene.frame prints: (0,0,1024,768) that not corresponds!!

Upvotes: 1

Views: 1157

Answers (2)

ABakerSmith
ABakerSmith

Reputation: 22959

You need to set the size of the GameScene to be equal to the size of its SKView. You can either do this in the didMoveToView of your GameScene:

-(void)didMoveToView:(SKView *)view {
    [super didMoveToView: view];
    self.size = view.bounds.size;
}

Or, you can set the size when you instantiate your GameScene:

SKView * skView = (SKView *)self.view;
// ...

GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
scene.scaleMode = SKSceneScaleModeAspectFill;
scene.size = skView.bounds.size;

[skView presentScene:scene];

Upvotes: 4

Darvas
Darvas

Reputation: 974

Initialize scene like this:

 GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
 scene.scaleMode = SKSceneScaleModeAspectFill;

Test coordinates like this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    /* Pressed node */
    SKNode *node = [self nodeAtPoint:location];

    NSLog(@"x - %f; y - %f", location.x, location.y);
}

Upvotes: 0

Related Questions