iOS开发中使用cocos2d添加触摸事件的方法

2020-01-14 16:58:26王冬梅

复制代码
-(void) setIsTouchEnabled:(BOOL)enabled

 

{

if( isTouchEnabled_ != enabled ) {

isTouchEnabled_ = enabled;

if( isRunning_ ) {

if( enabled )

[self registerWithTouchDispatcher];

else {

CCDirector *director = [CCDirector sharedDirector];

[[director touchDispatcher] removeDelegate:self];

}

}

}

}

//这句是关键

-(void) registerWithTouchDispatcher

{

CCDirector *director = [CCDirector sharedDirector];

[[director touchDispatcher] addStandardDelegate:self priority:0];

}

 

 

接下来就实现方法:

复制代码
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

 

{

//    for( UITouch *touch in touches ) {

// CGPoint location = [touch locationInView: [touch view]];

//       

// location = [[CCDirector sharedDirector] convertToGL: location];

// CGPoint touchPos = location;

//       

//        NSLog(@"point==%@",NSStringFromCGPoint(touchPos));

// }

   

   

    UITouch* touch = [touches anyObject];