2014-12-02 68 views
0

對於初學者我想動畫我比賽的主角,所以我可以在以後更換精靈陣列中的圖像 -Objective-C的動畫人物

伊夫在我的頭文件中這樣做對我的主角:

- (void)Animate; 

在我的實現文件中寫香港專業教育學院:

-(void) Animate{ 

    UIImage* img1 = [UIImage imageNamed:@"Obstacle.png"]; 
    UIImage* img2 = [UIImage imageNamed:@"Obstacle02.png"]; 
    UIImage* img3 = [UIImage imageNamed:@"Obstacle03.png"]; 
    UIImage* img4 = [UIImage imageNamed:@"Obstacle04.png"]; 

    NSArray *images = [NSArray arrayWithObjects:img1,img2,img3,img4, nil]; 
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 160.0)]; 
    //images.name = @"animation"; 

    [imageView setAnimationImages:images]; 
    [imageView setAnimationRepeatCount:0]; 
    [imageView setAnimationDuration:0.5]; 
    //imageView.center = character.center ; 
    [imageView startAnimating]; 

    // character used to be myView 
    // the images inside the array ! 
} 

記下我使用的Xcode的遊戲功能。

那麼我沒有做什麼,我該如何解決它?

+1

您需要將imageView添加到,例如,當前顯示的視圖控制器視圖,以便您可以看到它。現在你分配一個imageview,它將在方法返回時被釋放。 – 2014-12-02 08:30:35

回答

0
UIImage* img1 = [UIImage imageNamed:@"Obstacle"]; 
UIImage* img2 = [UIImage imageNamed:@"Obstacle02"]; 
UIImage* img3 = [UIImage imageNamed:@"Obstacle03"]; 
UIImage* img4 = [UIImage imageNamed:@"Obstacle04"]; 


NSArray *animateArray = [NSArray arrayWithObjects:img1,img2,img3,img4, nil]; 

self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160, 160)]; 

self.imageView.animationImages = animateArray; 

self.imageView.animationDuration = 3.0; 

[self.imageView startAnimating]; 

self.imageView.animationRepeatCount = 0; 

[self.view addSubview:self.imageView]; 
+0

非常感謝你,我瞭解這段代碼,但它的說法是我需要聲明img1,img2等,我也會用我之前提供的代碼替換這段代碼(我當然會改變它) – TheIOSTroubledProgrammer 2014-12-02 10:31:24

+0

哦,我唯一的問題是建立imageView作爲屬性 – TheIOSTroubledProgrammer 2014-12-02 10:43:00

+0

@TheIOSTroubledProgrammer是的,你需要聲明img1,2,3,就像你所做的一樣。還有一點,你不需要添加'.png',而PNG可以自動識別。但'jpg'等其他,你需要將它作爲後綴添加。 – 2014-12-02 11:34:05