【问题标题】:objective-c image moving problemObjective-C 图像移动问题
【发布时间】:2011-09-17 22:32:47
【问题描述】:

嘿,我遇到了问题。 我制作了一个图像对象,该对象每 2 秒由一个 nstimer 添加。 并且更新计时器会对其进行更新,以便图像继续前进。 但它只会继续前进,直到添加一个新的,我无法解决原因。

这是添加它的方法。

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

【问题讨论】:

    标签: iphone objective-c arrays object spawning


    【解决方案1】:

    这是因为您不断地重新分配图像。您需要每次都创建一个新的 image1 变量,然后将其添加到 NSMutableArray。

    然后在更新方法中使用for循环将数组中心的每个图像移动到任意点。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      相关资源
      最近更新 更多