About CCTouches!

About CCTouches!
0.0 0


I test the CCTouch, then I find the m_nViewId is always 0, why?? thanks!


It is useless, we will delete it.


#737 is created for it.


How can I distinguish these points?


On android, points are put into CCSet in sequence, CCSet0] means the first touch, CCSet1] is the second, and so on.
I am not sure if it is true on iOS.


I first put oen finger, then I put second finger! Then I uplift the first finger. In the ccTouchesEnded(), the pTouches->count() == 1. through the pTouches, I don’t know the first finger or second finger!?


Yes, you are right. Android have a pid point which touch is operated, we don’t translate this message. I don’t know if iOS has this feature, we should keep all platforms have the same feature.

Why do you need it?


On iOS its possible to identify each touch by its addres in memory (for one touch its always the same).

UITouch* touch;
touchId = reinterpret_cast(touch);


Yes, how bright you are. You can use in the same way. The address of each touch keeps the same in its life cycle.




I believe UITouch objects on iPhone have a ‘hash’ property that returns a unique identifier for them. Safer to use than the memory address


I have this same problem and need to track CCTouches by hash. How can I create the hash instead of using memory address?



May be you can put the values into you hash table.
Why do you need hash?


Hi, just putting multi touch into my game and came across this problem too. The ID/index of the UITouch is not in the CCSet class.

@Minggo, it is needed for a case like this: Say you are moving three sprites around the screen using three fingers, I receive a CCSet with three CCTouch items in it (one CCTouch for each finger). Then I take a finger off the screen so I have two left, I now receive a CCSet with two CCTouch items (two pairs of coordinates), I can’t tell which finger disappeared! I could “guess” which finger disappeared by checking old coordinates but I might get it wrong if for example if all the fingers were close to each other when one was removed.

At the moment I am using the address of the “CCTouch**" to determine which finger disappeared. This works due to the code in Cocos2d’s Eagleview.mm but I think this could break in a future version of Cocos2D-X. Ideally the real UITouch’s ID should be passed onto the CCTouch class.
The code in Eagleview.mm currently looks like this:-

  • touchesBegan:touches withEvent:event
    cocos2d::CCSet set;
    for (UITouch touch in touches) {
    index = (NSNumber*)CFDictionaryGetValue(touchesIntergerDict, touch);
    int unUsedIndex = 0;
    // it is a new touch
    if (! index) {
    unUsedIndex = [self getUnUsedIndex];
    // The touches is more than MAX_TOUCHES ?
    if (unUsedIndex -1) {

          pTouch = s\_pTouches[unUsedIndex] = new cocos2d::CCTouch();
          float x = [touch locationInView: [touch view]].x;
          float y = [touch locationInView: [touch view]].y;
          pTouch-\>SetTouchInfo(0, x, y);
          CFDictionaryAddValue(touchesIntergerDict, touch, [NSNumber numberWithInt:unUsedIndex]);


    if (set.count() 0)
    You can see it uses the UITouch index itself in the line “NSNumber **index = CFDictionaryGetValue;”.

The code to pass on the index would be like this:-
** in the above Eagleview.mm touchesDown, the line “pTouch~~>SetTouchInfo;” needs to become "pTouch~~>SetTouchInfo(0, x, y, index);”. And the same for the touchesMoved, touchesEnded, touchesCancelled functions.
* The SetTouchInfo function in the class CCTouch header file, needs an extra int iIndex parameter like “void SetTouchInfo(int nViewId, float x, float y, int iIndex)”.
* And class CCTouch, in the header file, needs a variable “int m_iIndex;”
* And the code in the CCTouch class function SetTouchInfo needs the line “m_iIndex = iIndex;” adding. e.g:-

class CCTouch : public CCObject
    CCTouch() {}
    CCTouch(int nViewId, float x, float y) : m_nViewId(nViewId), m_point(x, y), m_prevPoint(x, y) {}

    CCPoint locationInView(int nViewId) {CC_UNUSED_PARAM(nViewId); return m_point; }
    CCPoint previousLocationInView(int nViewId) {CC_UNUSED_PARAM(nViewId); return m_prevPoint; }
    int view() { return m_nViewId; }

    void SetTouchInfo(int nViewId, float x, float y, int iIndex)              // <- added "int iIndex"
        m_nViewId   = nViewId;
        m_prevPoint = m_point;
        m_point.x   = x;
        m_point.y   = y;
        m_iIndex = iIndex;                  // <- added

    int     m_nViewId;
    CCPoint m_point;
    CCPoint m_prevPoint;
    int m_iIndex;                               // <- added

I did the above in the iPhone iOS Cocos2d-x version, it is the same sort of thing on Android ( “int pid = event.getPointerId(i);” in calls to “public boolean onTouchEvent(MotionEvent event)” ).


Ok, could you pull a request by github?


OK, pull request sent.