TouchesBegan不起作用 - iOS

时间:2013-03-20 10:45:22

标签: cocos2d-iphone

- (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;
}

问题在于

_nextProjectile = [[CCSprite spriteWithFile:@“newarrow.png”] retain];      _nextProjectile.position = ccp(430,190);

当我在init方法初始化__nextProjectile时,ccTouchBegan方法不起作用.. 并通过放置 _nextProjectile = [[CCSprite spriteWithFile:@“newarrow.png”]保留];         _nextProjectile.position = ccp(430,190); 在ccTouchBegan中,每次Touch执行时都会初始化。 任何帮助?!!

1 个答案:

答案 0 :(得分: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];

}