Game Development Reference
In-Depth Information
The first thing that comes to mind is adding a gesture recognizer to our scene,
pointing it to some method, and be done with it. But unfortunately, this won't
work—SKNodes and SKScenes do not support adding gesture recognizers.
But there is a way to make this work.
SKScene has a handy method, (void)didMoveToView:(SKView *)view ,
which we can use, and it gets called each time a scene is about to be attached
to some view. When we run our game, this happens right after the scene creation,
and thus it is a useful place to set up our gesture recognizers. The following is
the code for this method:
- (void)didMoveToView:(SKView *)view
{
UISwipeGestureRecognizer *swiper = [[UISwipeGestureRecognizer
alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
swiper.direction = UISwipeGestureRecognizerDirectionRight;
[view addGestureRecognizer:swiper];
UISwipeGestureRecognizer *swiperTwo = [[UISwipeGestureRecognizer
alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
swiperTwo.direction = UISwipeGestureRecognizerDirectionLeft;
[view addGestureRecognizer:swiperTwo];
}
We create two gesture recognizers, set the methods to be called, and specifically to
UISwipeGestureRecognizer , set the swipe direction. Thus, if we swipe to the right,
one method is called, and if we swipe to the left, another one is triggered. These
methods are fairly straightforward, as they only increase or decrease speed:
- (void) handleSwipeRight:(UIGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateRecognized) {
backgroundMoveSpeed += 50;
}
}
- (void) handleSwipeLeft:(UIGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateRecognized &&
backgroundMoveSpeed > 50) {
backgroundMoveSpeed -= 50;
}
}
 
Search WWH ::




Custom Search