在我目前的Sprite Kit項目中,我編寫了一個機制,我已經將UISwipeGestureRecognizer用於向左方向應用於兩個節點。當在屏幕的一側出現左側滑動時,其中一個節點移動,而在屏幕另一側出現左側滑動時,另一個節點移動。但是,我找不到一種方法使節點同時移動;儘管屏幕上位於不同的位置,但仍然無法識別2個左側滑動。同樣,我也爲用戶向右滑動時編寫了類似的機制。以下是我的代碼。我將不勝感激任何幫助。截至目前,只有一個節點可以在同一時間內刷卡,不能兩者都在一起,這是我想如何根據觸摸的位置獲得兩個完全相同的UISwipeGestureRecognizer以同時工作?
-(void)didMoveToView:(SKView *)view {
UISwipeGestureRecognizer *leftSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwiped1:)];
[leftSwipe1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[leftSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftSwipe1];
UISwipeGestureRecognizer *rightSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwiped1:)];
[rightSwipe1 setDirection:UISwipeGestureRecognizerDirectionRight];
[rightSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightSwipe1];
self.physicsWorld.gravity = CGVectorMake(0, -9.8);
self.physicsWorld.contactDelegate = self;
}
-(void)rightSwiped1:(UIGestureRecognizer *)gestureRecognizer {
CGPoint pt = [gestureRecognizer locationInView:self.view];
if(pt.x < (self.view.bounds.size.width/2))
{
SKNode *person1 = [self childNodeWithName:@"person1"];
SKAction *moveRight = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 80, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[person1 runAction:moveRight];
} else if (pt.x > (self.view.bounds.size.width/2)) {
SKNode *person2 = [self childNodeWithName:@"person2"];
SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 400, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[person2 runAction:moveRight2];
}
}
-(void)leftSwiped1:(UIGestureRecognizer *)gestureRecognizer {
CGPoint pt = [gestureRecognizer locationInView:self.view];
if(pt.x < (self.view.bounds.size.width/2))
{
SKNode *person1 = [self childNodeWithName:@"person1"];
SKAction *moveLeft = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[person1 runAction:moveLeft];
} else if (pt.x > (self.view.bounds.size.width/2)) {
SKNode *person2 = [self childNodeWithName:@"person2"];
SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 80, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[person2 runAction:moveLeft2];
}
}
你爲什麼不嘗試直接申請的UISwipeGestureRecognizer到SKNode? –
我該怎麼做? – RT33