2013-03-20 109 views
0
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 

    if (_nextProjectile != nil) return; 

    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    [[SimpleAudioEngine sharedEngine] playEffect:@"pew-pew-lei.caf"]; 

    CGSize winSize = [[CCDirector sharedDirector] winSize]; 

    CGRect rect = CGRectMake(imgArrow.position.x, imgArrow.position.y,imgArrow.contentSize.width, imgArrow.contentSize.height); 

    if(CGRectContainsPoint(rect, location)) 
    { 
     **_nextProjectile = [[CCSprite spriteWithFile:@"newarrow.png"]retain]; 
     _nextProjectile.position = ccp(430,190);** 

     [imgArrow runAction:[CCSequence actions: 
          [CCRotateTo actionWithDuration:0 angle:0], 
          [CCCallFunc actionWithTarget:self selector:@selector(finishShoot)], 
          nil]]; 

     [blankBow setVisible:true]; 
     [imgArrow setVisible:false]; 
     [walkAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"bow1.png", 0]]]; 

     [walkAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"bow2.png", 1]]]; 

     [walkAnimFrames addObject: 
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: 
      [NSString stringWithFormat:@"bow3.png", 2]]]; 

     walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.2f]; 

     self.walkAction = [CCRepeatForever actionWithAction: 
          [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]; 

     self.walkAction.tag = 1; 
     [blankBow runAction:_walkAction]; 

     CCSprite *sprite = blankBow; 

     CCCallBlock *block = [CCCallBlock actionWithBlock:^{ 
      [sprite stopActionByTag:1]; 
     }]; 
     CCDelayTime *time = [CCDelayTime actionWithDuration:0.3]; 
     [sprite runAction:[CCSequence actions:time, block, nil]]; 

     if(flagmultitouch == TRUE) 
     { 
      id actionMove = [CCMoveTo actionWithDuration:1.5 
               position:ccp(-48,160)]; 
      id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                selector:@selector(spriteMoveFinished:)]; 
      [_nextProjectile runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; 

      flagmultitouch = FALSE; 
     } 

     if(_nextProjectile !=nil) 
      _nextProjectile.tag = 2; 
    } 
    else 
    { 

    } 

    return YES; 
} 

問題是與ccTouchBegan不工作-IOS

_nextProjectile = [[CCSprite spriteWithFile:@ 「newarrow.png」]保留]; _nextProjectile.position = ccp(430,190);

當我在init方法初始化__nextProjectile ccTouchBegan方法不起作用.. 和由puting _nextProjectile = [[CCSprite spriteWithFile:@ 「newarrow.png」]保留]; _nextProjectile.position = ccp(430,190); ccTouchBegin每當Touch執行時都會初始化。 有什麼幫助?

+0

把斷點放在ccTouchBegan的頂部,是命中? – Guru 2013-03-20 10:48:00

+0

上面的代碼工作正常,但是當我按對象兩次對象_nextProjectile出現TWICE..Which意味着_nextProjectile每次重新初始化.. – 2013-03-20 10:55:02

+0

如果(_nextProjectile!= nil)返回; – Guru 2013-03-20 10:58:10

回答

2

爲同一圖層啓用觸摸。

-(void)onEnter 
{ 
    [super onEnter]; 

    self.touchEnabled = YES; //In Cocos2d 1.0: self.isTouchEnabled 
} 



- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView: [touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

}