V.D
V.D

Reputation: 403

ccTouchesMoved works but ccTouchMoved does not

self.isTouchEnabled = YES; in init method ofcourse.

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

}

in Above code ccTouchesMoved works fine but ccTouchMoved doesn't call.. any help ?!

Upvotes: 1

Views: 1740

Answers (2)

Stephane Delcroix
Stephane Delcroix

Reputation: 16230

There's 2 behaviours: one for standard touches, one for multiple touches. You don't have to addTargetedDelegate:::, you can simply set the touchMode property to the value you like. The CCLayer will take care of the registration for you.

- (void)onEnter
{
    [self setTouchMode: kCCTouchesAllAtOnce]; //resp kCCTouchesOneByOne
}

Behind the scenes, changing the touchMode will disable then re-enable the touches, and enabling the touches (enableTouch) will register the proper delegate for you, by calling either addStandardDelegate or addTargetedDelegate.

Upvotes: 0

wenbo qiu
wenbo qiu

Reputation: 104

Cocos2d supports two different ways of handling touch events. These are defined by two different types of delegates (both defined in CCTouchDelegateProtocol.h).

Standard Touch Delegate: @optional

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 

These are the same sorts of events you'd get in a standard CocoaTouch app. You'll get all events, and all touches; it will be up to you to sort out which touches you care about in a multi-touch environment.To get these events in a CCLayer subclass, you simply set isTouchEnabled = YES, like so:

self.isTouchEnabled = YES;

Targeted Touch Delegate

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; 
@optional 
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event; 
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event; 
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event; 

Note two important differences between the targeted and the standard touch delegate:

  1. These methods provide only a single touch, rather than a set of them – and for that reason, the method names start with “ccTouch” rather than “ccTouches”.

  2. The ccTouchBegan method is required and returns a boolean value.

So ccTouchBegan will be invoked separately for each of the available touches, and you return YES to indicate a touch you care about. Only touches claimed by ccTouchBegan will be subsequently passed on to the Moved, Ended, and Cancelled events (all of which are optional).

To receive these events, you must register as a targeted touch delegate with the global dispatcher. In a CCLayer subclass, override registerWithTouchDispatcher as follows:

-(void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; }

(which will mean importing “CCTouchDispatcher.h” at the top of your file).

Which to use?

Apart from the more complex registration, the targeted touch delegate is generally easier to use, since you don't have to split the NSSet up yourself, and you don't have to keep checking whether the event is the one you want in the Moved/Ended/Cancelled events. But if you want to deal with multiple touches in one method (for example, because you combine them into a zoom or rotate input), you'll probably want to use the standard touch delegate instead. Note that you can only use one or the other.

Upvotes: 3

Related Questions