How do you detect strokes in CCScene on Cocos2D V3?

I know you could do this:

<CCLayerObject>.isTouchEnabled = YES;

... in combination with registration to the touch manager:

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

... and then just get the callbacks:

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint point = [self convertTouchToNodeSpace:touch];
    NSLog(@"X:%ftY:%f", point.x, point.y);
}

But what do you need to do in V3?

+3
source share
2 answers

It seems like all I needed to do:

In the CCScene constructor, activate:

[self setUserInteractionEnabled:YES];

Add the touchBegan method to:

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    NSLog(@"touchBegan");
}

TA-dah! Much easier in V3 than in V2!

Additionally, you can also:

[self setMultipleTouchEnabled:YES];
+4
source

In Cocos2d 3.0, any CCNode can receive strokes. All you have to do is enable the strokes:

self.userInteractionEnabled = YES;

Then you have four different stroke processing methods:

  • touchBegan : called when the user touches the screen

  • touchMoved:: , .

  • touchEnded: ,

  • touchCancelled: ,
    Node (,
    touch Node)

Node :

- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchLocation = [touch locationInNode:self];
    currentHero.position = touchLocation;
}

: https://makegameswith.us/gamernews/366/touch-handling-in-cocos2d-30

+2

All Articles