嗨,我試圖用手勢識別與我的精靈套件的遊戲,我寫了這個代碼SpriteKit手勢識別器
@interface GameScene() <UIGestureRecognizerDelegate>{
UISwipeGestureRecognizer *swipeGestureLeft;
ISwipeGestureRecognizer *swipeGestureRight;
}
@end
@implementation GameScene
-(id)initWithSize:(CGSize)size{
if(self = [ super initWithSize:size]){
}
return self;
}
-(void)didMoveToView:(SKView *)view{
swipeGestureLeft = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)];
[swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[view addGestureRecognizer:swipeGestureLeft];
swipeGestureRight = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[view addGestureRecognizer:swipeGestureRight];
}
- (void) willMoveFromView: (SKView *) view {
[view removeGestureRecognizer: swipeGestureLeft ];
[view removeGestureRecognizer: swipeGestureRight];
}
-(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{
[email protected]"Left"'
}
-(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{
[email protected]"Right"'
}
@end
我覺得每一件事情是確定的,但我的手勢將無法正常工作,我不有錯誤信息,是否有東西丟失我應該添加到我的應用程序,或者我應該添加一些東西到我的視圖控制器,或者你們可以建議我一個totorial顯示我如何使用手勢與sprite套件,
你爲什麼要刪除GestureRecognizer?這是必要的嗎? – LinusGeffarth
@LinusG。我猜是因爲場景過渡。那些swipeRight和swipeLeft方法在該場景中定義。在場景轉換後識別器不會被刪除,所以應用會崩潰。 – Whirlwind