Вопрос

I am developing a game using cocos2d-iphone 3.0. I use this bool to make code inside (after user touches sprite) be implemented only once. When user touches sprite second time, this code will be not implemented. The only problem is, that Xcode says that touchBegan method should not return a value ( return NO ;). How can I change the method, so there will be no mistake? Should I change something else? Is this way correct at all?

bool firstClicked = false;
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
 if(firstClicked==true)
 {
    return NO ;
 }


CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedlocation = [[CCDirector sharedDirector] convertToGL: location];
CGPoint convertedNodeSpacePoint = [self convertToNodeSpace:convertedlocation];

if (CGRectContainsPoint([_sprite boundingBox],convertedNodeSpacePoint))
    {  firstActFirstTimeClicked=true;
.......// some code. not important 
} }
Это было полезно?

Решение

If you only need to do it once, you would want to deactivate the touch began on your sprite. So in your custom overriden sprite just do something like this :

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
 {
    // Do awesome game related code over here
    self.userInteractionEnabled = NO;
 }

This should work on a custom CCSrpite. This way you don't have to handle all that silly position code in your scene, and you are ready to go with one line of code.

Obviously you need to enable user interaction when you are creating your CCSpritein order for the touchBegan to be called in the first place.

Другие советы

Dont return a BOOL. If you want to run the code only once then simply return from the method. No need to return a BOOL NO. Try this..

 bool firstClicked = false;
 -(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
   if(firstClicked==true)
     {
        return;
     }


     CGPoint location = [touch locationInView: [touch view]];
     CGPoint convertedlocation = [[CCDirector sharedDirector] convertToGL: location];
     CGPoint convertedNodeSpacePoint = [self convertToNodeSpace:convertedlocation];

     if (CGRectContainsPoint([_sprite boundingBox],convertedNodeSpacePoint))
       {  firstActFirstTimeClicked=true;
          .......// some code. not important 
       } }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top