简体   繁体   中英

cocos2D Touching screen crash

My application crashes when these blocks of code are executed. There are no errors, just warnings. The warning says, "Conflicting return type in implementation of 'ccTouchesBegan:withEvent':'void'VS'BOOL'(aka 'signed char')" Please help.

-(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
return YES;
}

-(BOOL) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *myTouch = [touches anyObject];
CGPoint point = [myTouch locationInView:[myTouch view]];
point = [[CCDirector sharedDirector] convertToGL:point];

CCNode *player = [self getChildByTag:kTagPlayer];
[player setPosition:point];

CCNode *computer = [self getChildByTag:kTagComputer];
[computer runAction:[CCMoveTo actionWithDuration:3.0
                                     position:ccp(player.position.x, player.position.y)]
 ];

return YES;
}

As your warning states these methods are required to return nothing ( void ), not a boolean value. Try to change it and see if it fixes the warning, otherwise the problem lies in your code and not in how these methods are called.

The return type on

-(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

should be (void)

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

}

there no return anything.

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

and you should use this for single touch;

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;

it also support multi touch:

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    NSArray *allTouches = [[event allTouches] allObjects];
    for (int i=0; i<[allTouches count];i++) {
        if ([some conditions]) {
            CGPoint position = [(UITouch*)[allTouches objectAtIndex:i] locationInView:[[CCDirector sharedDirector]openGLView]];
            position.y = [[CCDirector sharedDirector]winSize].height - position.y;
            //deal with the position;
            return TRUE;
        }
    }
return FALSE;

}

you must active touch interaction first:

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
//if support multi touch you must set swallowsTouches "NO".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM