Game Development Reference
In-Depth Information
return hitView;
In this case, you first call the super implementation to receive the view the hitTest
would normally return. In almost all cases, this will be the CCGLView itself, but be-
cause you can add subviews to the CCGLView , it might return a subview, and in this
case you want to allow the subview to handle the touch.
Otherwise, the runningScene is obtained from the CCDirector , which gives you
access to the cocos2d node hierarchy via the children array. Because the hitTest
point is in Cocoa Touch coordinates, you also have to convert it to GL coordinates be-
fore passing both the sceneChildren and the glPoint to the
hitTestNodeChildren method shown in Listing 15-5 . If that method returns a
hit, the hitTest responds by returning self . Otherwise, it lets the hitTest fail by
returning nil , allowing all views behind the cocos2d view to take their turn and pro-
ceed with the hit testing.
The hitTestNodeChildren method in Listing 15-5 is more complicated and
harder to understand because it uses recursion to traverse the cocos2d node hierarchy.
In other words, the function can call itself to go even deeper into the cocos2d node
hierarchy. Add the hitTestNodeChildren method just above the hitTest
Listing 15-5 . Recursively Testing All Nodes to Test If Their boundingBox Contains a
Given Point
@implementation CCGLView (hittest)
-(BOOL) hitTestNodeChildren:(CCArray*)children point:(CGPoint)point
BOOL hit = NO;
if (children.count > 0)
Class sceneClass = [CCScene class];
Class layerClass = [CCLayer class];
Search Nedrilad ::

Custom Search