0

我有这个代码来显示图像的动画

-(void) blink:(ccTime) delta {


    animateblink ++; //adds 1 every frame

    if (animateblink <= 6 ) {  //if we included frames to show for breaking and the current frame is less than the max number of frames to play

        if (animateblink < 6) {


      [sprite setTexture:[[CCSprite spriteWithFile:[NSString stringWithFormat:@"%@_blinkk00%i.png", baseImageName,animateblink]] texture]];

    [self unschedule:_cmd];
    [self schedule:@selector(openEyes:) interval:0.1f];

            [self schedule:@selector(moveSprite:) interval:0.1f];
}
    }
}

我有 6 张类似动画的图片

dragonimage_blinkk001,dragonimage_blinkk002,dragonimage_blinkk003,dragonimage_blinkk004,dragonimage_blinkk005,dragonimage_blinkk006 like that

我放了两种方法,1:用于动画时间 2:用于图像的移动

代码是

-(void) openEyes:(ccTime) delta {

    [sprite setTexture:[[CCSprite spriteWithFile:[NSString stringWithFormat:@"%@.png", baseImageName]] texture]];


    [self unschedule:_cmd];

    int blinkInterval = (arc4random() % 6) + 1; // range 3 to 8
    [self schedule:@selector(blink:) interval:blinkInterval];
}


-(void)moveSprite:(ccTime)delta

{
    movementCounter ++;
    if (movementCounter == 1000) {
        [self unschedule:_cmd];
    }
    else
    {
        spriteimagename.position = ccp(spriteimagename.position.x+10,spriteimagename.position.y);
    }
}

但是第一种方法动画时间不正确,动画有很多延迟,我只想随机快速地显示图像的动画,它是龙飞动画。

我的第二个方法根本不起作用,我没有得到该图像的任何移动。

我希望你能理解我的问题。如何解决上述两个方法问题。提前致谢。

4

1 回答 1

0

快速解决方案是在 cocos2d CCSpriteFrame 类中添加 frameIndex 成员。我不知道是否允许更改。但它工作正常。

@interface CCSpriteFrame : NSObject <NSCopying>
{
        int          frameIndex;
        ....
}

//这里在代码中初始化具有唯一索引的帧

CCAnimation* animation;

    NSMutableArray *animFrames2 = [NSMutableArray array];
    for( int i=1;i<=15;i++)
    {
        CCSpriteFrame *frame = [cache spriteFrameByName:[NSString stringWithFormat:@"ActerSmash_%d.png",i]];
        frame.frameIndex = (int)i;
        [animFrames2 addObject:frame];
    }
    animation = [CCAnimation animationWithFrames:animFrames2];

    CCAnimate *action = [CCAnimate actionWithDuration:ACTER_SMASH_SPEED animation:animation restoreOriginalFrame:YES];
    action.tag = kTagAnimationSmashAnim ;
    mActer.AnimSmash = action;

//检查帧索引

  CCAnimate *anim = mActer.AnimSmash ;

    for(CCSpriteFrame *frame in anim.animation.frames)
    {
      if([mActer isFrameDisplayed:frame] )  )
            {
                if(frame.frameIndex == 5 )
                {
                    //Do something..
                }
            }
    }
于 2012-07-16T07:33:02.037 回答