SpriteBuilder を使用してゲームのレベルを設計しています。ゲームでは、スワイプすると動くブロック オブジェクトがあります。ただし、レベルにこれらのオブジェクトが複数ある場合、それぞれが最後に追加されたオブジェクトのみを参照します。それらはすべて固有のオブジェクトとして初期化されますが、スワイプしても動きません。正しく機能するのは、レベルに最後に追加されたものだけです。
スワイプジェスチャはこちらです。
- (void)didLoadFromCCB {
// Listen for swipe gestures to the right and left
UISwipeGestureRecognizer * swipeRight= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[[[CCDirector sharedDirector] view] addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer * swipeLeft= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[[[CCDirector sharedDirector] view] addGestureRecognizer:swipeLeft];
}
そして、これは、間違ったオブジェクトを自己として記録するスワイプ方法の 1 つです。また、正しいオブジェクトをログに記録するタッチ開始メソッドもあります。
- (void)swipeRight {
// Only move the object if it is being touched while swiping
CCLOG(@" Swipe right %@", self);
if (self.touched) {
// Move the object off the screen to the right
float width = [self parent].contentSizeInPoints.width;
[self swipeOffScreen:ccp(self.position.x + width, self.position.y)];
}
}