1

I am trying to figure out touch handling on multiple CCNodes .

I have

 Main CCLayer
            ------> z:2 Hud CCNode

On main layer I choose an object, on hud layer I want to control it. I followed this q&a its very helpful Cocos2d handling touch with multiple layers

On Main Layer touch events, below is Hud Node work:

-(void) registerWithTouchDispatcher
{
   [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES]; 
}
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
    //detects touched Object and sends it to hud 

    if (object != nil)
    {
        //sends it to hud
        return true;
    }

    return false;
}
-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event

-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent*)event

-(void) ccTouchCancelled:(UITouch*)touch withEvent:(UIEvent*)event

On Hud CCNode none of ccTouchBegan/moved/ended methods are fired

-(void) registerWithTouchDispatcher
{
  [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES];

}
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
    if (object!=nil) {
        NSLog(@"Touch began");
        return YES;
    }
    else
        return NO;
}

EDIT: I have a button to set speed of the object on HUD Node , it has nothing to do with object!=nil because when I put breakpoints I see that -(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event is never called

-(void)speed1Tapped:(id)sender
{
    if (object!=nil) {
        NSLog(@"Moving Object is %@:",object!=nil);
     }
}
On Log I get:
object is <ObjectATC: 0x13763850>

Why ccTouchBegan/moved/ended methods are not fired in CCNode?

How can I handle touches on multiple CCNodes, CCLayers ?

Community
  • 1
  • 1
Mord Fustang
  • 1,523
  • 4
  • 40
  • 71

2 Answers2

0

In your touch methods, you have a conditional that says if (object != nil), but object is not defined anywhere in the code that you have shown. Is it defined elsewhere? Otherwise, this is why it is not firing.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • when I put break point at any of this methods, I see that break points is never reached, besides I have buttons that control same object plse see the updated question – Mord Fustang Feb 01 '13 at 15:34
  • @MordFustang and your break point is outside of the conditional? – dqhendricks Feb 01 '13 at 17:49
  • breakpoints don't stop unless there is code on that line if I am not mistaken. – dqhendricks Feb 01 '13 at 17:50
  • I put break points on lines `-(void) registerWithTouchDispatcher` and `-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event` – Mord Fustang Feb 01 '13 at 17:54
  • @MordFustang do you call the method, `registerWithTouchDispatcher` anywhere in your code? seems like this should be called in your onEnter method in order to have it register. You will also want to unregister in your onExit method, otherwise you may get zombies attached to the touch dispatcher that will cause crashes. – dqhendricks Feb 01 '13 at 19:46
0

I guess you dont receive touch method because you did not register the touchDipatcher

either in your CCNodes init method or onEnter method try to register it.

//register touches
- (void)onEnter {
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [super onEnter];
}

- (void)onExit {
    [[[CCDirector sharedDirector] touchDispatcher]removeDelegate:self];
    [super onExit];
}
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82