Home > Development, Tips, cocos2d > What is passed with ccTouchBegan event

What is passed with ccTouchBegan event

January 11th, 2010 labor Leave a comment Go to comments

- (BOOL) ccTouchesBegan:(NSMutableSet *)touches withEvent:(UIEvent *)event
- (BOOL) ccTouchesMoved:(NSMutableSet *)touches withEvent:(UIEvent *)event
- (BOOL) ccTouchesEnded:(NSMutableSet *)touches withEvent:(UIEvent *)event
- (BOOL) ccTouchesCancelled:(NSMutableSet *)touches withEvent:(UIEvent *)event

; This is call-back function that means “called by system(or cocos2d)”

(NSMutableSet *)touches – (UITouch touch = [touches anyObject];)
; This mutable set includes information of touches newly began.

(UIEvent *)event – (UITouch touch = [[event allTouches] anyObject];)
; This UIEvent class includes information for all touches (check the manual for detail)

To read a location where touch event has been triggered (using cocos2d)

CGPoint pt = [touch locationInView:[touch view]];
CGPoint location = [[Director sharedDirector] convertToGL:pt];

For instance, when you touch first one and keep it and touch second.

ccTouchesBegan will be triggered twice and first parameter will have one touch for each time and second parameter will have one at first touch and two at second touch. It means first parameter contains information for only touch newly began and second parameter has info for all touches.

Tips for detection of two moving touches >>>>

When ccTouchesMoved is triggered, there is no way to determine this moved touch comes from where.
Let’s say there are two touches(A, B) and one(A) moved to another location, we don’t know this touch was A or B.

BUT, when touch moves slowly, I can tell which one moves actually.
First of all, I had to save each location of two touches and when “touch move” event is triggered, check the distance between new moved location with each location of two saved touches.

I used this function to calculate the distance between two points(CGPoint)
static inline int
ptdiff(const CGPoint a, const CGPoint b)
{
return sqrt((a.x – b.x) * (a.x – b.x) + (a.y – b.y) * (a.y – b.y));
}

So, I just assume that nearer point is previous location.
Fact) The distance of two touches will be at least 20 because of finger size (Depending on finger size, it might be different)
With this fact, just find the point within 20.
Categories: Development, Tips, cocos2d Tags:
  1. No comments yet.
  1. No trackbacks yet.