2012-07-16 91 views
0

我有此代碼來顯示一個圖像的動畫動畫圖像序列在cocos2d

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

我有動畫

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

的像6個圖像我把兩種方法, 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); 
    } 
} 

但是在第一種方法中動畫時間不正確,動畫有很多延遲,我只想隨機快速地顯示圖像的動畫,它是一個飛龍動畫。

我在方法上的秒不起作用,我沒有得到任何圖像的運動。

我希望你能理解我的問題。如何解決上述兩個方法問題。 在此先感謝。

+0

您可能希望有HTTP的讀取://www.cocos2d -iphone.org/wiki/doku.php/prog_guide:animation。特別是CCAnimate部分 – 2012-07-16 05:41:14

+0

@Ben我不能這樣做的精靈表的幫助,因爲我需要通過這個值thebaseimagename其他類 – stackiphone 2012-07-16 07:03:28

回答

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.. 
       } 
      } 
    } 
+0

讓我chk.thanks – stackiphone 2012-07-16 07:46:18

+0

這我不woking ...我用爲我的圖像製作movmnt – stackiphone 2012-07-16 07:55:55

+0

@stackiphone,是的,我也用我的形象運動,爲我工作。我想你不是指相同的精靈動畫幀,現在編輯我的答案,可能會有所幫助。 – Guru 2012-07-16 08:36:32