Objective-C 图像移动问题
嘿,我遇到了问题。 我制作了一个图像对象,由 ntimer 每 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];
}
hey i am running into a problem.
i maade an image object that gets added every 2 seconds by an nstimer.
and an update timer updates it so the image goes forward.
but it only goes forward until a new one gets added and i can't solve why.
this is the method for adding it.
-(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];
}
self explaining.
-(void) update {
image.center = CGPointMake(image.center.x+2, image.center.y);
}
and this spawns them.
-(void) spawn {
[self addTarget];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为你不断地重新分配图像。您每次都需要创建一个新的 image1 变量,然后将其添加到 NSMutableArray 中。
然后在 update 方法中使用 for 循环将数组中心的每个图像移动到任意点。
Its because you are constantly reallocating the image. You need to be creating a new image1 variable every time and then adding it to an NSMutableArray.
Then in the update method use a for loop to move each image in the array's centre to whatever point.