2013-01-22 29 views
0

我非常沮喪,我知道這是因爲我不知道我在做什麼與cocos2d。我正在關注cocos2d上的Ray Wenderlich教程,我正試圖將它們放在一起。當輕擊屏幕時,一個子彈朝水龍頭方向發射。我使用發射了多少子彈?

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 
    [self.officer shootToward:touchLocation]; 
    [self.officer shootNow]; 
} 

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 
    [self.officer shootToward:touchLocation]; 
    [self.officer shootNow]; 
} 

它調用了我的軍官階層這些方法

- (void)shootNow { 
    // 1 
    CGFloat angle = ccpToAngle(_shootVector); 
    _gun.rotation = (-1 * CC_RADIANS_TO_DEGREES(angle)) + 90; 

    // 2 
    CGSize winSize = [[CCDirector sharedDirector] winSize]; 
    float mapMax = MAX(winSize.width, winSize.height); 
    CGPoint actualVector = ccpMult(_shootVector, mapMax); 

    // 3 
    float POINTS_PER_SECOND = 300; 
    float duration = mapMax/POINTS_PER_SECOND; 

    // 5 
    for(id item in self.children) { 
    NSString *bulletName = [NSString stringWithFormat:@"bullet.png"]; 
    CCSprite * bullet = [CCSprite spriteWithSpriteFrameName:bulletName]; 
    //bullet.tag = _type; 
    bullet.position = ccpAdd(self.position, ccpMult(_shootVector, _gun.contentSize.height)); 
    CCMoveBy * move = [CCMoveBy actionWithDuration:duration position:actualVector]; 
    CCCallBlockN * call = [CCCallBlockN actionWithBlock:^(CCNode *node) { 
     [node removeFromParentAndCleanup:YES]; 
    }]; 
    [bullet runAction:[CCSequence actions:move, call, nil]]; 
    [self.batchNode addChild:bullet]; 

     //[self addChild:bullet]; 
     [_shotsFired addObject:bullet]; 
    } 
} 

所以我想這將是一個簡單的for循環經過第5步x量的時間,然後調用重載方法。那沒有用。所以我試圖計算屏幕上的觸摸,我想如果我得到了x的水龍頭然後調用重載方法(這還沒有寫入)?與此相關的問題是每次按下屏幕的不同區域時,計數都從一個開始。有些人請幫助我完成這一週拉長頭髮的漫長過程嗎?我如何計算我開槍的次數?

回答

2

難道你只是在你的視圖控制器上創建一個屬性,然後每次調用shoot方法時,只需將1添加到屬性中,然後在調用reload方法時將其重置爲0?

+0

哇多麼容易!我覺得在智能方面沒有那麼多! –