2011-07-02 20 views
2

「パーティクル」がユーザーのタッチに引き付けられる単純なゲームに取り組んでいます。TouchesMovedは、変更されていないタッチを無視して変更したタッチを返します。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSArray *touch = [touches allObjects]; 
    for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) { 
     lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self]; 
    } 
    isTouching = YES; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSArray *touch = [touches allObjects]; 
    for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) { 
     lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self]; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSArray *touch = [touches allObjects]; 
    for (numberOfTouches = 0; numberOfTouches < [touch count]; numberOfTouches++) { 
     lastTouches[numberOfTouches] = [((UITouch *)[touch objectAtIndex:numberOfTouches]) locationInView:self]; 
    } 
    if (!stickyFingers) { 
     isTouching = NO; 
    } 
} 

lastTouchesプログラムの別の部分は、粒子を移動させるために使用するCGPointの配列です。

私が抱えている問題は、3つの関数のどれかが呼び出されると、CGPointsとnumberOfTouchesの配列を上書きするということです。私はこれが問題になるとは思わなかったが、TouchesMovedは変更されたタッチを取得し、同じままであったタッチを取得しないことが分かった。その結果、あなたが指のうちの1つを動かすが他の指は動かさなければ、プログラムは動いていない指について忘れ、すべての粒子が動いている指に向かう。両方の指を動かすと、粒子は2本の指の間を移動します。

私は、あるものを更新している間に移動していないタッチを保持する必要があります。

何か提案がありますか?

+0

'touchesCancelled:withEvent:'も実装することを忘れないでください。 –

答えて

1

Set theoryレスキュー!上記で

//Instance variables 
NSMutableSet *allTheTouches; 
NSMutableSet *touchesThatHaveNotMoved; 
NSMutableSet *touchesThatHaveNeverMoved; 

//In touchesBegan:withEvent: 
if (!allTheTouches) { 
    allTheTouches = [[NSMutableSet alloc] init]; 
    touchesThatHaveNotMoved = [[NSMutableSet alloc] init]; 
    touchesThatHaveNeverMoved = [[NSMutableSet alloc] init]; 
} 
[allTheTouches unionSet:touches]; 
[touchesThatHaveNotMoved unionSet:touches]; 
[touchesThatHaveNeverMoved unionSet:touches]; 

//In touchesMoved:withEvent: 
[touchesThatHaveNeverMoved minusSet:touches]; 
[touchesThatHaveNotMoved setSet:allTheTouches]; 
[touchesThatHaveNotMoved minusSet:touches]; 

//In touchesEnded:withEvent: 
[allTheTouches minusSet:touches]; 
if ([allTheTouches count] == 0) { 
    [allTheTouches release]; //Omit if using ARC 
    allTheTouches = nil; 
    [touchesThatHaveNotMoved release]; //Omit if using ARC 
    touchesThatHaveNotMoved = nil; 
} 
[touchesThatHaveNotMoved minusSet:touches]; 
[touchesThatHaveNeverMoved minusSet:touches]; 

touchesThatHaveNotMovedは最後touchesMoved:に動かなかったタッチを開催します、とtouchesThatHaveNeverMovedは、彼らが始まって以来、一度も移動していないタッチを開催します。どちらかまたは両方の変数、およびそれらを含むすべてのステートメントは、気にしないで省略することができます。

+0

これは完全に間違っています。 iOS(bizarrely)は* touchしていない場合* touchesMovedを渡しません。これは機能しません。 (これは正常なゲームエンジンでうまくいきますが、「触れるが動きません」と知っています) – Fattie

関連する問題