简体   繁体   中英

Developing Iphone game - adding images at random

I am currently writing an iPhone game for my coursework - space ship flying through space, if asteroid is hit ship slows down, if star collected, ship speeds up - I currently have the ship travelling through space and one instance of a star and one instance of an asteroid each added using:

asteroid = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Asteroid.png"]];
[self addSubview:asteroid];

and

star = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"StarImage.png"]];
[self addSubview:star];

I would like to add multiple instances of these objects to the game in a random fashion, However I am unsure of the best way to do do this. Does anybody have any suggestions??

Cheers x

How about creating an NSMutableArray for stars and one for astreoids and then adding the instances of each to the corresponding NSMutableArray.

Then you need some methods to adjust the positions and speeds etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM