cocos2d supports 2 kind of touches:
a Standard Touches
b: Targeted Touches
a)
They are like the Cocoa (Cocoa, not cocos2d) touches. The delegate receives a set of touches.
The difference between Cocoa touches and Standard touches, is that Standard touches should return a BOOL.
- NO: means that all the events will be propagated to the next delegate.
- YES: means that all the events won't be propagated.
It is worth noting that each touch event (BEGIN, MOVE, END, CANCEL) is treated independently.
So, you can return YES for the BEGIN event, and NO for the END event.
b)>
It uses a different approach. The delegate, instead of receiving a set of touches, it receives 1 touch a the time.
And the delegate can claim a touch or not, and swallow it or not.
- Claim: This delegate will receive ALL the events of the claimed touch (begin, moved, end, cancelled).
- Swallow: If YES, it means that this delegate will be the only one to receive the events of this touch. The touch won't be propagated to other delegates. If NO, it means that this event will be propagated to other delegates.
Simple Example:
If you have 3 touches: TA, TB, TC
and 1 standard delegate, this delegate will receive the 3 touches.
(BOOL) ccTouchesBegin( touches=TA,TB,TC)
{
// these 3 touches will be consumed, they won't be propagated to the next delegate
return YES;
// or, these 3 touches will be propagated at all.
return NO;
}
Complex example:
You have 3 delegates in this order:
- targetDelegate1
- standardDelegate
- targetDelegate2
Let's say that you have 1 touch: TA (touch A).
- targetDelegate1 receives TA(BEGIN)
- If he claims it he will receive all TA events: TA(MOVE), TA(END) and TA(CANCEL)
- If the delegate was registered as a "swallow" delegate, then standardDelegate and targetDelegate2 won't receive any events from TA (MOVE,END,CANCEL).
let's say you have a 2nd and 3rd touch: TB and TC
- targetDelegate1 doesn't claim TB and TC, so it won't receive the rest of the events (MOVE,END,CANCEL)
- so standardDelegate WILL receive TB(BEGIN,MOVE,END,CANCEL) and TC(BEGIN,MOVE,END,CANCEL).
- and standardDelegate should decide whether to consume these events (they won't be propagated to targetDelegate2), or ignore them (they will be propagated to targetDelegate2). Let's image that standardDelegate consumes TB(BEGIN,MOVE) and TC(END).
- targetDelegate2 won't receive any event from TB. Even though TB(END) and TB(CANCEL) were not consumed, they won't be propagated to targetDelegate2, because the "target" handlers can only claim a touch at BEGIN time.
- Let's say that targetDelegate2 decides to claim TC. So it will receive TC(MOVE) and TC(CANCEL) but it won't receive TC(END) because it is being consumed by standardDelegate.
So, as I see it, the cocos2d touch system is very flexible, but this flexibility also means more complex and slower code.
Proposed changes:
1) StandardDelegate: The touches can't be consumed. They will be propagated to the next delegate, always.
The possibility to consume touches was introduced in cocos2d v0.5 or v0.6, but with the new targeted delegates, I think they are deprecated.
Why:? Suppose that the StandardDelegate receives 5 touches, but it really wants to consume 1 of them. Impossible. Either the StandardDelegate consumes all of them, or ignore all of them. A mixed solution is not possible with StandardDelegate. If you want to consume 1 touch, you should use a TargetDelegate.
Just this little change (StandardDelegate can't consume/swallow touches) will simplify the code, and also will force the user to use the "correct" delegate (less errors in the user code).
2) I'm not sure yet about this change, but I think that the StandardDelegates should always be after the targetDelegate in the propagation queue.
Example:
1. targetDelegate1
2. targetDelegate2
3. standardDelegate1 <--- standard delegates should be after the target delegates in the propagation queue.
4. standardDelegate2
What do you think ?