0

Imagine three instances of a CCLayer subclass Block positioned next to each other. Each is 100x100 pixels and has some basic square background art. These three objects are part of an encapsulating CCLayer subclass called Container.

How can I swipe across the three Blocks and detect the swipe/tap/touch for each Block in Container?

If the problem was constrained to just handling taps on Blocks in Container then one could use a delegate. Something like BlockDelegate with a required method userTappedBlock:(Block *) b which would inform the Container whenever a Block is tapped.

However this approach hasn't worked for swiping yet. I get the callback in Container but only for the first Block that's tapped.

SundayMonday
  • 19,147
  • 29
  • 100
  • 154

2 Answers2

1

You can handle touches in layer, that contains your blocks. Then, in touchEnded:WithEvent: method check what block contains touch's position.

Morion
  • 10,495
  • 1
  • 24
  • 33
  • Something like that will work. Although I think I'd check in `touchMoved` as well as `touchEnded` since I'm swiping across the layers. Anyways I just thought it would be nice for the blocks to do the touch detection for me. – SundayMonday Sep 03 '12 at 17:37
  • Don't think that it is need. Just, for example, see how CCMenu is implemented. All the touches handles menu, not it items. – Morion Sep 04 '12 at 08:32
0

In CustomLayer.cpp write this method for tapping the layer

void CustomLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)

{

     CCTouch *touch = (CCTouch *) pTouches->anyObject();
     CCPoint location = touch->getLocationInView();
     location = CCDirector::sharedDirector()->convertToGL(location);

         if(layer->boundingBox().containsPoint(location))
                CCLog("layer Hit Test");


}
Sumit Kandoi
  • 427
  • 4
  • 12