2011-06-18 69 views
0

我目前有這個方法buttonHit,它調用playAnimationToNumber,它接受一個int,然後依次用來運行一個循環,爲每次迭代播放一個圖像數組,但它似乎只是在播放最後陣列..你能明白爲什麼嗎?因爲我有點失落,並希望得到幫助。animationArray only playing last image

//attached to button and it calls animation method. 
- (void)buttonHit:(id)sender{ 
    [self playAnimationToNumber:5]; 
} 



- (void)playAnimationToNumber:(int)number{ 

    for (int counter=1; counter<=number; counter++) { 

     NSString *imageNameForFirstNumber = [NSString stringWithFormat:@"Flap%i.png", counter]; 
     NSArray *imagesForAnimation = [NSArray arrayWithObjects:[UIImage imageNamed:@"FlapMoving1.png"], [UIImage imageNamed:@"FlapMoving2.png"], [UIImage imageNamed:imageNameForFirstNumber], nil]; 


     animationArray.animationImages = [[NSArray alloc] initWithArray:imagesForAnimation];   

     animationArray.animationDuration = 0.5; 
     animationArray.animationRepeatCount = 1; 
     [animationArray startAnimating]; 
     [self.view addSubview:animationArray]; 



    } 


    [animationArray release]; 
} 

工作代碼 ' - (空)playAnimationToNumber:(INT)號{

NSMutableArray *imagesForAnimation = [[NSMutableArray alloc] initWithCapacity:0]; 


for (int counter=1; counter<=number; counter++) { 

    NSString *imageNameForFirstNumber = [NSString stringWithFormat:@"Flap%i.png", counter]; 
    [imagesForAnimation addObject:[UIImage imageNamed:@"FlapMoving1.png"]]; 
    [imagesForAnimation addObject:[UIImage imageNamed:@"FlapMoving2.png"]]; 
    [imagesForAnimation addObject:[UIImage imageNamed:imageNameForFirstNumber]]; 
} 
animationArray.animationImages = [NSArray arrayWithArray:imagesForAnimation];   

animationArray.animationDuration = 5.9; 
animationArray.animationRepeatCount = 1; 
[animationArray startAnimating]; 
[self.view addSubview:animationArray]; 
[imagesForAnimation release]; 

}'

回答

2

在這裏你去試試這個代碼。

- (void)playAnimationToNumber:(int)number{ 

    NSMutableArray *imagesForAnimation = [[[NSMutable alloc] initWithCapacity:0] autoRelease]; 
    [imagesForAnimation addObject:[UIImage imageNamed:@"FlapMoving1.png"]]; 
    [imagesForAnimation addObject:[UIImage imageNamed:@"FlapMoving2.png"]]; 

     for (int counter=1; counter<=number; counter++) { 

      NSString *imageNameForFirstNumber = [NSString stringWithFormat:@"Flap%i.png", counter]; 
      [imagesForAnimation addObject:[UIImage imageNamed:imageNameForFirstNumber]]; 
    } 
      animationArray.animationImages = [NSArray arrayWithArray:imagesForAnimation];   

      animationArray.animationDuration = 0.5; 
      animationArray.animationRepeatCount = 1; 
      [animationArray startAnimating]; 
      [self.view addSubview:animationArray]; 



     } 
} 
+0

完美..謝謝你,只有兩個問題是,第二行 **的NSMutableArray ALLOC **,它不會讓我用自動釋放,所以我剛剛發佈的外部陣列for循環和它的工作完美。感謝大家!現在有時間根據一個數字實現幾個動畫:P指交叉。 – HurkNburkS

+0

實際上,儘管所有的數字都跑過了它,但它還是增加了中間翻轉的位置......所以第一個數字得到兩個中間翻牌的玩法,但之後每個數字只顯示數字......所以它的工作原理,但不是我打算如何我可能沒有解釋得很好。 – HurkNburkS

+0

我已經將工作代碼添加到了我的文章中,您非常有幫助,我只是將其他兩個圖像添加到for循環中,以便每次播放它們,而不僅僅是一次,謝謝。 – HurkNburkS