Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to use motion for transition instead touches.

I am building the simple game using sprite kid (the tutorial that treehouse provide us); but I will like to ad the ability to shake the phone in order to make the transition from launch image to background image ( As we did building cristal ball, "touch or shake to get a prediction"). I already did the touch transition right just fallowing step by step the video but when I wrote the code for "motion", nothing happened. Can someone tell me whats is wrong Here is the code I wrote: // // TNTitleScene.m // Scriming is coming // // Created by Ihos on 06/09/14. // Copyright (c) 2014 Ihos. All rights reserved. //

import "TNTitleScene.h"

import "TNGamePlayScene.h"

@implementation TNTitleScene

-(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { /* Setup your scene here */

SKSpriteNode *background =[SKSpriteNode spriteNodeWithImageNamed:@"Launch"];
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:background];

} return self; }

-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { TNGamePlayScene *gamePlayScene = [TNGamePlayScene sceneWithSize:self.frame.size]; SKTransition *transition = [SKTransition fadeWithDuration:1.0]; [self.view presentScene:gamePlayScene transition:transition]; }

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ TNGamePlayScene *gamePlayScene = [TNGamePlayScene sceneWithSize:self.frame.size]; SKTransition *transition = [SKTransition fadeWithDuration:1.0]; [self.view presentScene:gamePlayScene transition:transition]; } @end

1 Answer

I think you will have to put the motion detection method in your view controller, not your scene. SKScene (which inherits from SKNode has touch detection methods, but not motion like shake and what not. So you will have to handle the shake from your view controller