Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

landscape mode don't work properly in spritekit game

i started a platformer game in spriteKit and i have a little problem with landscape mode. When i make a jump with my hero, I use the

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

and while the hero it's in the air and collides with a wall or any object and miss his speed, then i use the

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];

CGPoint positionInScene = [touch locationInNode:self];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];

CGPoint posicionHero = [self.world childNodeWithName:@"hero"].position;
SKSpriteNode *touchHero = (SKSpriteNode *)[self nodeAtPoint:posicionhero];

//pragma mark -hero movement in the air

if ((touchedNode != touchHero)
    &&
    (positionInScene.x > [self.world childNodeWithName:@"hero"].position.x))
{
    [[self.world childNodeWithName:@"hero"].physicsBody applyImpulse:CGVectorMake(5, 0)];
}
if ((touchedNode != touchHero)
    &&
    (positionInScene.x < [self.world childNodeWithName:@"hero"].position.x))
{
    [[self.world childNodeWithName:@"hero"].physicsBody applyImpulse:CGVectorMake(-5, 0)];
}

}

to keep that speed. I've been testing in portrait mode and everything were ok, but my game it's in landscape mode, so finally I have blocked the game to landscape mode, and now I'm testing it in landscape mode and I'm having troubles, because when I move my finger forward in a jump, nothing happens. Instead of this I have to move my finger upward, to get that movement. I'm sure i'm missing some basic configuration for my landscape mode. Any help will be apreciated

like image 702
Alfro Avatar asked Feb 14 '23 10:02

Alfro


1 Answers

This is actually a common problem in SpriteKit games. This occurs because viewDidLoad actually runs before the app checks for device orientation. You can fix it by replacing viewDidLoad with viewWillLayoutSubviews which will run after device orientation detection. For example, you could replace the default SpriteKit viewDidLoad with this:

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;

        // Create and configure the scene.
        SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeAspectFill;

        // Present the scene.
        [skView presentScene:scene];
    }
}

For more information, checkout these sources: UIViewController returns invalid frame? http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners

like image 134
AaronBaker Avatar answered Feb 17 '23 02:02

AaronBaker