Drawing Touch Points With Cocos2d
I’ve been experimenting with cocos2d for iOS development and made a very simple program that grabs the touch locations on the screen and draws the points. It uses a C++ vector to store the CGPoint primitives and the draw: message takes care of drawing them, whenever we fill the vector with points.

Here’s the draw:

Here’s the draw:
static std::vectorAnd here’s how the points are captured:points; ... -(void) draw { ... // Draw points glPointSize(4); for (int i = 0; i < points.size(); i++) { glColor4ub(0,255,0,255); // Green CGPoint p = points.at(i); ccDrawPoint(p); glColor4ub(255,255,255,255); // White CGPoint *vertices = &points[0]; // Vector to array ccDrawPoly(vertices, points.size(), NO); } ... }
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
NSLog(@"TOUCH BEGIN %f %f", location.x, location.y);
points.clear();
points.push_back(location);
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
points.push_back(location);
}
-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
points.clear();
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
NSLog(@"TOUCH END %f %f", location.x, location.y);
for (int i = 0; i < points.size(); i++) {
CGPoint p = points.at(i);
NSLog(@" TOUCH @ %f %f", p.x, p.y);
}
}
Download Xcode Project