5秒ごとに新しいオブジェクトを配列に追加するタイマーがありますが、わずかな問題が発生しています。私は、配列内のオブジェクトを移動するrepeatablyと呼ばれるコードを持っていますが、何らかの理由で、新しいオブジェクトが配列に生成されたときに、前回のオブジェクトの移動が停止します。最後に生成されたものだけでなく、配列内のすべてのオブジェクトが呼び出されるようにするにはどうすればよいですか?配列内のすべてのオブジェクトを移動しますか?
ここでオブジェクトを移動するために呼ばれています私のコードです:
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];
}
私は問題が配列に 'androids'を追加する方法にあると思います。あなたはそのコードを表示できますか? – drvdijk
alright投稿コード –
これはiPhone固有のものではないため、タグを更新しました。ちょうどCocoaメモリ管理の問題です。また、コードを書式設定し、質問に編集内容を含めました。 (以下の冗長な答えを削除してください - 完全な文脈が質問に含まれているときに最適です)最後の提案: "astroid"ではなく "asteroid"と綴られています。あなたが選ぶ変数名は大したことではありませんが、ユーザーに説明すると、スペルミスに気づくでしょう。 :-) –