簡體   English   中英

物鏡圖像移動問題

[英]objective-c image moving problem

嘿,我遇到了問題。 我制作了一個由nstimer每2秒添加一次的圖像對象。 並且更新計時器會對其進行更新,以使圖像前進。 但它只會繼續進行,直到添加了一個新的,而我無法解決原因。

這是添加它的方法。

-(void)addTarget {

UIImage *image1=[UIImage imageNamed:@"enemy.png"];
                 image=[[UIImageView alloc]initWithImage:image1];
                 image.frame=CGRectMake(0,0,50,50);
                 [self.view addSubview:image];
image.center = CGPointMake(150, 150);
image.tag = 1;
[_targets addObject:image];
                     [image release];       
}

自我解釋。

-(void) update {
 image.center = CGPointMake(image.center.x+2, image.center.y);

}

這產生了它們。

-(void) spawn {
[self addTarget];
}

這是因為您不斷地重新分配映像。 您需要每次創建一個新的image1變量,然后將其添加到NSMutableArray中。

然后,在update方法中,使用for循環將數組中心的每個圖像移動到任意點。

- (void)update {
    for (UIImage *image in _targets) {
        image.center = CGPointMake(image.center.x+2, image.center.y);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM