dcp3450
dcp3450

Reputation: 11187

How do I create new instances of an object in ios?

I'm working on a concept that will have objects falling from the top of the screen. For this example lets say, 4 objects: red square, blue square, green square, yellow square.

I have all of these objects placed in the interface builder and connected with the names given in the .h files: IBObject UIImageView *greenSquare; for example.

I able to interact with the "greenSquare" fine and have it fall from the top of the view to the bottom and disappear off the view. Right now I want to be able to recreate the "greenRectangle" and have it fall again. I can't just reset that one instance since at any given point 1 or more of the same object would be falling.

The end result would be x number of the squares of any of the colors would be showing and falling.

I'm sure my workflow for this would be:

Any push in the right direction would be great. Thanks everyone!

Upvotes: 0

Views: 1380

Answers (3)

dcp3450
dcp3450

Reputation: 11187

here is what I did:

in my .h file I created the object instances:

UIImageView *yellowImage;
UIImageView *greenImage;
UIImageView *orangeImage;
UIImageView *redImage;

UIImage *yellow;
UIImage *green;
UIImage *orange;
UIImage *red;

I then set the images:

green = [UIImage imageNamed:@"greenball.png"];
yellow = [UIImage imageNamed:@"yellowball.png"];
orange = [UIImage imageNamed:@"orangeBall.png"];
red = [UIImage imageNamed:@"redball.png"];

I called a method and set a timer:

[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

finally, inside the onTimer I create a random number that chooses a ball and creates it like this:

greenImage = [[UIImageView alloc] initWithImage:green];
greenImage.frame = CGRectMake(startX,0,43,43);
[self.view insertSubview:greenImage belowSubview:bottomBar];

Upvotes: 0

Philipp Kyeck
Philipp Kyeck

Reputation: 18850

why do you use InterfaceBuilder for this - switch to a "code only" implementation and you should be just fine.

1) create image

UIImage *green = [UIImage imageNamed:@"green.png"];
UIImageView *img = [[UIImageView alloc] initWithImage:green];
[view addSubview:img];

2) position it

img.center = CGPointMake(<RANDOM NUMBER>, -30);

3) start animation
4) when it hits the bottom, remove it

[img removeFromSuperview];

5) add this img to the top again ...

if you want to have multiple of these green img you can create more than one at a time. if just one is enough you could reuse the img every time the animation reached the bottom.

edit

if you want to create multiple UIImageViews from one UIImage do it like this:

UIImage *img = [UIImage imageNamed:@"green"];

UIImageView *imgView1 = [[UIImageView alloc] initWithImage:img];
imgView1.center = CGPointMake(100, 100);
[self.view addSubview:imgView1];

UIImageView *imgView2 = [[UIImageView alloc] initWithImage:img];
imgView2.center = CGPointMake(100, 500);
[self.view addSubview:imgView2];

Upvotes: 2

blauzahn
blauzahn

Reputation: 351

Maybe you should think about to set the rectangles y position back to the top of screen just when it disappears at the bottom. So you don´t have to recreate it and can reuse it. Dan

Upvotes: 0

Related Questions