0

我的游戏有问题,它运行良好,直到我尝试添加动画,现在每当我去拍摄它时,我的游戏就会崩溃,我无法弄清楚我的动画代码有什么问题。下面我将输入导致忍者星射击的所有代码,但正如我所说,我相信错误出在动画中。

ccsprite Projectile 是被射击的东西,也是我试图制作动画的东西

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (StrategyBullet > 0) {
    //SOME IF STATEMENTS
    if (Strategyscore == 47) {
        StrategyBullet = StrategyBullet +5;
    }
    if (Strategyscore == 97) {
        StrategyBullet = StrategyBullet +5;
    }
    if (Strategyscore == 197) {
        StrategyBullet = StrategyBullet +10;
    }
    // Choose one of the touches to work with
    UITouch *touch = [touches anyObject];
    CGPoint location = [self convertTouchToNodeSpace:touch];

    // Set up initial location of projectile
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CCSprite *projectile = [CCSprite spriteWithFile:@"ninja star 1.png"];
    [self addChild:projectile z:2];

    {
        NSString *animationName = @"UNIQUE_ANIMATION_NAME";

        CCAnimation* animation = nil;
        animation = [[CCAnimationCache sharedAnimationCache]  animationByName:animationName];

        if(!animation)
        {
            NSMutableArray *animFrames = [NSMutableArray array];

            for( int i=1;i<=5;i++)
            {
                NSString* path = [NSString stringWithFormat:@"ninja star %d.png", i];
                CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:path];
                CGSize texSize = tex.contentSize;
                CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
                CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:tex rect:texRect];
                [animFrames addObject:frame];
            }

            animation = [CCAnimation animationWithSpriteFrames:animFrames];

            animation.delayPerUnit = 0.175f;
            animation.restoreOriginalFrame = YES;

            [[CCAnimationCache sharedAnimationCache] addAnimation:animation name:animationName];
        }

        if(animation)
        {
            CCAnimate *animAction  = [CCAnimate actionWithAnimation:animation];
            [projectile runAction:animAction];
        }
    }

    projectile.position = ccp(20, winSize.height/2);

    // Determine offset of location to projectile
    CGPoint offset = ccpSub(location, projectile.position);

    // Bail out if you are shooting down or backwards
    if (offset.x <= 0) return;

    // Ok to add now - we've double checked position
    [self addChild:projectile];

    int realX = winSize.width + (projectile.contentSize.width/2);
    float ratio = (float) offset.y / (float) offset.x;
    int realY = (realX * ratio) + projectile.position.y;
    CGPoint realDest = ccp(realX, realY);

    // Determine the length of how far you're shooting
    int offRealX = realX - projectile.position.x;
    int offRealY = realY - projectile.position.y;
    float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
    float velocity = 480/1; // 480pixels/1sec
    float realMoveDuration = length/velocity;
    // collison stuff
    projectile.tag = 2;
    [_projectiles addObject:projectile];
    StrategyBullet --;
    [StrategyBulletLabel setString:[NSString stringWithFormat:@"%d", StrategyBullet]];
    // Move projectile to actual endpoint
    [projectile runAction:
     [CCSequence actions:
      [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
      [CCCallBlockN actionWithBlock:^(CCNode *node) {
         [node removeFromParentAndCleanup:YES];
         // CCCallBlockN in ccTouchesEnded
         [_projectiles removeObject:node];

     }],
      nil]];

}

}

4

1 回答 1

0

崩溃是因为你 addChild 弹丸两次(我最好的猜测)。其余的看起来还不错,尽管我倾向于使用精灵表来制作动画,而不是像你那样使用基于文件的帧。

于 2013-10-21T13:43:28.840 回答