【发布时间】:2009-07-11 18:54:15
【问题描述】:
我有一个数组和一个计时器,它每 5 秒向我的数组添加一个新对象,但遇到了一个小问题。我有一个可重复调用的代码,它移动我的数组中的对象,但由于某种原因,当一个新对象生成到我的数组中时,前一个生成的对象将停止移动。如何使数组中的每个对象都被调用,而不仅仅是最后生成的对象?
这是我用来移动对象的代码:
for (UIView *astroids in astroidArray) {
int newX = astroids.center.x + 0;
int newY = astroids.center.y + 1;
astroids.center = CGPointMake(newX,newY);
}
编辑:抱歉,这是我的数组代码:
// spawn the astroids every few seconds randomly along the x axis
// and remove them once they get off the screen
if ((gameStarted == YES) && (gameOver == NO)) {
int randomX = arc4random() % 320;
astroidArray = [[NSMutableArray alloc] init];
UIImage *astroid = [UIImage imageNamed:@"astroidwhite.png"];
UIImageView *astroids = [[UIImageView alloc] initWithImage:astroid];
//set X and Y of the new imageView
astroids.center = CGPointMake(randomX , -10);
//add to array
[astroidArray addObject:astroids];
//add to the view, so it gets displayed.
[self.view addSubview: astroids];
[astroids release];
}
【问题讨论】:
-
我认为问题在于如何将“机器人”添加到数组中。你能展示一下代码吗?
-
我更新了标签,因为这不是 iPhone 特有的,只是 Cocoa 内存管理的问题。还格式化了代码并将编辑包含在问题中。 (请删除下面多余的答案——最好在问题中包含完整的上下文。)最后一个建议:拼写为“asteroid”而不是“astroid”。您选择什么变量名称并不重要,但如果您向用户描述它,请注意拼写错误。 :-)
标签: objective-c cocoa arrays memory-management